Thursday 2 February 2012

Write a program in C to generate even & odd number between 1 to any number and their sum.


//program to generate even & odd number between 1 to any number and their sum
main()
{
int a,b,sum=0,sum1=0;
printf("Enter any number upto when you want to generate even and odd number and their sum ");
scanf("%d",&a);
printf("The even numbers between 1 and %d is given below:- \n",a);
for(b=2;b<=a;b+=2)
{
printf("%d ",b);
sum=sum+b;
}
printf("\nThe odd numbers between 1 and %d is given below:- \n",a);
for(b=1;b<=a;b+=2)
{
printf("%d ",b);
sum1=sum1+b;
}
printf("\nThe sum of all even numbers is %d\nThe sum of all odd numbers is %d",sum,sum1);
printf("\nThe sum of all even and odd numbers is %d",sum+sum1);
getch();
}
Output
Enter any number upto when you want to generate even and odd number and their sum 50
The even numbers between 1 and %d is given below:-
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
The odd numbers between 1 and %d is given below:-
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
The sum of all even numbers is 650
The sum of all odd numbers is 625
         The sum of all even and odd numbers is 1275

No comments:

Post a Comment