Program to Print the Value from 1 to 10, and Display the Addition of Odd & Even Numbers
Write a program to print the value from 1 to 10, and display addition of odd numbers & even numbers?
Answers
Answered by
0
first start with preprocessor directive
//program for printing 1 to 10 numbers as well as even sum and odd sum
#include<stdio.h>
void main()
{
int i,n,evensum=0,oddsum=0;
clrscr(); /*clears the screen*/
for(i=1;i<=10;i++)
{
printf("%d\n",i); /*prints 1 to 10 numbers*/
if(i%2==0) /*condition to check even*/
{
evensum=(evensum+i); /*even numbers are added here } and kept in the variable evensum*/
else
{
oddsum=(oddsum+i); /*odd numbers are added here and kept } in the variable oddsum*/
} /*end of for loop used for repetition*/
printf("even numbers sum in 1 to 10 is%d\n",evensum);
printf("odd numbers sum in 1 to 10 is %d",oddsum);
getch(); /*shows the output*/
}
output of the above program is
1
2
3
4
5
6
7
8
9
10
even numbers sum in 1 to 10 is 30
odd numbers sum in 1 to 10 is 25
//program for printing 1 to 10 numbers as well as even sum and odd sum
#include<stdio.h>
void main()
{
int i,n,evensum=0,oddsum=0;
clrscr(); /*clears the screen*/
for(i=1;i<=10;i++)
{
printf("%d\n",i); /*prints 1 to 10 numbers*/
if(i%2==0) /*condition to check even*/
{
evensum=(evensum+i); /*even numbers are added here } and kept in the variable evensum*/
else
{
oddsum=(oddsum+i); /*odd numbers are added here and kept } in the variable oddsum*/
} /*end of for loop used for repetition*/
printf("even numbers sum in 1 to 10 is%d\n",evensum);
printf("odd numbers sum in 1 to 10 is %d",oddsum);
getch(); /*shows the output*/
}
output of the above program is
1
2
3
4
5
6
7
8
9
10
even numbers sum in 1 to 10 is 30
odd numbers sum in 1 to 10 is 25
Similar questions
Biology,
8 months ago
Biology,
8 months ago
Psychology,
1 year ago
English,
1 year ago
History,
1 year ago