Pages

Showing posts with label sum. Show all posts
Showing posts with label sum. Show all posts

Monday, February 16, 2015

C program to find sum of elements above and below the main digonal of a matrix


C program to find sum of elements above and below the main digonal of a matrix

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,m,n,d1=0,d2=0,a[5][5];
clrscr();
printf("How many rows and columns:");
scanf("%d%d",&m,&n);
printf("Enter matrix elements:
");


for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
scanf("%d",&a[i][j]);
if(j>i)
d1+=a[i][j];
else
if(i>j)
d2+=a[i][j];
}

printf("Sum of elements above the digonal=%d
",d1);

printf("Sum of elements below the digonal=%d",d2);
getch();
}
Read more »

Saturday, February 14, 2015

C program to print first n natural numbers and their sum


#include<stdio.h>
#include<conio.h>

void main()
{
int n,i,sum=0;
clrscr(); //to clear the screen
printf("Enter the value of n:");
scanf("%d",&n);

for(i=1;i<=n;++i)
{
printf("
%d",i);

sum+=i;
}
printf("

Sum of first n natural numbers is:%d",sum);

getch(); //to stop the screen
}
Read more »