This program illustrate how to get sum and average of any digit.
Output:
Enter number of any digit = 1234
Entered digit = 1234
Reverse digit = 4321
Sum = 10
Average = 2.500
#include <stdio.h>
#include <conio.h>
void main()
{
int num, temp, sum = 0, rem, i, rev = 0, count = 0;
float avg;
printf("Enter number of any digit : ");
scanf("%d", &num);
temp = num;
for(i = 0; temp > 0; i++)
{
//take reminder or take last digit
rem = temp % 10;
//add each number
sum += rem;
// take in reverse
rev = rev * 10 + rem;
//remove last digit
temp = temp / 10;
count++;
}
avg = (float) sum / count;
printf("Entered digit = %d", num);
printf("Reverse digit = %d", rev);
printf("Sum = %d", sum);
printf("Average = %.3f", avg);
getch();
}
#include <conio.h>
void main()
{
int num, temp, sum = 0, rem, i, rev = 0, count = 0;
float avg;
printf("Enter number of any digit : ");
scanf("%d", &num);
temp = num;
for(i = 0; temp > 0; i++)
{
//take reminder or take last digit
rem = temp % 10;
//add each number
sum += rem;
// take in reverse
rev = rev * 10 + rem;
//remove last digit
temp = temp / 10;
count++;
}
avg = (float) sum / count;
printf("Entered digit = %d", num);
printf("Reverse digit = %d", rev);
printf("Sum = %d", sum);
printf("Average = %.3f", avg);
getch();
}
Output:
Enter number of any digit = 1234
Entered digit = 1234
Reverse digit = 4321
Sum = 10
Average = 2.500
No comments:
Post a Comment