Finding min and max between several integers - c

Seeing the solution to this online on Stackoverflow https://stackoverflow.com/a/19200031/3185410 I tried to come up with another solution by setting max to -infinity and min to +infinity.
The code here by #haccks works pretty fine.
#include <stdio.h>
int main()
{
int num, max, min;
printf ("Enter four numbers: ");
scanf ("%d", &num);
max = min = num;
for (int i = 0; i < 3; i++)
{
scanf ("%d", &num);
if (max < num)
max = num;
else if (min > num)
min = num;
}
printf ("\n%d %d", max, min);
return 0;
}
Here's the one I came up with:
#include <stdio.h>
#include <limits.h>
void main()
{
int max,min,num,i;
min=INT_MAX;
max=INT_MIN;
for (i=0;i<=3;)
{
i++;
printf("Enter number %d : ",i);
scanf("%d",&num);
if (num>max) max=num;
else if (num<min) min=num;
}
printf("max is %d and min is %d",max,min);
}
What am I doing wrong with that?

If the users input 5 then 6 you have :
for 5 :
5>-inf => max = 5
for 6 :
6>5 => max = 6
but then min has become 5 and yet it is still INT_MAX.
If you put if instead of else if, you loose in performance but have the right output I think (I cannot test right now).

Set first input as min and max value. And then add logic you wrote. Here you had 2 lines of extra code but you don't have to sacrifice performance.
#include <stdio.h>
#include <limits.h>
void main()
{
int max,min,num,i=0;
min=INT_MAX;
max=INT_MIN;
printf("Enter number %d : ",i+1);
scanf("%d",&num);
min=num;
max=num;
for (i=1;i<=3;)
{
i++;
printf("Enter number %d : ",i);
scanf("%d",&num);
if (num>max) max=num;
else if (num<min) min=num;
}
printf("max is %d and min is %d",max,min);
}

Related

Can anyone notice where the mistakes is in my code?

Can anyone tell me why is my output wrong "1* 3 = 1" whereas other multiplications is correct? Thanks!
#include <stdio.h>
int main()
{
int n,i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n",n);
while(i<= 12)
{
printf("\n%d*%d=%d", i, n, total);
i++;
total = i*n;
}
}
You are printing a value, total, before you assign any value to it. On the first iteration of the while loop, total doesn't contain any defined value.
Here's a way to fix it:
#include <stdio.h>
int main(void)
{
int n, i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n", n);
while (i <= 12)
{
total= i * n;
printf("\n%d*%d=%d", i, n, total);
i++;
}
}
Simply calculate total before you print it, not after.

Check the random number by modulo and user's input

im trying to determine how many times the user has guess right the number and place of a random number.
for example, if the number is 1234 and the user's input is 7214
so he guess right only the number 2 and 4.
because of this: 1[2]3[4] == 7[2]1[4].
problem: the program throws me out after i take the user's input. im getting an error which says: "Expression: result_pointer != nullptr"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int hit(int num);
int strike(int num);
int rndNum(int num);
void main()
{
int num = 0;
int chosenNum;
int saveHits;
srand(time(NULL));
printf("The Random number: %d", chosenNum = rndNum(num));
printf("\nPlease enter a 4 digit number: ");
scanf("%d", num);
saveHits = hit(num, chosenNum);
printf("\nThe number of hits: %d", saveHits);
getch();
}
int rndNum(int num)
{
int rndNum = rand() % 9000 + 1000;
return rndNum;
}
int hit(int num1, int chosenNum1)
{
int i, hit1 = 0;
for (i = 0; i < 4; i++)
{
if (num1 % 10 == chosenNum1 % 10)
hit1++;
num1 /= 10;
chosenNum1 /= 10;
}
return hit1;
}
You're missing a &
scanf("%d", &num);
^

How to find the Highest mark for giving three marks?

#include <stdio.h>
#include<conio.h>
int main ()
{
printf("Enter the Physics ,Chemistry and Maths Marks");
int mark[3]= {40,50,10};
int s[3];
int i;
int sum = 0, highest = 0;
clrscr();
for (i = 0; i < ; i++)
{
sum += mark[i];
if (mark[i] > highest)
highest = mark[i];
}
printf("The Highest Mark is %d: \n", highest);
getch();
return 0;
}
its working fine, I need to give a input dynamically and get the output
How to do that?
Enter the Marks : 30 20 10
output: 30
This:
int mark[3];
mark[3] = scanf("%d",mark[3]);
is wrong because of several reasons. It seems that you don't know how to use scanf properly. It should be
int mark[3];
scanf("%d", &mark[0]);
scanf("%d", &mark[1]);
scanf("%d", &mark[2]); /* Get each number from stdin and store it in the address of the variable given */
or better
int mark[3];
scanf("%d %d %d", &mark[0], &mark[1], &mark[2]);
or even better
int mark[3], i;
for(i = 0; i < 3; i++) /* Loop 3 times */
{
scanf("%d", &mark[i]);
}
You can get the numbers from the input using scanf() and then find the highest number using fmax():
#include <stdio.h>
#include <math.h>
int main()
{
int mark[3] = {0, 0, 0};
int highest = 0;
printf("Enter the Marks\n");
for (int i = 0; i < 3; i++) {
printf("%d: ", i + 1);
scanf("%d", &mark[i]);
highest = fmax(highest, mark[i]);
}
printf("The Highest Mark is %d: \n", highest);
}

Iteration program

I have written the following code:
#include <stdio.h>
/* max_number.c: outputs the largest number of five numbers entered */
void main(void) {
int i, num, max;
for (i = 1; i <= 5; i++) {
printf("Enter a number: ");
scanf("%d", &num);
if (num >= max)
max = num;
}
printf("The maximum number is %d\n", max);
}
When I run the program with any type of data I continually get "The maximum number is 14". Can someone please point me in the direction of what I am doing wrong? Thank you!
Variable max is not initialized.
Try
int i, num, max = INT_MIN;

Program to print N first factorial numbers in c

Factorial number is a number that is multiplied by it's previous numbers. For example it's 5. 1*2*3*4*5 is it's factorial number.
I already made a program which prints factorial of any number, but i don't know how to make it to print N first factorial number in c.
For example i type 10. It must show first 10 numbers along with their factorials (Making a table)
Here is what i was made to print factorial of any number.Is there any possibility to do with while/ if else statements/ and for loop?
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
printf("Factorial of %d js %d\n", n, fakt);
getch();
}
You probably want this:
Program:
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i=1;i<= n;i++) //use braces to write more than one statement inside the loop
{
fakt=fakt*i;
printf("Factorial of %d is %d\n", i, fakt);
}
getch();
}
Output:
Enter a number:
5
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
#include <conio.h>
#include <stdio.h>
void main()
{
int f=1,i,v;
clrscr();
printf("Enter the number :");
scanf("%d",&v);
for(i=1;i<=v;i++)
{
f=f*i;
printf("num =%d and fac=%d\n",i,f);
}
getch();
}
this code will work
That code already uses the for loop. The while loop equivalent is:
i = 1;
while (i <= n) {
fakt = fakt*i;
i++;
}
#include <stdio.h>
int factorial(int n)
{
int i,fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
return fakt;
}
int main()
{
int n;
printf("Enter a number:\n");
scanf("%d", &n);
int i = 0;
for(i=1;i<=n;i++)
{
printf("Factorial for %d is %d\n",i,factorial(i));
}
return 0;
}
I think this will do the job just fine.
You can do a nested loop.
Run the parent loop from 1 to n,
and the nested loop will be your already working for loop.
You may want this:
#include <stdio.h>
int main()
{
int n, i, num, factorial;
printf("Enter the number of terms: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
num = i;
factorial = 1;
while(num)
factorial *= num--;
printf("%d \t %d\n", i, factorial);
}
return 0;
}
Output:
Enter the number of terms: 10
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Use this fastest version of factorial using recursion with if ..else statement
#include<stdio.h>
int fact(int n);
void main()
{
int n;
printf("\nEnter an integer:");
scanf("%d",&n);
fact(n);
}
int fact(int n)
{
int a;
if(n==0)
{
printf("The Factorial of 0 is 1\n");
return 1;
}
else
{
a=n*fact(n-1);
printf("The Factorial of %d is %d\n",n,a);
return a;
}
}
#include<stdio.h>
int main(int n){
int fact;
clrscr();
printf("Enter a number and type exit:\n");
scanf("%d",&n);
if(n!=0){
fact=n*main(n-1);
printf("Factorial of %d is %d\n",n,fact);
getch();
return fact;
}
else{
printf("Factorial of 0 is 1.\n");
getch();
return 1;
}
}

Resources