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

How to Fight Overthinking or Negative Thoughts?

How to Combat Overthinking or Negative Thoughts? 1. Don’t Fight Your Thoughts Trying to suppress or control negative thoughts often backfire...