Pages

Tuesday, October 23, 2012

How can i find the sum of all odd number between a range.

#A program that finds the sum of all odd numbers between M to N.

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. long long int i,m,n,sum;
  6. clrscr();
  7. FILE *p;
  8. p=fopen("file5.txt","r");
  9. fscanf(p,"%lld %lld",&m,&n);
  10. for(i=m;i<=n;i++){
  11. if(i%2==1)
  12. printf(" %lld ",i);}
  13. sum=0;
  14. for(i=m;i<=n;i++){
  15. if(i%2==1)
  16. sum=sum+i;}
  17. printf("\n\nSum of ODD numbers=%lld\n",sum);
  18. getch();
  19. }

// in Notepade  wirte m=10 and n=100 then save as file5.txt//

 RULES & output:
1.Program type:used by for loop and if.
2.Simple input:10-100
3.Simple output:11 13 15 17 19 21 23 ………………99.
 Sum=2475.

No comments:

Post a Comment