Problem Description
Write a 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
(Inclusive of both positive and negative numbers)
Input format:
Input consists of n+1 integers. The first integer corresponds to n and the next n integers correspond to the numbers to be added. Consider 0 to be a positive number
Example 1:
Input: 10
Output:
Odd=25
(1+3+5+7+9) = 25
Even=30
(2+4+6+8+10)=30
If the input number is odd then the user should throw the message as "Wrong Input"
Example 2:
Input : 11
Output=Wrong InputTest Case 1
Input (stdin)10
Expected OutputSum of all odd numbers=25 Sum of all even numbers=30
Test Case 2
Input (stdin)19
Expected OutputWrong Input
Program
#include <stdio.h> int main() { int n,t=0,f=0; scanf("%d",&n); if(n%2!=0) { printf("Wrong Input"); } else { n=n+1; while(n--) { if(n%2==0) { t=t+n; } else { f=f+n; } } printf("Sum of all odd numbers=%d\n",f); printf("Sum of all even numbers=%d",t); } return 0; }
No comments:
Post a Comment
Note: only a member of this blog may post a comment.