Problem 21

author
By Supreme2023-03-15
Problem Statement :
Write a C program to find the sum and average of different numbers which are accepted by user as many as user wants
Code:

#include <stdio.h>

int main(){

//21. Write a C program to find the sum and average of different numbers //which are accepted by user as many as user wants

printf("\tSum & Average Of Given Numbers");
printf("\n=========================================");

int value,sum=0,no,i;
float avg;

printf("\n\nHow Many Numbers You Want To Enter : ");
scanf("%d",&no);

while(i<=no){
printf("\nEnter The Value [%d] : ", i);
scanf("%d",&value);
sum = sum + value;
i++;
}

avg = (sum/no);

printf("\n\n______________________________________________");
printf("\n\nSum Of These Numbers Is : %d",sum);
printf("\n\nAverage Of These Numbers Is : %.2f",avg);
printf("\n______________________________________________\n\n");

return 0;
}

Description :
  • The program is designed to find the sum and average of different numbers which are entered by the user as many as the user wants.
  • The program includes the standard input/output library <stdio.h>.
  • The main function is defined with a return type of integer.
  • An integer variable 'value' and a float variable 'avg' are declared.
  • The variables 'sum' and 'i' are initialized to zero.
  • The program prompts the user to enter how many numbers they want to input.
  • The user input is stored in the 'no' variable.
  • The program uses a while loop to accept values from the user and calculate the sum of the entered values.
  • The program also calculates the average of the entered values.
  • The output displays the sum and average of the entered values.
  • The program returns an integer value of 0 to the operating system indicating successful execution.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.