Blogger templates

Pages

Sunday, 20 May 2012

Write a C Program to Find the Sum of Odd Numbers and Even Numbers from 1 to N by using if and for Statement

Sum of Odd and Even
Data types:
  • Data Type defines what Type of value you can store in a Variable.
  • Primary Data Types: int ,float and char.
Float:
  • Format Specifier = %f
  • Keyword = float
  • Space = 4 byte
  • Range = 3.4 * 10^(38) to -3.4 * 10^(38)
  • Decimal values are allowed. 
Statement of C Program: Enter a Number ( N ) and Calculate the Sum of Odd and Even Numbers From 1 to N :
 
#include<stdio.h>
#include<conio.h>
void main()
{
int i, N ;
oddsum=0;                                  
evensum=0;
clrscr();
printf(" Enter the value of N\n");
scanf("%d" , &N);
for( i=1 ; i<=N ; i++ )
{
if(i%2 ==0)
{
evensum=evensum+ i;
}
else
{
oddsum=oddsum+i;
}
}
printf(" Sum of Odd Numbers = %d" , oddsum);
printf(" Sum of Even Numbers = %d" , evensum);
getch()
}

Output:
Enter the value of N
10
Sum of Odd Numbers = 25
Sum of Even Numbers = 30

0 comments:

Post a Comment