How do I subtract multiple inputted numbers in a loop? - c

The program should collect inputted numbers from the user through a loop (10 maximum numbers) and breaks if the user enters a negative number. After collecting input, it should calculate the sum, difference, product, and quotient. My problem is that I do not know the right way to find the difference, I know this since the answer from the calculator is different from the program's.
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0, difference = 0, product = 1, quotient = 1;
for (i = 1; i <= 10; ++i) {
printf("Enter n%d: ", i);
scanf("%lf", &number);
// if the user enters a negative number, break the loop
if (number < 0.0) {
break;
}
sum += number; // sum = sum + number;
difference = number;
product *= number;
quotient = number / quotient;
}
printf("Sum = %.2lf", sum);
printf("\nDifference = %.2lf", difference);
printf("\nProduct = %.2lf", product);
printf("\nQuotient = %.2lf", quotient);
return 0;
}

double sum=0, difference=0, product =1 , quotient=1;
for (i = 1; i <= 10; ++i) {
printf("Enter n%d: ", i);
scanf("%lf", &number);
if(number<0){
break;
}
sum += number;
difference -= number;
product *= number;
quotient = number/quotient;
}
I am supposing you have no knowledge of array yet.

Related

Find the maximum, minimum, and average values in the array

In my code, the program will not allowed the negative number entered, the program will stop reading, then calculate the maximum value, minimum value and average value.
That is my code
#include <stdio.h>
int main(void) {
int age[10] = {0}; // initalized an array
printf("Please enter ages: \n"); // allow user to enter numbers
for (int i = 0 ;i < 10; i++) {
scanf("%d",&age[i]);
if (age[i] < 0) { // if it is negative number, it is should stop reading
break;
}
else if (age[i] >= 0) {
continue;
}
}
int maximum = age[0];
int minimum = age[0];
float average = 0.0;
int length = sizeof(age) / sizeof(age[0]);
for (int j = 0; j < length; j++) {
if (maximum < age[j]) {
maximum = age[j];
}
else if (minimum > age[j]) {
minimum = age[j];
}
average += age[j];
}
average = average / length;
printf("%d\n", maximum);
printf("%d\n", minimum);
printf("%.1f\n", average);
return 0;
}
Please enter ages: 5 -1
expected result: max:5;min:5,average:5;
actual result: max:5;min:-1,average: 0.4;
That was a question that I met, the code should not accept any negative value.
Thank you all.
but if I add age[i] = 0; then break;
The average value will equal to 0.5.
You don't need an array.
You don't need both a loop variable and a length.
It's more appropriate to use ? : for updating minimum/maximum.
You don't need two loops
You need to check the int return value of scanf(), which indicates the number of items successfully scanned, so it should be 1. I'll leave that for you/OP to add (hint: replace for-loop by while-loop to avoid having to add a separate length variable again).
int main(void)
{
printf("Please enter ages: \n");
int minimum = INT_MAX;
int maximum = 0;
int sum = 0;
int count = 0;
for (count = 0; count < 10; count++)
{
int age;
scanf("%d", &age);
if (age < 0)
{
break;
}
sum += age;
minimum = (age < minimum) ? age : minimum;
maximum = (age > maximum) ? age : maximum;
}
if (count > 0)
{
printf("Min: %d\n", minimum);
printf("Max: %d\n", maximum);
printf("Avg: %.1f\n", (float)sum / count);
}
else
{
printf("You didn't enter (valid) age(s).\n");
}
return 0;
}
Your approach is overly complicated and wrong.
You want this:
...
int length = 0; // declare length here and initialize to 0
for (int i = 0; i < sizeof(age) / sizeof(age[0]); i++) {
scanf("%d", &age[i]);
if (age[i] < 0) // if it is negative number, it is should stop reading
break;
length++; // one more valid number
}
// now length contains the number of numbers entered
// the rest of your code seems correct
You also might need to handle the special case where no numbers are entered, e.g: the only thing entered is -1. It doesn'make sense to calculate the average or the largest/smallest number when there are no numbers.
A possible solution could be:
(corrections are written in the commented code)
#include <stdio.h>
int main(void){
int arraySize = 10;
int age[arraySize]; //initialize not required
//the number of existing values inside the array (effective length)
int length = 0;
printf("Please enter ages: \n"); // allow user to enter numbers
for(int i=0; i<arraySize; i++){
scanf("%d",&age[i]);
// if it is negative number, it is should stop reading
if(age[i]<0){ break; }
//the else-if is not required
//but, if the compiler goes here,
//it means that the value is acceptable, so
length++;
}
int maximum = age[0];
int minimum = age[0];
float average = 0.0;
for(int j=0; j<length; j++){
if(maximum<age[j]){ maximum = age[j]; }
else if(minimum>age[j]) { minimum = age[j]; }
average += age[j];
}
average = average / length;
printf("%d\n", maximum);
printf("%d\n", minimum);
printf("%.1f\n", average);
return 0;
}
OP's primary problem is the 2nd loop iterates 10 times and not i times (the number of times a non-negative was entered.
For fun, let us try a non-floating point solution as it really is an integer problem.
An array to store values is not needed.
#include <limits.h>
#include <stdio.h>
int main(void) {
// Keep track of 4 things
int min = INT_MAX; // Set min to the max int value.
int max = INT_MIN;
long long sum = 0; // Use wide type to cope with sum of extreme ages.
int count = 0;
#define INPUT_N 10
printf("Please enter ages: \n");
for (count = 0; count < INPUT_N; count++) {
int age;
if (scanf("%d", &age) != 1) {
fprintf(stderr, "Missing numeric input.");
return EXIT_FAILURE;
}
if (age < 0) {
break;
}
if (age < min) min = age;
if (age > max) max = age;
sum += age;
}
if (count == 0) {
fprintf(stderr, "No input.");
return EXIT_FAILURE;
}
printf("Maximum: %d\n", max);
printf("Minimum: %d\n", min);
// Could use FP and
// printf("Average: %.1f\n", 1.0 *sum / count);
// But for fun, how about a non-FP approach?
#define SCALE 10
#define SCALE_LOG 1
sum *= SCALE; // Scale by 10 since we want 1 decimal place.
// Perform a rounded divide by `count`
long long average_scaled = (sum + count/2) / count;
// Print the whole and fraction parts
printf("Average: %lld.%.*lld\n",
average_scaled / SCALE, SCALE_LOG, average_scaled % SCALE);
return 0;
}
First of all, you must record how many positive numbers you enter. Then the value of length will be correct.
Second, for the second for loop, j must be smaller than the number of positive ages. Therefore, you won't add negative age[j] to average.
You can simply modify the second for loop.
#include <stdio.h>
int main(void) {
int age[10] = {0}; // initalized an array
printf("Please enter ages: \n"); // allow user to enter numbers
int length = 0;
for (int i = 0 ;i < 10; i++) {
scanf("%d",&age[i]);
if (age[i] < 0) { // if it is negative number, it is should stop reading
break;
}
else if (age[i] >= 0) {
length++;
continue;
}
}
int maximum = age[0];
int minimum = age[0];
float average = 0.0;
for (int j = 0; j < length; j++) {
if (maximum < age[j]) {
maximum = age[j];
}
else if (minimum > age[j]) {
minimum = age[j];
}
if ( age[j] > 0.0 )
{
average += age[j];
}
}
average = average / length;
printf("%d\n", maximum);
printf("%d\n", minimum);
printf("%.1f\n", average);
return 0;
}

My calculation shows wrong when enter large number?

#include <stdio.h>
main()
{
int choice, no;
printf("1. Show sum of odd/even number to N term\n");
printf("2. Smallest, largest and average of the supplied numbers\n");
printf("3. Terminate the programs\n\n");
printf("Enter your choice[1|2|3]: ");
scanf("%d", &choice);
if (choice == 1)
{
int i , no , sum = 0, j, sum2 = 0;
printf("\nEnter any number: ");
scanf("%d", &no);
for (i = 2; i <= no; i = i + 2)
{
sum = sum + i;
}
printf("\nSum of all even number between 1 to %d = %d\n", no, sum);
for (j = 1; j <= no; j = j + 2)
{
sum2 = sum2 + j;
}
printf("Sum of all odd number between 1 to %d = %d\n", no, sum2);
}
else if(choice == 2)
{
float max, min, avg, num,counter=0, sum = 1;
printf("\nPlease enter all the number you want![0 to end]: ");
scanf("%f", &num);
max = min = num;
while (num != 0)
{
printf("Please enter all the number you want![0 to end]: ");
scanf("%f", &num);
if (max < num && num > 0)
max = num;
else if (min > num && num > 0)
min = num;
sum = sum + num;
counter++;
}
printf("\nThe smallest and largest of entered numbers are %.2f and %.2f respectively.\n", min, max);
avg = sum / counter;
printf("The sum of entered number is %.2f\n", sum);
printf("The average of entered number is %.2f\n", avg);
}
}
My problem is when i choose number 2 it will show smallest and largest number but the sum show wrongly when i enter large number like 200! But it work fine when i enter small value!?
small number
Big number
picture included
Your sum has never count the first input. With initial value sum = 1,
For your small numbers: your sum = (1 + 1 + 1 + 2) happens to be right.
But for your big numbers: your sum = (1 + 100 + 100 + 200 ) = 400.1 (you can see you missed the first input 100);
Your mistakes:
sum should be initialized as 0;
you did not count the first input (before loop): not calc sum nor counter++
when user finally input 0, you should not continue counter++ because '0' is not a valid input.
Your program has several issues:
You initialise the sum to 1, not to 0 as it ought to be.
You handle the first value and subsequent values differently. This is basically okay, but make sure that the treatment is the same in bothz cases. In your code, you assign min and max for the first value correctly, but miss incrementing the sum and the counter.
Your code doesn't check whethet a valid float has been entered. That means that it will hang if the user enters something that isn't a float. Your program should handle such input as if it were zero.
In theory, you should not divide by counter when it is zero. In practice, that doesn't happen, because you also account for the terminating zero in your counter.
Perhaps it would be better to treat the first value like all other values. You could then either initialise min and max to big and small values (for example ±FLT_MAX from <float.h>) or you could check count == 0 inside the loop to implement diferent behaviour for the first and the following values.
In that case, you could break out of an infinite loop when invalid input or zero was given. This might seem complicated, but leads to simpler code:
#include <stdio.h>
#include <float.h>
int main(void)
{
float max = -FLT_MAX; // minimum possible float value
float min = FLT_MAX; // maximum possible float value
float sum = 0.0f;
int count = 0;
for (;;) {
float num;
printf("Please enter all the number you want![0 to end]: ");
if (scanf("%f", &num) < 1 || num == 0) break;
if (max < num) max = num;
if (min > num) min = num;
sum += num;
count++;
}
if (count) {
float avg = sum / count;
printf("%d values\n", count);
printf("Smallest: %.2f\n", min);
printf("Largest: %.2f\n", max);
printf("Sum: %.2f\n", sum);
printf("Average: %.2f\n", avg);
}
return 0;
}
#include <stdio.h>
main()
{
int choice = 0;
for (;choice != 3;)
{
printf("_____________________________________________________________\n\n");
printf("1. Show sum of odd/even number to N term\n");
printf("2. Smallest, largest and average of the supplied numbers\n");
printf("3. Terminate the programs\n\n");
printf("Enter your choice[1|2|3]: ");
scanf("%d", &choice);
printf("_____________________________________________________________\n\n");
if (choice == 1)
{
int i, no, sumc1 = 0, j, sum2c1 = 0;
printf("\nEnter any number: ");
scanf("%d", &no);
for (i = 2; i <= no; i = i + 2)
{
sumc1 = sumc1 + i;
}
printf("\nSum of all even number between 1 to %d = %d\n", no, sumc1);
for (j = 1; j <= no; j = j + 2)
{
sum2c1 = sum2c1 + j;
}
printf("Sum of all odd number between 1 to %d = %d\n\n\n", no, sum2c1);
}
else if (choice == 2)
{
float counter, num, large, small, num2, sum = 0, avg;
printf("\nEnter first number[Enter 0 to stop]: ");
scanf("%f", &num);
num2 = num;
large = num;
small = num;
for (counter = 0; num != 0; counter++)
{
printf("Enter another number [Enter 0 to stop]: ");
scanf("%f", &num);
if (num > large && num > 0)
large = num;
if (num<small && num > 0)
small = num;
sum = sum + num;
}
sum = sum + num2;
avg = sum / counter;
printf("\nThe largest number is %.2f\n", large);
printf("The smallest number is %.2f\n", small);
printf("The sum of entered numbers are %.2f\n", sum);
printf("The average of entered number are %.2f\n\n\n", avg);
}
}
}
I just figured out the main problem thanks to all, although some of replies gave me full code, i have to use just simple code because i only started to learn the basic. thanks.
this code might be useful for someone.
//uniten.encik//

C language- How to print out the largest number inputted into an array?

I want to be able to print out the largest age inputted by the user as well as the smallest age.
Also, I noticed my program doesn't include the numbers after the decimal point. It would just say 25.00 for example, rather than 25.25.
Any help is greatly appreciated!
#include "stdafx.h"
#include "stdio.h"
void main()
{
int age[11];
float total = 0;
int i = 1;
float average;
do
{
printf("# %d: ", i);
scanf_s("%d", &age[i]);
total = (total + age[i]);
i = i + 1;
} while (i <= 10);
average = (total / 10);
printf("Average = %.2f", average);
}
I think it will be helpful.
For decimal point you have to declare your array as float.
FLT_MAX These macros define maximum value of a float. Before useing FLT_MAX you should inclue float.h header file.
#include <stdio.h>
#include <float.h>
int main(void)
{
float age[11];
float total = 0;
int i = 1;
float average;
float largestInput = 0.0;
float smallestInput = FLT_MAX;
do
{
printf("# %d: ", i);
scanf("%f", &age[i]);
total = total + age[i];
//here i am checking the largest input
if (age[i]> largestInput){
largestInput = age[i];
}
//here i am checking the smallest input
if (age[i] < smallestInput) {
smallestInput = age[i];
}
i = i + 1;
} while (i <= 10);
average = (total / 10);
printf("Average = %.2f\n", average);
printf("Largest Input Is = %.2f\n", largestInput);
printf("Smallest Input IS = %.2f", smallestInput);
return 0;
}
Pseudo-code. Keep two extra variables.
long largestInput, smalltestInput.
set
largestInput = smalltestInput = age[0];
for (i = 0; i < 10; i++) {
if age[i]> largestInput{
largestInput = age[i];
}
if age[i] < smallestInput {
smalltestInput = age[i];
}
}
Print it as you like
Your code works fine on my system. I've modified your code to not be Windows specific and shown you how to calculate the smallest and largest of the numbers entered. The idea is to constantly compare the current entry to the current smallest and largest number and change smallest and largest as needed. The code below also shows how to start at array index 0.
#include <stdio.h>
int main()
{
int age[10];
float total = 0;
int i = 0;
float average;
int large = -9999;
int small = 9999;
do
{
printf("# %d: ", i+1);
scanf("%d", &age[i]);
total = (total + age[i]);
if( age[i] < small )
{
small = age[i];
}
if( age[i] > large )
{
large = age[i];
}
i++;
} while (i < 10);
average = (total / 10.00);
printf("Smallest = %d Largest = %d \n", small, large);
printf("Average = %.2f\n", average);
}
# 1: 30
# 2: 40
# 3: 50
# 4: 2
# 5: 3
# 6: 4
# 7: 6
# 8: -1
# 9: 4
# 10: 5
Smallest = -1 Largest = 50
Average = 14.30

Sum of Digits Program not giving correct answer for negative number in c

printf("Sum Digit Program\n");
int n,re, sum = 0;
printf("Enter an integer n="); scanf("%d", &n);
while(n){
re = n % 10;
sum = sum + re;
n = n / 10;
}
printf("Sum digit = %d", sum);
return 0;
}
I try this and it works well with positive integer but when I enter a negative integer like: -323 => -8
It's supposed to be -3+2+3 =2 not -3-2-3=-8
I try using abs function but it still doesn't work right
OP almost had it. Simply treat MSDigit as signed. All other digits, use abs(rem). Works for INT_MIN
printf("Sum Digit Program\n");
int sum = 0;
printf("Enter an integer n=");
scanf("%d", &n);
while (n) {
int re = n % 10;
n = n / 10;
sum += n ? abs(re) : re; // At MSDigit when n==0
}
printf("Sum digit = %d", sum);
Well, you may use conditional operator to store the sign value like int sign = (n >= 0 ? 1 : -1); as shown below -
#include <stdio.h>
#include <stdlib.h>
/*
* #brief Logic for returning sum of digits
*/
int digi_sum(int n)
{
int sign = (n >= 0 ? 1 : -1);
int sum = 0;
n *= sign;
while (n)
{
if (n < 10)
sum += (sign * (n % 10));
else
sum += n % 10;
n /= 10;
printf("Sum: %d, n: %d\n", sum, n);
}
printf("sum: %d, n: %d\n", sum, n);
return sum;
}
/*
* #brief Driver function
*/
int main(int argc, char *argv[])
{
int num = -323;
printf("Sum: %d\n", digi_sum(num));
return 0;
}
The idea is to store the sign of the number into a separate variable and use it when n < 10.
Use n=abs(n/10) instead of n=n/10
#include <stdio.h>
#include <math.h>
main()
{
printf("Sum Digit Program\n");
int n,re, sum = 0;
printf("Enter an integer n="); scanf("%d", &n);
while(n)
{
re = n % 10;
sum = sum + re;
n =abs(n/10);
}
printf("Sum digit = %d", sum);
return 0;
}
You can add a condition to the first line of your loop to make sure that the sum so far is positive when n < 10, after that it will make the subtraction for the least digit if it has too. Then your loop should look like this:
while(n){
if (abs(n) < 10) {
sum = abs(sum);
}
re = n % 10;
sum = sum + re;
n = n / 10;
}
I think that you need a precondition for the first number.
with an if then else.
Solved
I changed the output to see the values
include<stdio.h>
int main(void)
{
int re,n;
int sum =0 ;
printf("Sum Digit Program \n");
printf("Enter an integer n= ");
scanf("%d", &n);
while(n)
{
if (abs(n) < 10) {
sum = abs(sum);
}
re= (n % 10);
sum = sum + re;
n= n / 10;
printf ("\n re = %d , n= %d \n", re, n);
}
printf ("\n sum= %d \n",sum);
return 0;
}

Average, max, and min program in C

So I'm coding in C, and I need to come up with code that will take n numbers from the user, and find their minimum, maximum, average, and sum of squares for for their values. So far I have the average and sum of squares portion, but the minimum and maximum is biting me.
Keep in mind I'm at a very rudimentary level, and I have not reached arrays yet. All I know are logical operators, functions, loops, and the use of the stdlib.h, math.h, and stdio.h libraries.
This is what I have so far.
The average function gave me a lot of problems when I tried to put float and double during compiling, so multiply it by a 1.0 fixed that. I have everything, just the minimum and maximum. I keep getting the last entry as my maximum, and a 0 for my minimum.
#include<stdio.h>
int main()
{
float average;
int i, n, count=0, sum=0, squaresum=0, num, min, max;
printf("Please enter the number of numbers you wish to evaluate\n");
scanf_s("%d",&n);
printf("Please enter %d numbers\n",n);
while(count<n)
{
min=0;
max=0;
if(num>max)
max=num;
if(num<min)
min=num;
scanf_s("%d",&num);
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
}
average = 1.0*sum/n;
printf("Your average is %.2f\n",average);
printf("The sum of your squares is %d\n",squaresum);
printf("Your maximum number is %d\n",max);
printf("Your minimum number is %d\n",min);
return(0);
}
Your algorithm is not quite right. Below is the correct implementation:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(void)
{
float average;
int n, num, count = 0, sum = 0, squaresum = 0;
int min = INT_MAX, max = INT_MIN;
bool gotAnswer = false;
/* Don't Let User Enter Wrong Input */
while(!gotAnswer)
{
printf("Please enter the number of numbers you wish to evaluate: ");
if(scanf_s("%d", &n) != 1)
{
/* User Entered Wrong Input; Clean Up stdin Stream*/
while(getchar() != '\n')
{
continue;
}
}
else
{
/* User Input Was Good */
gotAnswer = true;
}
}
/* Clear stdin Stream Just In Case */
while(getchar() != '\n')
continue;
while(count < n)
{
/* Don't Let User Enter Wrong Input */
gotAnswer = false;
printf("Enter number %d: ", count + 1);
if(scanf_s("%d", &num) != 1)
{
/* User Entered Wrong Input; Clean Up stdin Stream */
while(getchar() != '\n')
continue;
/* Let User Try Again */
continue;
}
else
{
/* User Input Was Correct */
gotAnswer = true;
/* Clear stdin Stream Just In Case */
while(getchar() != '\n')
continue;
}
if(num > max)
max = num;
if(num < min)
min = num;
sum += num;
squaresum += num * num;
count++;
}
average = 1.0 * sum / n;
printf("Your average is %.2f\n", average);
printf("The sum of your squares is %d\n", squaresum);
printf("Your maximum number is %d\n", max);
printf("Your minimum number is %d\n", min);
system("pause");
return 0;
}
I've added error checking and recovery. Please ask if you have any questions about the logic.
The way your code is currently written, min has to start out at a high value (not 0), or the code won't work. The best value to choose is the maximum possible value for an int.
You should also consider whether or not you want to reset these variable each time through the loop.
Enter the first num outside the loop and assign that to max min
scanf("%d",&num);
max = min = num;
Change your while loop to infinite loop
while(1) {...}
and now check for the condition that whether your counter count is equal to n is or not to break out from the infinite loop
if(count == n)
break;
Full code after modification:
#include<stdio.h>
int main()
{
float average;
int i, n, count=0, sum=0, squaresum=0, num, min, max;
printf("Please enter the number of numbers you wish to evaluate\n");
scanf_s("%d",&n);
printf("Please enter %d numbers\n",n);
scanf_s("%d",&num);
max = min = num;
while(1)
{
if(num>max)
max=num;
if(num<min)
min=num;
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
if(count == n)
break;
scanf_s("%d",&num);
}
average = 1.0*sum/n;
printf("Your average is %.2f\n",average);
printf("The sum of your squares is %d\n",squaresum);
printf("Your maximum number is %d\n",max);
printf("Your minimum number is %d\n",min);
return(0);
}
Assume your first number in the list as the minimum and maximum.
Compare every next character with the current minimum and the current maximum and update accordingly.
your while loop should look like
min=3;
max=0;
while(count<n)
{
scanf("%d",&num);
if(num>max)
max=num;
if(num<min)
min=num;
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
}
And I agree with Robert Harvey♦.. You must set min
Add a boolean, moved giving the values min, max 0 are the start of loop
#include<stdio.h>
int main()
{
float average;
int i, n, count=0, sum=0, squaresum=0, num, min, max;
bool first = true;
printf("Please enter the number of numbers you wish to evaluate\n");
scanf_s("%d",&n);
printf("Please enter %d numbers\n",n);
min=0;
max=0;
while(count<n)
{
scanf_s("%d",&num);
if (first) {
first = false;
min = max = num;
}
if(num>max)
max=num;
if(num<min)
min=num;
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
}
average = 1.0*sum/n;
printf("Your average is %.2f\n",average);
printf("The sum of your squares is %d\n",squaresum);
printf("Your maximum number is %d\n",max);
printf("Your minimum number is %d\n",min);
return(0);
}
Should also consider to check the return value of scanf
There're some issues in your code:
Where num is read? You should do it before min and max
When while loop executes first time you should just assign num to max and min.
Something like that:
int min = 0;
int max = 0;
// If your compiler supports C99 standard you can put
// bool first_time = true;
int first_time = 1;
while (count < n) {
printf("Please, enter the next number\n");
scanf_s("%d", &num);
// If your compiler supports C99 you can put it easier:
// if (first_time) {
if (first_time == 1) {
first_time = 0;
max = num;
min = num;
}
else {
if(num > max)
max = num;
if(num < min)
min = num;
}
...
int marks , marks_count=0 , max=0 , min=100 , marks_number=1;
float total , avg;
printf("Hit enter to input marks of 10 student.\n\n");
getchar();
do
{
printf("Input %d Mark : " , marks_number);
scanf("%d" ,& marks);
if (marks>max)
{
max=marks;
}
else if (marks<min)
{
min=marks;
}
marks_count++;
marks_number++;
total=total+marks;
}
while (marks_count<10);
while (marks_number<10);
avg=total/marks_count;
printf("\n\nAverage marks are : %.2f\n" , avg);
printf("Maximum marks are : %d\n" , max);
printf("Minimum marks are : %d\n\n\n" , min);
You can use this code, it checks if the loop starts for the first time or not. If it runs for the first time it assigns the value of n to the minimum and the maximum variable and after that, it continues. When it runs a second time it checks and finds that the program runs a second time thus it does not initialize the variables with the value of n without comparing.
int n, limit, sum = 0, minimum, maximum;
float average;
bool firstTime = "true";
printf("\nEnter Limit: ");
scanf("%d", &limit);
printf("\nEnter %d numbers: \n", limit);
for (int i = 0; i < limit; i++)
{
scanf("%d", &n);
if (firstTime)
{
minimum = n;
maximum = n;
firstTime = false;
}
if (minimum > n)
{
minimum = n;
}
if (maximum < n)
{
maximum = n;
}
sum = sum + n;
}
average = sum / limit;
printf("\nMinimum: %d", minimum);
printf("\nMaximum: %d", maximum);
printf("\nSum: %d", sum);
printf("\nAverage: %.3lf", average);

Resources