Matrix Row Sum
Given a R*C matrix (R - rows and C- Columns), print the sum of the values in each row as the output.
Input Format:
First line will contain R and C separated by a space.
Next R lines will contain C values, each separated by a space.
First line will contain R and C separated by a space.
Next R lines will contain C values, each separated by a space.
Output Format:
R lines representing the sum of elements of rows 1 to R.
R lines representing the sum of elements of rows 1 to R.
Boundary Conditions:
2 <= R, C <= 50
2 <= R, C <= 50
Example Input/Output 1:
Input:
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Input:
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
10
26
42
58
Code:10
26
42
58
#include <iostream>
using namespace std;
int main(int argc, char**
argv)
{
int r,c,i,j,sum=0;
cin>>r>>c;
int arr;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>arr;
sum+=arr;
}
cout<<sum<<"\n";
sum=0;
}
}
Please do comment If u have any Queries!
::::PYTHON 3 :::
ReplyDeletedim = list(map(int, input().split()))
for i in range(0,dim[0]):
a = list(map(int, input().split()))
print(sum(a))
#include
ReplyDeleteusing namespace std;
int main(int argc, char** argv)
{
int r,c,t;
cin>>r>>c;
for(int i=0;i>t;
sum+=t;
}
cout<<sum<<"\n";
}
}
#include
ReplyDeleteusing namespace std;
int main(int argc, char** argv)
{
int r,c,t;
cin>>r>>c;
for(int i=0;i>t;
sum+=t;
}
cout<<sum<<"\n";
}
}
#include
ReplyDeleteusing namespace std;
int main(int argc, char** argv)
{
int r,c,t;
cin>>r>>c;
for(int i=0;i<r;i++){
int sum=0;
for(int j=0;j<c;j++){
sum+=t;
}
cout<<sum<<"\n";
}
}
import java.util.*;
ReplyDeletepublic class Hello {
public static void main(String[] args) {
//Your Code Here
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int arr[][]=new int[a][b];
//int sum=0;
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
arr[i][j]=sc.nextInt();
}
}
for(int i=0;i<a;i++)
{
int sum=0;
for(int j=0;j<b;j++)
{
sum+=arr[i][j];
}
System.out.println(sum);
}
//System.out.println("krce");
}
}