Sunday 26 August 2018

C Program for Sum of Odd and Even Number Separately From 0 to Given Value Using For Loop & If and Else Function

C Program for Sum of Odd and Even Number Separately   From 0 to Given Value Using For Loop & If and Else Function 


#include <stdio.h>

main()

{
int num,i,n,sumodd=0,sumevv=0; /*Declaration of variables and some variables like sumodd & sumevv are also declared and initialised with the value of zero */
printf("Enter A Value for Loop:\n"); /*Need the number or the value of the number of loops in for loop*/
scanf("%d",&n); 
printf("Enter Values to be Sum\n"); /*Enter those numbers that will be added to the for loop at a later time*/
for(i=1;i<=n;i++)
{
num=i; /* provided numbers*/
if(num%2==0) 

/*If else condition for detecting odd and even number*/

/*Even number divided by 2 the remainder will be zero so the even number will go in the IF condition the odd number divided by 2 the remainder will not be zero so the odd number will go for ELSE condition*/

{
if(num>0) /* IF ELSE for Only positive or greater than zero integer will be able for addition less than zero or negative valid integer will be rejected */
{
sumevv+=num; //sumevv=sumevv+num;
}
else
{
continue;
}
}
else
{
if(num>0) /* IF ELSE for Only positive or greater than zero integer will be able for addition less than zero or negative valid integer will be rejected */
{
sumodd+=num; //sumodd=sumodd+num;
}
else
{
continue;
}
}
}
printf("\nODD SUM = %d",sumodd); /*For printing the sum of odd numbers*/ 
printf("\nEVEN SUM = %d",sumevv); /*
For printing the sum of even numbers*/

}