Sum of odd number & between two range in C language..

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

Let’s understand some concept.

ODD Number..

Number which are not divided by 2 with 1 or 2 remainder is known as odd number.

Sum of odd 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. OR if last term is odd then a={(n+1)/2}2.

Some important symbols used in code.

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

Sum of odd number by using formula

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

Inputs..

If a=100 then

Results..

Sum= 2500.

Sum of odd number by using ‘for’ loop..

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

Inputs..

If a=100 then

Results..

Sum= 2500

Sum of odd number by using ‘while’ loop..

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

Inputs..

If a=100 then

Results..

Sum= 2500

Sum of odd number by using ‘ do while’ loop..

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

Inputs..

If a=100 then

Results..

Sum= 2500

Sum of odd number between two range number by using ‘for’ loop..

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

Inputs..

If a=10, b=100 then

Results..

Sum= 2475..

Sum of odd number between two range number by using ‘while’ loop..

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

Inputs..

If a=10, b=100 then

Results..

Sum= 2475..

Sum of odd number between two range number by using ‘do while’ loop..

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

Inputs..

If a=10, b=100 then

Results..

Sum= 2475..

Design a site like this with WordPress.com
Get started