Find two largest numbers in input - c

I need to make a program that will perform the following task:
Enter N natural numbers. Complete the input with 0. Output the number
of the maximal number.
I have already done this, and you can see the code below:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
int i = 0, num, max_place = -1;
int max = -2147483647;
printf("Start enter numbers, bruh (please end input with 0):\n");
scanf("%d", &num);
while (num != 0) {
if (num >= max) {
max = num;
max_place = i;
}
i++;
scanf("%d", &num);
}
if (max_place == -1) printf("Numbers were not entered");
else printf("\nMax number was on %d place, bruh", max_place + 1);
return 0;
}
The teacher then made the task more difficult – the program needs to print the maximum number and the next maximum after it of the entered numbers.
How can I do it?

If you can use arrays and sort use that way. if not, this is in your code
int main(void) {
int i = 0, num, max_place = -1, second_max_place = -1;
int max = -2147483647;
int second_max = -2147483647;
printf("Start enter numbers, bruh (please end input with 0):\n");
scanf("%d", &num);
while (num != 0) {
if (num == 0) break;
if (num >= max) {
second_max = max;
second_max_place = max_place;
max = num;
max_place = i;
}
if(num < max && num >= second_max){
second_max = num;
second_max_place = i;
}
i++;
scanf("%d", &num);
}
if (max_place == -1) printf("Numbers were not entered");
else{
printf("\nMax number was on %d place, bruh", max_place + 1);
printf("\nSecond Max number was on %d place, bruh", second_max_place + 1);
}
return 0;
}

Related

New To C and trying to make a collatz conjecture program but it's not working and i don't know why

This is my attempt at a c program which solves the collatz conjecture i am really new to c and would like to know why my code is not working
#include <stdio.h>
int main()
{
int num = 0;
int count = 0;
printf("Enter your number: ");
scanf("%d", &num);
while ("%d" != 2,num);{
if (num %2 == 0)
{
num = num / 2;
printf("%d", num);
count = count + 1;
}
else
{
num = num * 3 + 1;
printf("%d", num);
count = count + 1;
}
}
if (num == 1);
{
printf("%d", count);
}
return 0;
}
Your syntax is wrong.
The condition in while should be a boolean condition, and num variable should be different than 1.
I edit also printfs to be more suggestive and add a new line to them. I excluded the last if condition because it was redundant. When the code finish while loop the num variable will be 1.
I think what you are trying to do is this:
#include <stdio.h>
int main()
{
int num = 0;
int count = 0;
printf("Enter your number: ");
scanf("%d", &num);
while (num != 1)
{
if (num %2 == 0)
{
num = num / 2;
printf("num = %d\n", num);
count = count + 1;
}
else
{
num = num * 3 + 1;
printf("num = %d\n", num);
count = count + 1;
}
}
printf("count = %d", count);
return 0;
}

How to get min/max value from 10 numbers with a loop? (C language)

Hi im fairly new in programming and started with C language and now im stuck with loops.
The problem is that I try to write a program that has to get an input value of 10 INT numbers that are greater than 20, and after that the program has to determine which of the numbers is the maximum and which is minimum. at the end it has to calculate the average of all numbers.
So now I managed to get only the average calculation to work correctly, and the main problem is the max/min values.
#include<stdio.h>
void main()
{
//Variables
int num, i = 1, cnt = 0, sum = 0, max = 0, min = 0;
float average;
printf("Enter 10 int numbers greater than 20:\n");
//Input check
while (i <= 10)
{
printf("\n%d) ", i);
scanf("%d", &num);
max = num;
min = num;
if (num <= 20)
{
printf("Wrong number! enter an integer greater than 20:\n");
continue;
}
i++;
sum += num;
cnt++;
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
//Average calculation and output
average = sum / (float)cnt;
printf("The maximum number is: %d\n", max);
printf("The minimum number is: %d\n", min);
printf("The average of all numbers is: %.2f\n", average);
}
Here is a quick check game in c that asks for few numbers and then when you enter 0 it shows you the lowest and highest number in the array (number collected)
int main(){
int a, max = 0, min;
char answer;
while(answer != 'n')
{
for(int i = 0; i < 100; i++){
printf("Enter Number:");
if (scanf("%d", &a) == 1)
{
if(a == 0) //check if the input value is 0 then break the loop
break;
else
{
if(a > max)
max = a;
if(a < min)
min = a;
}
}
}
printf("lowest: %d, highest: %d", min, max);
printf("\nWould you like to start over? (j/n): ");
scanf("%s", &answer);
max = 0;
//min=0;
if(answer == 'n')
break;
}
return 0;
}

C loop until 0 is given

I'm trying to make a loop that lets the user enter numbers one by each other and calculate the sum of all those numbers until the user enters 0 as an input. However, my code is running only once and then stops.
#include <stdio.h>
int main(void) {
int i = 0;
int num;
int total = 0;
printf("Give me a number \n");
scanf("%d", num);
if(num < 0 && num > 0){
printf("Give me a number \n");
scanf("%d", num);
total = total + num;
i = i + 1;
}
printf("The total is %d", total);
}
There are no loops in your code
The condition (num < 0 && num > 0) is alway false. A number can't be: less than zero AND greater than zero
Wrong call of scanf
You probably want:
printf("Give me a number \n");
if (scanf("%d", &num) != 1) exit(1);
while(num != 0){
total = total + num;
i = i + 1;
printf("Give me a number \n");
if (scanf("%d", &num) != 1) exit(1);
}
The above solution will exit the program if the user inputs a non-integer, e.g. a letter.
An alternative solution that will end the loop on non-integer input is:
printf("Give me a number \n");
while(scanf("%d", &num) == 1 && num != 0){
total = total + num;
i = i + 1;
printf("Give me a number \n");
}
This last solution can even be written a little more compact - like:
while(printf("Give me a number \n"), scanf("%d", &num) == 1 && num != 0){
total = total + num;
i = i + 1;
}
It's a bit harder to read but avoids duplicated lines.
This is the idiomatic way IMO. There is only one scanf call to scanf and we use break to end the loop prematurely if the user has entered 0.
#include <stdio.h>
int main() {
int i = 0;
int num;
int total = 0;
while (1) // loop forever
{
printf("Give me a number \n");
scanf("%d", &num);
if (num == 0) // if the number is 0
break; // we terminate the loop
total = total + num;
i = i + 1;
}
printf("You entered %d numbers, the total is %d", i, total);
}
Disclaimer:
for brevity there is no error check for scanf which IMO is OK for toy programs.
Otherways the remarks provided by 4386427 in his answer are correct.
Make sure to take the address of the variable (&num) in the scanf() call and add a do{ } while() statement.
int main(){
int i = 0;
int num;
int total =0;
do {
printf("Give me a number \n");
scanf("%d", &num);
total = total + num;
i = i + 1;
} while(num != 0);
printf("The total is %d", total);
}
the problem is in the if statement you are trying to check if the given number (num) is at once greater than 0 and less than 0 which is why the if scope won't be accessed u can use the following if statement
if(num!=0)
this will check if the num is different than zero!
and since you are trying to repeat this bloc until 0 is inserted you have to use a while/dowhile instead.
you have also to pay attention to the scanf because you forgot to specify the pointer of the num variable (scanf("%d", &num);)
In your case, use of while (num != 0) would be more suitable.
int main(){
int i = 0;
int num = 1;
int total = -1;
while (num != 0){
printf("Give me a number \n");
scanf("%d", &num);
total = total + num;
i = i + 1;
}
printf("The total is %d", total);
}

School exercice in c about prime numbers

Make a program that asks the user for an integer and says if the
number is prime or not. A number greater than 1 is prime if only
is divisible by 1 and by itself. Then, it will tell us what the prime number is.
example:
Enter a number: 8
8 is not first. The first one immediately superior to 8 is 11.
Enter a number: 5
5 is first. The first one immediately above 5 is 7.
I can only solve first part.
Here is my code:
#include <stdio.h>
int main() {
int num, i;
do {
printf("Enter a numer: ");
scanf("%d", & num);
}
while (num < 1);
for (i = 2; i < num; i++) {
if (num % i == 0)
printf("Its prime");
}
if (num % 1 == 0 && num % num == 0)
printf("Not prime");
return 0;
}
Try this logic. Not tested
#include <stdio.h>
int main()
{
int num, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
int isPrime=IsPrime(num)
if(isPrime==0){
numNext=num+1;
int nextPrimeNum=checkNextPrime(numNext);
}
}
int IsPrime(int num){
for(i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num%i == 0)
{
flag = 1;
break;
}
}
if (num == 1)
{
flag=1;//neither prime nor composite
}
return flag;
}
int checkNextPrime(int numNext){
int isNextPrime=IsPrime(numNext)
if(isNextPrime==0){
printf("This is the required output :"numNext);
return numNext;
}
else{
numNext=numNext+1;
checkNextPrime(int numNext)
}
}

Program C counting total number of odd digits

I'm writing a C program that counts the number of odd digits from user input.
Eg.
Please enter the number: 12345
countOddDigits(): 3
int countOddDigits(int num);
int main()
{
int number;
printf("Please enter the number: \n");
scanf("%d", &number);
printf("countOddDigits(): %d\n", countOddDigits(number));
return 0;
}
int countOddDigits(int num)
{
int result = 0, n;
while(num != 0){
n = num % 10;
if(n % 2 != 0){
result++;
}
n /= 10;
}
return result;
}
The code is not working.
Can someone tell me where does it go wrong?
There were a few mistakes in your code. Here is a working version of your code:
#include <stdio.h>
int countOddDigits(int n);
int main()
{
int number;
printf("Please enter the number: \n");
scanf("%d", &number);
printf("countOddDigits(): %d\n", countOddDigits(number));
return 0;
}
int countOddDigits(int n)
{
int result = 0;
while(n != 0){
if(n % 2 != 0)
result++;
n /= 10;
}
return result;
}
You are mixing n and num together - there is no need for two variables.
n%=10 is just causing mistakes - you need to check the last digit if(n%2!=0) and then move to the next one n/=10, that's all.
Looping variable is not correct. Your outer loop is
while (num !=0)
but the num variable is never decremented; the final statement decrements the n variable. My guess is you want to initialize
int n = num;
while (n != 0 )
{ ...
n/= 10;
}

Resources