Program is hanging after input - c

The program is supposed to read numbers that are entered and determine if they are even or odd.
Then it needs to display the odd and even count, and the averages of the numbers entered. It stops when 0 is entered.
My problem is that it just hangs after the first number entered.
#include "stdio.h"
int main(void)
{
int oddcount = 0, evencount = 0; /* Count of even and odd numbers. */
int number;
float avge = 0, avgo = 0; /* Average for even and odd numbers. */
int evennum = 0, oddnum = 0;
printf("Enter a number or enter 0 to stop: ");
scanf_s("%i", &number);
while(number != 0)
{
if(number % 2 == 0)
{ evencount = evencount + 1;
evennum += number; }
else
{ oddcount = oddcount + 1;
oddnum += number; }
}
if(oddnum != 0)
avgo = oddnum / oddcount; /* Finding the odd average. */
if(evennum != 0)
avge = evennum / evencount; /* Finding the even average. */
printf("Here is the count odd %i and even %i numbers.\n", oddcount, evencount);
printf("And their averages %f odd and %f even.\n", avgo, avge);
return 0;
}

You aren't reading new numbers in after the first one. Try something like
while( scanf("%d", &number) == 1 && number != 0){
//Count evens and odds
}
Another way you could handle this would to be to add another scanf at the end of your while loop

Because it is an infinite loop...
while(number != 0)
{
if(number % 2 == 0)
{ evencount = evencount + 1;
evennum += number; }
else
{ oddcount = oddcount + 1;
oddnum += number; }
}
Here,your while-loop guiding variable number never gets modified in-between the iterations which keeps it unchanged and hence,the while condition remans true for-ever...
Probable solution seems to be :-
int number=1; // just for initialisation with a non-zero number...
while(number != 0)
{
printf("Enter a number or enter 0 to stop: ");
scanf_s("%i", &number);
if(number % 2 == 0)
{ evencount = evencount + 1;
evennum += number; }
else
{ oddcount = oddcount + 1;
oddnum += number; }
}

Related

Floating Point Exception - One Variable Stopped Getting Updated

everyone. Could anybody help me correct the code?
#include<stdio.h>
int main (void) {
int first = 0, second = 0, product, count = 2, lcm = 0, countProduct = 0;
printf("Enter the first integer: ");
scanf("%d", &first);
printf("Enter the second integer: ");
scanf("%d", &second);
product = -1;
while (product % first != 0 && product % second != 0){
if (first % second == 0) {
lcm = first;
break;
}else if (second % first == 0)
{
lcm = second;
break;
} else if (first > second)
{
countProduct = first * count;
count++;
if (countProduct % first == 0 && countProduct % second == 0)
{
lcm = countProduct;
product = lcm;
}
} else
{
countProduct = second * count;
count++;
if (countProduct % first == 0 && countProduct % second == 0)
{
lcm = countProduct;
product = lcm;
}
}
}
printf("GCD of %d and %d is %d\n", first, second, first*second / lcm);
return 0;
}
What I found out so far is that when I assign 1 to first followed by any other number, the lcm variable does not get updated but remains the value during initialization.
Everything other set I did gave me the expected output except for this.
Thank you so much.

Find two largest numbers in input

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;
}

how to fix this unexpected output?

I have been doing on decomposing Integers to prime numbers, but i got stucked , it s working for one prime number, but when the iteration continues then it s not working properly and I can t come up with any solution.
For example for number 12 i expect output 2^2 x 3
but instead program stops at 2^2 x and new input is expected.
int main(int argc, char *argv[])
{
int number;
int stop = 1;
int prime = 0;
int sqr = 0;
while (stop == 1) {
/**------ERROR------ **/
if(scanf("%d",&number) != 1 || number < 0 ){
fprintf(stderr,"%s","Error: Chybny vstup!\n");
return 100;}
if(number == 0){
stop = 0;
break;}
/**------DECOMPOSITION------ **/
for (int index = 1;index <=number;){
if(number == 1){
//Ak sa input cislo rovna 1 tak vypise nasledovne riadky
printf("Prvociselny rozklad cisla 1 je:\n");
printf("1\n");
break;}
if(number % (index + 1) == 0){
// ak je cislo % index +1 (pociatocna hodnota je 2) tak sa vydeli cislom index + 1
// do prime sa ulozi dane prvocislo
// ak je delitelne prvocislom pripocitame sqr +1
// cyklus pokracuje s rovnakym indexom
number = number / (index + 1);
prime = index + 1;
sqr = sqr + 1;
continue;}
else{
if(sqr == 1){
// vypise len prvocislo
printf("%d",prime);}
if(sqr != 1){
//vypise prvocislo aj mocninu
printf("%d^%d", prime,sqr);}
if(number != 1){
printf(" x ");}
else{
printf("\n");
break;}
}
index = index + 1;
sqr = 0;
}
}
return 0;
}
I see two problems in the code:
The special case for number == 1 is being handled inside the loop. That special case is much easier to handle before the loop.
Counting the exponent is mixed into the loop that updates the factor. It's much easier to use a second loop to count the exponent.
#include <stdio.h>
int main(void)
{
while (1){
/**------GET USER INPUT------ **/
int number;
if(scanf("%d",&number) != 1 || number < 0 ){
fprintf(stderr,"%s","Error: Chybny vstup!\n");
return 100;
}
/**------HANDLE SPECIAL CASES------ **/
if(number == 0){
break;
}
if(number == 1){
printf("Prvociselny rozklad cisla 1 je:1\n");
continue;
}
/**------DECOMPOSITION------ **/
for (int factor = 2; factor <= number; factor++){
int exponent = 0;
while(number % factor == 0){
number /= factor;
exponent++;
}
if (exponent == 1){
printf("%d", factor);}
else if (exponent > 1){
printf("%d^%d", factor, exponent);}
if(exponent != 0 && number != 1){
printf(" x ");}
}
printf("\n");
}
}

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;
}

Unusual Floating point exception (core dumped) Error with C

I am currently a student, trying to get factorials to print out as prime numbers multiplied to certain exponents like so:
5! = (2^3)(3^1)(5^1)
However, I keep getting an unusual error, which occurs right after using scanf to retrieve my input (By the way, I would really appreciate someone showing me how to retrieve multiple inputs from an exterior file to do this using input redirection, since that's how we were supposed to retrieve our inputs for this).
Anyway, I'm assuming this error is somewhere in the specification for my while loop. I would greatly appreciate any help/tips/pointers. Thank you!
#include <stdio.h> //headers
#include <stdbool.h>
//function prototypes - I will be using functions inside of each other
int find_prime_count (int prime, int num);
int find_next_prime (int prime);
bool is_prime (int num);
int main(void) //main function
{
int primeCount[100] = {0}, prime = 2, fact, i = 2, temp = 2, currentPrimeCount, printCount = 0;
printf ("Enter number: ");
scanf ("%d", &fact);
while (i <= fact)
{
printf ("i is less than factorial");
while (temp != 1)
{
printf ("Temp is not equal to one");
currentPrimeCount = find_prime_count (prime, temp);
printf ("currentPrimeCount calculated");
temp = temp / (currentPrimeCount * prime);
printf ("Temp updated");
primeCount[prime + 1] += currentPrimeCount;
printf ("primeCount[prime + 1] updated");
prime = find_next_prime (prime);
printf ("Next prime found");
}
i += 1;
temp = i;
}
printf ("%3d! = ", fact);
i = 0;
while (i < 100)
{
if (primeCount[i] != 0)
{
if (printCount == 0)
{
printf ("(%d^%d)", i, primeCount[i]);
}
else if (printCount != 0)
{
printf (" * (%d^%d)", i, primeCount[i]);
}
printCount += 1;
if ((printCount % 9) == 0)
{
printf ("/n");
}
if ((printCount > 9) && ((printCount % 9) == 0))
{
printf (" ");
}
}
}
return 0;
}
bool is_prime (int num)
{
bool check = true; //sets check variable to true
int i = 2; //starts counter variable at 2 (will test all numbers >=2 && <num)
while (i < num && check == true)
{
if ((num % i) == 0) //if it is divisible by any number other than 1 and itself
{
check = false; //it is not a prime number and the check becomes false
}
i += 1; //increasing counter
}
return check; //returns boolean value
}
int find_next_prime (int prime)
{
int i = prime;
bool check = false;
printf ("find_next_prime starts.");
while (check == false)
{
i += 1;
check = is_prime (i);
}
printf ("find_next_prime ends.");
return i;
}
int find_prime_count (int prime, int num)
{
int count = 0;
printf ("find_prime_count starts.");
while ((prime % num) == 0)
{
count += 1;
num = num / prime;
}
printf ("find_prime_count ends.");
return count;
}
Using gdb, I can tell that it is a divide by zero error in prim % num.
Hints:
Compile with the -g flag
Run using gdb
Set a breakpoint ...

Resources