/ Undifined / 14 - WAP to print sum of even and odd numbers from 1 to N numbers.

14 - WAP to print sum of even and odd numbers from 1 to N numbers.

WAP to print sum of even and odd numbers from 1 to N numbers. 
#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);
}
Output 
Enter the value of num
10
Sum of all odd numbers  = 25
Sum of all even numbers = 30

about author

Blogger Sens it website about blogger templates and blogger widgets you can find us on social media
Previous Post :Go to tne previous Post
Next Post:Go to tne Next Post

No comments:

Post a Comment