C program for the addition of N natural numbers using if else statements
kw.c
#include<stdio.h>intmain(){intn,sum;printf("———————————————————————————————————————————");printf("\nProgram for the addition of N numbers");printf("\n———————————————————————————————————————————");printf("\nEnter the number ");scanf("%d",&n);if(n>0){sum=(n*(n+1)/2);printf("\nSum of %d natural numbers = %d",n,sum);}else{printf("\nPlease enter the positive natural number");}printf("\n———————————————————————————————————————————\n");return0;}
Output
kodingwindow@kw:~$ gcc kw.c kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Program for the addition of N numbers
———————————————————————————————————————————
Enter the number 0
Please enter the positive natural number
———————————————————————————————————————————
kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Program for the addition of N numbers
———————————————————————————————————————————
Enter the number 10
Sum of 10 natural numbers = 55
———————————————————————————————————————————
kodingwindow@kw:~$
C program for the addition of N natural numbers using while loop
kw.c
#include<stdio.h>intmain(){floatsum=0,n=1;printf("———————————————————————————————————————————");printf("\nProgram for the addition of N numbers");printf("\n———————————————————————————————————————————");printf("\nEnter the numbers\n");while(n!=0){sum+=n;scanf("%f",&n);}printf("Sum = %f",sum-1);printf("\n———————————————————————————————————————————\n");return0;}
Output
kodingwindow@kw:~$ gcc kw.c kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Program for the addition of N numbers
———————————————————————————————————————————
Enter the numbers
-10
-50
10
30
0
Sum = -20.000000
———————————————————————————————————————————
kodingwindow@kw:~$
Dear User, Thank you for visitng Kodingwindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.
Dear User, We are grateful for your interest in the Kodingwindow Android app. We intend to soon make it available on the Google Play Store. Currently, Android versions 10 and up require manual apk installations. Would you like to get the app?