Sum of Even number & between two range in C language…

Sum of even number between the two range number are given below….

Let’s understand some concept.

Even Number..

Number which divided by 2 with 0 remainder is known as Even number.

Sum of even Number. if you enter number then the sum done by this formula…If a=last term 100. Then n=a/2. So, x=n*(n+1).

Some important symbols used in code.

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

Sum of even number by using formula…

If you Enter an Even number or odd number. This code for both case..

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a,b,c,d,e;
  6. clrscr();
  7. printf(“Enter the to find sum of even number upto range”);
  8. scanf(“%d”, &a);
  9. if(a%2==0)
  10. {
  11. b=a/2;
  12. c=b*(b+1);
  13. printf(“%d”,c);
  14. }
  15. else if(a%2!=0)
  16. {
  17. b=(a-1)/2;
  18. c=b*(b+1);
  19. printf(“%d”, c);
  20. }
  21. getch();
  22. }

Inputs..

If a=100 then

Results..

Sum= 2550

Sum of even number by using ‘for’ loop

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a,b,c=0,d,e;
  6. clrscr();
  7. printf(“Enter the to find sum of even number upto range”);
  8. scanf(“%d”, &a);
  9. for(b=2;b<=a;b++)
  10. if(b%2==0)
  11. {
  12. c+=b;
  13. }
  14. printf(“%d”, c);
  15. getch();
  16. }

Inputs..

If a = 100 then

Results..

Sum= 2550.

Sum of even number by using ‘while’ loop…

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a,b=2,c=0;
  6. clrscr();
  7. printf(“Enter the to find sum of even number upto range”);
  8. scanf(“%d”, &a);
  9. while(b<=a)
  10. {
  11. if(b%2==0)
  12. c+=b;
  13. b++;
  14. }
  15. printf(“%d”, c);
  16. getch();
  17. }

Inputs..

If a=100 then

Results..

Sum= 2550

Sum of even number by using ‘do while’ loop…

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a,b=2,c=0;
  6. clrscr();
  7. printf(“Enter the to find sum of even number upto range”);
  8. scanf(“%d”, &a);
  9. do
  10. {
  11. if(b%2==0)
  12. c+=b;
  13. b++;
  14. }
  15. while(b<=a);
  16. printf(“%d”, c);
  17. getch();
  18. }

Inputs..

If a=100 then

Results..

Sum= 2550..

Sum of even number between the two range number by using ‘for’ loop…

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a,b,c=0,d,e;
  6. clrscr();
  7. printf(“Enter the to find sum of even number between two range number”);
  8. scanf(“%d%d”, &a, &b);
  9. for(c=a;c<=b;c++)
  10. if(c%2==0)
  11. {
  12. d+=c;
  13. }
  14. printf(“%d”, d);
  15. getch();
  16. }

Inputs..

If a=1 and b=100 then

Results..

Sum= 2550..

Sum of even number between the two range number by using ‘while’ loop…

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a,b,c=0,d,e;
  6. clrscr();
  7. printf(“Enter the to find sum of even number between two range number”);
  8. scanf(“%d%d”, &a, &b);
  9. c=a;
  10. while(c<=b)
  11. if(c%2==0)
  12. {
  13. d+=c;
  14. c++;
  15. }
  16. printf(“%d”, d);
  17. getch();
  18. }

Inputs..

If a=1 and b=100 then

Results..

Sum= 2550..

Sum of even number between the two range number by using ‘do while’ loop…

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a,b,c=0,d,e;
  6. clrscr();
  7. printf(“Enter the to find sum of even number between two range number”);
  8. scanf(“%d%d”, &a, &b);
  9. c=a;
  10. do
  11. {
  12. if(c%2==0)
  13. {
  14. d+=c;
  15. c++;
  16. }
  17. }
  18. while(c<=b);
  19. printf(“%d”, d);
  20. getch();
  21. }

Inputs..

If a=1 and b=100 then

Results..

Design a site like this with WordPress.com
Get started