1. Write a program that calculates and prints the average of several integers. A
ID: 3544755 • Letter: 1
Question
1. Write a program that calculates and prints the average of several integers. Assume the last value read with scanf is the sentinel 9999. A typical input sequence might be
10 8 11 7 9 9999
Indicating that the average of all the values preceding 9999 is to be calculated.
2. Write a function integerPower ( base, exponent) that returns the value of
base^(exponent)
For example, integerPower( 3,4) = 3*3*3*3. Assume that exponent is a positive, nonzero
integer, and base is an integer. Function integerPower should use
Explanation / Answer
/*******************first program********************/
#include<stdio.h>
int main()
{
int n,count=0;float avg=0;
printf("enter the numbers : ");
while(1)
{
scanf("%d",&n);
if(n==9999)
break;
avg=avg+n;
count++;
}
printf("the average is : %f",avg/count);
return 0;
}
/****************************************************/
/***************second program***********************/
#include<stdio.h>
int integerPower(int base,int exp)
{int val=base,i;
for(i=1;i<exp;i++)
val=val*base;
return val;
}
int main()
{
int base,exp,result;
printf("enter the base value :");
scanf("%d",&base);
printf("enter the exponent value :");
scanf("%d",&exp);
result=integerPower(base,exp);
printf(" the result is : %d ",result);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.