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

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


#include <stdio.h>
int main() {
    int x=1,n,sum=0;
    printf("Enter n value ");
    scanf("%d",&n);
    do
    {
       
        if(x%2==0 && n%2==0)
        {
            sum=sum+x;
        }
        else if(x%2!=0 && n%2!=0)
        {
            sum=sum+x;
        }
        x++;
    }while(x<=n);
   
    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


90 . Write a C-program to print sum of even or odd numbers based on the n value using single do while loop
91 . Write a C-program to display table using do while loop
92 . Write a C-program to print a to z using do while loop
93 . Write a C-program to print ASCII values of a to z using do while loop
94 . Write a C-program to print ASCII values of A to Z using do while loop
95 . Write a C-program to print ASCII values of Numbers using do while loop