Write a program in C to display the n terms of nth natural number, even && odd natural number and their sum



QUESTION:- Write a program in C to display the n terms of nth natural number, even && odd natural number, and their sum 


#include <stdio.h> 

void main() 


    int a, b,c=0, even=0, odd=0

    printf("enter the value of a: "); 

    scanf("%d", &a); 

    for(b = 1; b<= a; b++) 

    { 

        c = c+b; 

        if(b%2==0

        { 

            even = even + b; 

        } 

        else  

        { 

            odd = odd +  b; 

        } 

    } 

    printf("the sum of first %d natural number is: %d \n",a, c); 

    printf("the sum of the %d number's odd number is: %d\n",a, odd); 

    printf("the sum of the %d number's even number is: %d\n",a, even); 



OUTPUT

enter the value of a: 4

the sum of the first 4 natural number is: 10 

the sum of the 4 number's odd number is: 4

the sum of the 4 number's even number is: 6


Follow our page for regular updates.

0 Comments