Floating Point Exception - One Variable Stopped Getting Updated - c

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.

Related

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

using several different functions

For my assignment, my program needs to ask the user which of the five functions he wants to execute. The five functions include:
Summation of a number
Factorial of a number
Fibonacci value of the nth term.
gcd of two numbers
a to the power of b.
The user will be prompted repeatedly until he wishes to exit. All my functions work fine. However, i think i messed up on one of the loops because once i enter which function i want to execute and enter a value, it keeps displaying the answer in an infinite loop.
#include <stdio.h>
#include <math.h>
// function to find summation of a number
int summation(int k) {
int i;
for(i = k; i >= 0; i--) {
k = i + (i-1);
}
return k;
}
// function to find the factorail of a number
int factorial(int num) {
int i;
for(i = num - 1; i > 0; i--) {
num = num * i;
}
return num;
}
// dunxtion to find the fibonacci of the nth term
int fibonacci(int n){
int i, t1 = 0, t2 = 1, nextTerm;
for(i = 1; i <= n; i++) {
if(i == 1) {
printf("%d, ", t1);
continue;
}
if(i == 2) {
printf("%d, ", t2);
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("%d, ", nextTerm);
}
return nextTerm;
}
// function to find the gcd of two numbers
int gcd(int n, int m) {
int i, gcd;
for(i=1; i <= n && i <= m; i++) {
// Checks if i is a factor of both integers
if(n % i == 0 && m % i == 0)
gcd = i;
}
return gcd;
}
// function to find value of n to the power of m
int power(int n, int m) {
return pow(n, m);
}
int main(void) {;
int option ,n, m;
//Asks user for what they want to find
printf("If you would like to find the summation of a number, enter 1 \n");
printf("If you would like to find the factorial of a number, enter 2 \n");
printf("If you would like to find the fibonacci sequence of a number, enter 3 \n");
printf("If you would like to find the gcd of two numbers, enter 4 \n");
printf("If you would like to find the power of a number a to b, enter 5 \n");
printf("If you would like to exit, enter 0 \n");
scanf("%d", &option);
// Enables the program to prompt the user until they wish to exit
while(option != 0) {
switch(option) { //If user wishes to find the summation
case 1: if(option == 1) {
printf("Enter a number: ");
scanf("%d", &n);
while(n > 0) {
if(n < 1) { //message displayed if an invalid value is entered
printf("invalid value");
}
else {
printf("Summation of %d is %d", n, summation(n));
}
}
}
case 2: if(option == 2) { //if user wishes to find factorial of a number
printf("Enter a number: ");
scanf("%d", &n);
while(n >= 0) {//message displayed if an invalid value is entered
if(n < 0) {
printf("invalid value");
}
else {
printf("factorial of %d is %d", n, factorial(n));
}
}
}
case 3: if(option == 3) { //if user wishes to find the fibonacci value of the nth term
printf("Enter a number: ");
scanf("%d", &n);
while(n >= 0) {//message displayed if an invalid value is entered
if(n < 0) {
printf("invalid value");
}
else {
printf("fibonacci of %d is %d", n, fibonacci(n));
}
}
}
case 4: if(option == 4) {
printf("Enter a number: ");
scanf("%d %d", &n, &m);
while(n >= 0 && m >= 0) {
if(n < 0 || m < 0) {//message displayed if an invalid value is entered
printf("invalid value");
}
else {
printf("GCD of %d and %d is %d", n, m, gcd(n, m));
}
}
}
case 5: if(option == 5) {
printf("Enter a number: ");
scanf("%d %d", &n, &m);
while(n >= 0 && m >= 0) {
if(n <= 0 || m < 0) {
printf("invalid value");
}
else {
printf("%d to the power of %d is %d", n, m, power(n, m));
}
}
}
default: if(option == 0) {
break;
}
}
scanf("%d", &option);
}
}
First of all, C has unstructured switch statement.
You need to add a break; statement after each of your case body to limit the execution for a particular case to the body mentioned under that case.
Otherwise, by default, (with the absence of a break statement) all the case statements works in fall-through manner. You can read more about it here.
That said, regarding the repeated execution of a single function, there's a serious flaw in most (if not all) of the the logic. For example, let's take this
while(n > 0) {
if(n < 1) { //message displayed if an invalid value is entered
printf("invalid value");
}
else {
printf("Summation of %d is %d", n, summation(n));
}
}
here, you're replying on n becoming 0 at some point to break out of the loop, but you did not modify n, at all.
To elaborate, C uses pass-by-value for argument passing, so for the call, summation(n), inside the function, whatever change you make to the parameter receiving the value of n, is not reflected in the caller, and thus, the n in the caller remains unchanged.
You just need a break statement at the end of every case
like:
case 1: if(option == 1) {
printf("Enter a number: ");
scanf("%d", &n);
while(n > 0) {
if(n < 1) { //message displayed if an invalid value is entered
printf("invalid value");
}
else {
printf("Summation of %d is %d", n, summation(n));
}
}
}
break;
As the control will fall down to next case if no break statement is present.

Program is hanging after input

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

Looping to look for non-repeated digits

I have a C program that I am working on which entails to find the first nonrepeated, last nonrepeated digit in an array. I am lost in how to accomplish this. Can anybody help me with the loop control variable to get those two things done ? My loop keeps returning a negative number.
Here is what I have:
for(p = 0; p <= i;p ++ ){
for(u=p+1;u>=1;u--){
if(digits[p] > digits[u]);
firstnon = digits[p];
}
}
if(firstnon){
printf("The first non-repeated digit is: %d",firstnon);
}
else{
printf("There isn't any non-repeated digits");
}
printf("\n");
for(c=0;c<= sizeof(digits)/sizeof(int);c++){
for(k=1;k<sizeof(digits);k++){
if(digits[c]==digits[k]){
lastnon = digits[k];
}
}
}
if(c==k){
printf("The last non-repeated digit is: %d\n",lastnon);
}
Have some problems in your code:
This line: if(digits[p] > digits[u]); don't do what you expect i think, see the last char ;, this kill the propose of the if statement. And the following line don't belong to the if block.
This sizeof(digits) return the size of digits in bytes, if the elements of digits are not bytes, you would be indexing the array with out of bound index (with k).
I don't understand really the search for last non-repeating digit, you are setting lastnon when digits[c] == digits[k], i don't think that it correct if i understand correctly the problem statement.
Here is a naive approach:
#include <stdio.h>
int main() {
int p, u, firstnon = -1, lastnon = -1, digit_repeat;
int digits[10] = { 1, 2, 3, 1, 3, 5, 1, 2, 4, 3};
int digits_size = sizeof(digits) / sizeof(digits[0]);
for (p = 0; p < digits_size; p++) {
digit_repeat = 0;
for (u = 0; u < digits_size; u++) {
if (p != u && digits[p] == digits[u]) {
digit_repeat = 1;
break;
}
}
if (!digit_repeat) {
firstnon = p;
break;
}
}
if (firstnon != -1) {
printf("The first non-repeated digit is: %d", digits[firstnon]);
} else {
printf("There isn't any non-repeated digits");
}
printf("\n");
for (p = digits_size - 1; p >= 0; p--) {
digit_repeat = 0;
for (u = 0; u < digits_size; u++) {
if (p != u && digits[p] == digits[u]) {
digit_repeat = 1;
break;
}
}
if (!digit_repeat) {
lastnon = p;
break;
}
}
if (lastnon != -1) {
printf("The last non-repeated digit is: %d", digits[lastnon]);
} else {
printf("There isn't any non-repeated digits");
}
printf("\n");
return 0;
}
To find the first non repeating digits.
Iterate through the digits.
In every digits, check the other digits (all) for the same digit in another position.
If there is another, continue
If there only one, saved, and break search.
The same apply for last non repeating digits, but the iteration is in reverse.
Some improve version.
#include <stdio.h>
int main() {
int p;
int digits[10] = { 1, 2, 3, 1, 3, 5, 1, 2, 4, 3};
int digits_size = sizeof(digits) / sizeof(digits[0]);
int used_digits[10] = { 0 };
int used_digits_size = sizeof(used_digits) / sizeof(used_digits[0]);
for (p = 0; p < digits_size; p++) {
used_digits[digits[p]]++;
}
for (p = 0; p < used_digits_size; p++) {
if (used_digits[p] == 1) {
printf("The first non-repeated digit is: %d", p);
break;
}
}
if (p == used_digits_size) {
printf("There isn't any non-repeated digits");
}
printf("\n");
for (p = used_digits_size - 1; p >= 0; p--) {
if (used_digits[p] == 1) {
printf("The last non-repeated digit is: %d", p);
break;
}
}
if (p == -1) {
printf("There isn't any non-repeated digits");
}
printf("\n");
return 0;
}
This version use an array to storage the usage of digits. The non-repeating digits are the one with usage == 1. Search this forward and backward to find first and last non repeating digit.

value returned from a function is not same as received. Why?

I am returning values 1 or 0 from function isprime(0 when it is not prime and 1 when it is prime) but when i print the returned value of x(return value of isprime) it is not same as what I returned from isprime. Why?
#include<stdio.h>
int isprime(int b);
main()
{
int a,rem,i;
printf("enter the number");
scanf("%d", &a);
for(i = 1; i < a; i++)
{
rem = a % i;
if(rem == 0)
{
int x = isprime(i);
printf(" value of x returned for i = %d is %d", i, x);
if(x = 1)
{
printf("%d\n", i);
}
}
}
return (0);
}
/**
*
*returns 1 if b is prime else returns 0
*/
int isprime(int b)
{
int x, count = 0;
printf("input recieved %d \n", b);
for(x = 1; x <= b; x++)
{
if (b % x == 0)
{
count = count + 1;
}
printf("the value of count is %d\n", count);
}
if(count == 2) {
printf("returning the value as 1\n");
return 1;
}
else {
printf("returning the value as 0\n");
return 0;
}
}
if(x = 1)
= is assignment. You need == operator instead. You are doing correct in other if conditions though.
Also, the logic of calculating prime numbers is inefficient. You can break the loop once the count is greater than 2.
if (b % x == 0)
{
count = count + 1;
if (count > 2)
{
// This ensures you are returning correct value later.
break;
}
}
Have a look at this algorithm: Sieve of Eratosthenes
This answer is correct.
For removing such kind of mistakes use it like
if(1=x)
using this approach you can avoid such behavior.
Here I am just approaching to avoid typo mistakes.

Resources