Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	for stdin.Scan() {
		n, _ := strconv.Atoi(stdin.Text())
		if n == 0 {
			break
		}

		a := make([][]int, n+1)
		for i := 0; i < n; i++ {
			sum := 0
			stdin.Scan()
			a[i] = make([]int, n+1)
			str := strings.Split(stdin.Text(), " ")
			for j := 0; j < n; j++ {
				a[i][j], _ = strconv.Atoi(str[j])
				sum += a[i][j]
				fmt.Printf("%5d", a[i][j])
			}
			a[i][n] = sum
			fmt.Printf("%5d\n", a[i][n])
		}
		a[n] = make([]int, n+1)
		for i := 0; i <= n; i++ {
			sum := 0
			for j := 0; j < n; j++ {
				sum += a[j][i]
			}
			a[n][i] = sum
			fmt.Printf("%5d", a[n][i])
		}
		fmt.Println()
	}
}

C++: #include<iostream>
#include<cstdio>
using namespace std;

int main(){
    int n;
    while(cin>>n , n){
        int table[n+1][n+1];
        
        for(int i=0; i<=n; i++){
            table[i][n]=0;
            table[n][i]=0;
        }
        
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                cin>>table[i][j];
            }
        }
        

        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
               table[i][n] += table[i][j];
            }
        }
        
        for(int j=0; j<=n; j++){
            for(int i=0; i<n; i++){
                table[n][j] += table[i][j];
            }
        }
        
        for(int i=0; i<=n; i++){
            for(int j=0; j<=n; j++){
                printf("%5d",table[i][j]);
            }
            cout<<endl;
        }
    }
    
    return 0;
}

C++: import java.util.*;
import java.lang.*;
import java.io.*;

public class Main{
	public static void main (String[] args) throws java.lang.Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    String line = br.readLine();
	    while(!line.equals("0")){
	        int n = Integer.parseInt(line);
	        int[] sum_arr = new int[n];
            int[][] table = new int[n+1][n+1];
	        for(int i=0;i<n;i++){
	            line = br.readLine();
	            String[] lines = line.split(" ");
	            int j=0;
	            int sum = 0;
	            for(String st:lines){
	                int tmp = Integer.parseInt(st);
	                table[i][j] = tmp;
	                sum += tmp;
	                sum_arr[j] += tmp;
	                ++j;
	            }
	            table[i][j] = sum;
	        }
	        int sum = 0;
	        for(int i=0;i<n;i++){
	            table[n][i] = sum_arr[i];
	            sum += sum_arr[i];
	        }
	        table[n][n] = sum;
	        
	        int length = Integer.toString(sum).length();
	        String fmt = "%" + length + "s";
	        for(int[] t:table){
	            int cnt = 0;
	            for(int val:t){
	                if(cnt>0){
    	                System.out.print(" ");
	                    cnt++;
	                }
	                System.out.print(String.format(fmt,Integer.toString(val)));
	            }
                System.out.println("");
	        }
            line = br.readLine();
	    }
	}
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
	int n;
	int data[101][101] = {0};
	cin >> n;
	rep(i,n)rep(j,n)
		cin >> data[i][j] , data[n][n] += data[i][j];
	rep(i,n){
		int s = 0;
		rep(j,n) s+= data[i][j];
		data[i][n] = s;
	}
	rep(i,n){
		int s = 0;
		rep(j,n) s+= data[j][i];
		data[n][i] = s;
	}
	rep(i,n+1){
		rep(j,n+1)
			printf("%5d",data[i][j]);
		cout << endl;
	}
}

C++: #include<iostream>
using namespace std;
int main(){
  int n;
  while(cin>>n){
    if(n==0) break;
    int s[n+1][n+1];
    for(int i=0;i<n;i++){
      int a = 0;
      for(int j=0;j<n;j++){
	cin >> s[i][j];
	a += s[i][j];
      }
      s[i][n] = a;
    }
    for(int i=0;i<n;i++){
      int a = 0;
      for(int j=0;j<n;j++){
	a += s[j][i];
      }
      s[n][i] = a;
    }
    int a = 0;
    for(int i=0;i<n;i++) a += s[n][i]; 
    s[n][n] = a;
    for(int i=0;i<=n;i++){
      for(int j=0;j<=n;j++){
	if(s[i][j]>=10000) cout << s[i][j] << endl;
	else if(s[i][j]>=1000) cout << " " << s[i][j];
	else if(s[i][j]>=100) cout << "  " << s[i][j];
	else if(s[i][j]>=10) cout << "   " << s[i][j];
	else if(s[i][j]>=0) cout << "    " << s[i][j];
      }
      cout << endl;
    }
  }
  return 0;
}

C++: #include <iomanip>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);

    int n;
    while (std::cin >> n && n > 0) {
        std::cin.ignore();

        std::vector<int> vec;
        std::string s;
        for (int i = 0; i < n; i++) {
            std::getline(std::cin, s);

            std::stringstream ss(s);
            std::copy(std::istream_iterator<int>(ss), {},
                      std::back_inserter(vec));
        }

        for (int i = 0; i < n; i++) {
            int sum = 0;
            for (int j = 0; j < n; j++) {
                std::cout << std::right << std::setw(5) << vec[n * i + j];
                sum += vec[n * i + j];
            }
            std::cout << std::right << std::setw(5) << sum << std::endl;
        }
        int total = 0;
        for (int i = 0; i < n; i++) {
            int sum = 0;
            for (int j = 0; j < n; j++) { sum += vec[n * j + i]; }
            std::cout << std::right << std::setw(5) << sum;
            total += sum;
        }
        std::cout << std::right << std::setw(5) << total << std::endl;
    }
}

C++: #include<stdio.h>
int main(void)
{
	int n,x[101][101],i,j,sum;
	while(1){
		scanf("%d",&n);
		if(n==0) break;
		for(i=0;i<n;i++){
			for(j=0;j<n;j++) scanf("%d",&x[i][j]);
		}
		for(i=0;i<n;i++){
			sum=0;
			for(j=0;j<n;j++) sum+=x[j][i];
			x[n][i]=0;
			x[n][i]=sum;
		}
		for(i=0;i<=n;i++){
			sum=0;
			for(j=0;j<n;j++) sum+=x[i][j];
			x[i][n]=0;
			x[i][n]=sum;
		}
		for(i=0;i<=n;i++){
			for(j=0;j<=n;j++){
				printf("%d ",x[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <cstdio>
#include <map> 
#include <sstream>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

int main(void){
  int n;
  while(cin>>n,n){
    vector<vector<int> > in(n+1,vector<int>(4,0));
    rep(i,n){
      rep(j,4){
	cin>>in[i][j];
	printf("%5d",in[i][j]);
	in[i][4] += in[i][j];
	in[n][j] += in[i][j];
      }
      printf("%5d\n",in[i][4]);
    }
    rep(j,4){
      printf("%5d",in[n][j]);
      in[n][4] += in[n][j];
    } 
    printf("%5d\n",in[n][4]);
  }

}

C++: 
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <utility>

#define ll (long lon
#define REP(i,n) for(int i = 0;i<(int)n;i++)
#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (a);i>(b);i--)

using namespace std;

int main (void){
    
    int number;
    while(cin >> number,number)
    {
        int math[11][11] ={0};
        REP(i,number)
        {
            REP(j,number)
            cin >> math[i][j];
        }
        REP(i,number)
            REP(j,number)
        math[i][number] += math[i][j];
        REP(i,number)
            REP(j,number)
        math[number][i] += math[j][i];
        REP(i,number)
        REP(j,number)
        math[number][number]+= math[i][j];
        REP(j, number+1)
        REP(i,number+1){
            printf("%5d",math[j][i]);
            if(i == number || (j == number && i == number))
                cout << endl;
                 }
    }
    return 0;
    
  
}