My Subject Blog

C Program to Find Sum and Average of First N Number using Function

/*
Write a Program using function to Add 1st n numbers and also Find out Average of 1st n numbers. 
(Where n is enter through Keyboard.) 
*/

#include <stdio.h>
int getsum(int );
float getavg(int );

int main(void) 
{
int n;
printf("Enter Any numner n = ");
scanf("%d",&n);
printf("\n SUM = %d",getsum(n));
printf("\n AVG = %f",getavg(n));
return 0;
}

int getsum(int n)
{
return ((n*(n+1))/2);
}

float getavg(int n)
{
return (((float)n*(n+1))/2)/n;
}

Comments

Most Visited Post

C Program to Find Largest Odd Number from Array

Followers