AIM
To
write a C program to find the sum of ‘n’ natural numbers and its sum.
ALGORITHM
Step
1: Start
Step 2: Read n
Step 3: if (n=0), if not go to step 5
Step 4: Print Invalid number, go to step
2
Step 5: ißn,
sß1,
Print i
Step 6: if (i=0), if not go to step 8
Step 7: Print s, go to step 10
Step 8: sß(s+1)
Step 9: iß(i-1),
go to step 4
Step 10: Stop
PROGRAM
#include<conio.h>
#include<stdio.h>
void main()
{
int
i,n,s=0;
printf(“Enter your
limit:”);
scanf(“%d”,&n);
if (n==0)
printf(“\nEnter a
valid number”);
printf(“Numbers:”);
for (i=1;i<=n;i++)
{
printf(“%d\t”,i);
s=s+i;
}
printf(“\nSum:%d”,s);
getch();
}
No comments:
Post a Comment