Write a C-program to print sum of even or odd numbers based on the n value using single loop?

Write a C-program to print sum of even or odd numbers based on the n value using single loop


#include <stdio.h>
int main() {
    int x,n,sum=0;
    printf("Enter n value ");
    scanf("%d",&n);
    for(x=1;x<=n;x++)
    {
        if(x%2==0 && n%2==0)
        {
            sum=sum+x;
        }
        else if(x%2!=0 && n%2!=0)
        {
            sum=sum+x;
        }
    }
    printf("%d sum is %d",n ,sum);
  
    return 0;
}

Output:

Enter n value 5

5 sum is 9

Enter n value 6
6 sum is 12




More Programs


52 . Write a C-program to print sum of even or odd numbers based on the n value using single loop
53 . Write a C-program to display table using for loop
54 . Write a C-program to print a to z
55 . Write a C-program to print ASCII values of a to z
56 . Write a C-program to print ASCII values of A to Z
57 . Write a C-program to print ASCII values of Numbers
58 . Write a C-program to print 1 to 10 values using while loop
59 . Write a C-program to print 10 to 1 values using while loop
60 . Write a C-program to print 1 to n values using while loop
61 . Write a C-program to print even numbers using while loop
62 . Write a C-program to print even number or odd number based on the n value using while loop