write a c program to print sum of all odd number and all even number in the range of 'n' natural number
Answers
Answered by
1
Attachments:
Answered by
1
Answer:
1)The sum of all even numbers
#include <stdio.h>
int main()
{
int a,n,sum;
/* it is the range of naturalnumbers*/
printf("enter the range ");
scanf("%d",&n);
for(a=1;a<=n;a++)
{
if(a%2==0)
{
sum=sum+a;
}
}
printf("the sum of even=%d",sum);
return 0;
}
Output:-
Enter the range 6
the sum of even 12
2)The sum of all odd numbers
#include <stdio.h>
int main()
{
int a,n,sum;
/* it is the range of naturalnumbers*/
printf("enter the range ");
scanf("%d",&n);
for(a=1;a<=n;a++)
{
if(a%2!=0)
{
sum=sum+a;
}
}
printf("the sum of odd=%d",sum);
return 0;
}
Output:-
enter the range 6
the sum of odd 9
Similar questions