C program to find the sum of odd numbers and sum of even numbers from 1 to N. Output the computed sums on two different lines with suitable headings

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* C program to find the sum of odd numbers and sum of even numbers from 1 to N. Output the computed sums on two different lines with suitable headings */

#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 all odd numbers  = %d\n”, oddSum);
printf (“Sum of all even numbers = %d\n”, evenSum);
}
/*—————————–
Output
RUN1

Enter the value of N
10
Sum of all odd numbers  = 25
Sum of all even numbers = 30

RUN2
Enter the value of N
50
Sum of all odd numbers  = 625
Sum of all even numbers = 650

——————————*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Write a C program to find the sum of ‘N’ natural numbers
  2. C program to check whether a given integer is odd or even
  3. Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..
  4. C program to find the biggest of three numbers
  5. Sample – Program to enter an integer and find out if it is even or odd.

Leave a Comment

Previous post:

Next post: