Sum of Natural number in C language..

Let’s understand some concept.

Natural Number..

All counting number which start from 1, 2, 3, 4, 5, 6, 7, 8, 9…………………. Infinite…

Sum of Natural Number a=n/2*(n+1) if you enter number then the sum done by this formula…

Some important symbols used in code.

  • () small bracket
  • && use for ‘and’ use for address
  • {} curly bracket
  • “” double code

The sum of Natural Number by using Formula..

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a, b=0, c;
  6. clrscr();
  7. printf(“Sum of Natural number from 1 to 100”)
  8. a=100;
  9. b=a/2*(b+1);
  10. printf(“%d”, b);
  11. getch();
  12. }

Inputs..

If a=100 then

Results..

The sum of Natural Number by using ‘for’ loop…

If you enter range of Natural Number then using a loop. A after increment natural Number added by this method b+=a which like a++. In loop same rule follow by Increment and adding in on container like all alphabet.

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a, b, c=0;
  6. clrscr();
  7. printf(“Sum of naturanumber”);
  8. for(a=1;a<=100;a++)
  9. {
  10. c+=a;
  11. }
  12. printf(“%d”, c);
  13. getch();
  14. }

Inputs..

If a=100 then

Results..

The sum of Natural Number by using ‘while’ loop..

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a, b=0, c;
  6. clrscr();
  7. printf(“Sum of Natural Number”);
  8. a=100;
  9. while(a<=100)
  10. {
  11. b+=a;
  12. a++;
  13. }
  14. printf(“%d”, b);
  15. getch();
  16. }

Inputs..

If a=100 then

Results..

The sum of Natural Number by using ‘do while’ loop…

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a=1, b=0, c;
  6. clrscr();
  7. printf(“Sum of Natural Number”);
  8. do
  9. {
  10. b+=a;
  11. a++;
  12. }
  13. while(a<=100);
  14. printf(“%d”, b);
  15. getch():
  16. }

Inputs..

If a=100 then

Results..

Leave a comment

Design a site like this with WordPress.com
Get started