Sunday, November 1, 2020

C program of 1 to n sum, average, even sum & average, odd sum & average

 #include <stdio.h>

int main()

{

    int i,n,even=0,odd=0,sum=0,counteven=0,countodd=0,countsum=0;

    printf("Enter a number:");

    scanf("%d",&n);

    for(i=1;i<=n;i++)

    {

        printf("%d\n",i);

        sum+=i;

        countsum++;

        if(i%2==0)

        {

            even+=i;

            counteven++;

        }

        else

        {

            odd+=i;

            countodd++;

        }

    }

    printf("Sum=%d\n",sum);

    printf("Average=%d\n",sum/countsum);

    printf("Even Sum=%d\n",even);

    printf("Even Average=%d\n",even/counteven);

    printf("Odd Sum=%d\n",odd);

    printf("Odd Average=%d",odd/countodd);

    return 0;

}


No comments:

Post a Comment

Everyone Is Fighting a Battle You Can't See ❤️ | A Simple Reminder to Be Kind

 Hi, namaste Krishna. So today we will be talking about that everyone is going through something in their life. Be it you, be it your friend...