Users Online
· Guests Online: 31
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
02 C Program to Calculate the Sum of Odd & Even Numbers
C Program to Calculate the Sum of Odd & Even Numbers
This C Program calculates the sum of odd & even numbers. The program first seperates odd and even numbers. Later it adds the odd and even numbers seperately.
Here is source code of the C program to calculate the sum of odd & even numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the sum of odd and even numbers from 1 to N
*/
#include <stdio.h>
void main()
{
int i, num, odd_sum = 0, even_sum = 0;
printf("Enter the value of num\n");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of all odd numbers = %d\n", odd_sum);
printf("Sum of all even numbers = %d\n", even_sum);
}
$ cc pgm12.c
$ a.out
Enter the value of num
10
Sum of all odd numbers = 25
Sum of all even numbers = 30
$ a.out
Enter the value of num
100
Sum of all odd numbers = 2500
Sum of all even numbers = 2550
This C Program calculates the sum of odd & even numbers. The program first seperates odd and even numbers. Later it adds the odd and even numbers seperately.
Here is source code of the C program to calculate the sum of odd & even numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the sum of odd and even numbers from 1 to N
*/
#include <stdio.h>
void main()
{
int i, num, odd_sum = 0, even_sum = 0;
printf("Enter the value of num\n");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of all odd numbers = %d\n", odd_sum);
printf("Sum of all even numbers = %d\n", even_sum);
}
$ cc pgm12.c
$ a.out
Enter the value of num
10
Sum of all odd numbers = 25
Sum of all even numbers = 30
$ a.out
Enter the value of num
100
Sum of all odd numbers = 2500
Sum of all even numbers = 2550
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.