C Prog - While loop - c

Hi once again I have found myself stuck with coding. I have found the correct code but I do not understand why my initial coding does not work. Could anyone explain briefly the problem?
My initial code and the runnable code is posted below.
INITIAL CODE (does not work as expected)
#include <stdio.h>
#include <conio.h>
int main(void)
{
int counter;
int num1, num2, smallest;
counter = 1;
printf("Enter first number:");
scanf("%d", &num1);
smallest = num1;
while (counter <= 10) // User input phase
{
printf("Enter next numer:");
scanf("%d", &num2);
if (num1 < num2)
{
smallest = num1;
}
if (num2 < num1)
{
smallest = num2;
}
counter++;
}
printf("Smallest is %d", smallest);
getch();
}
RUNNABLE CODE
#include <stdio.h>
#include <conio.h>
int main(void)
{
int counter;
int num1, smallest;
counter = 1;
printf("Enter first number:");
scanf("%d", &num1);
smallest = num1;
while (counter <= 10) // User input phase
{
printf("Enter next numer:");
scanf("%d", &num1);
if (num1 < smallest)
{
smallest = num1;
}
if (smallest < num1)
{
smallest = smallest;
}
counter++;
}
printf("Smallest is %d", smallest);
getch();
}

In your first attempt you save the value of smallest each loop, but you get rid of it because of the next loop will check always num1 and new entered number in num2.
In the second code, the working one, the loop is keeping track of the last smallest found.
A little thing, the code chunk
if (num1 < smallest)
{
smallest = num1;
}
if (smallest < num1)
{
smallest = smallest;
}
the second if does not make sense, you can write
if (num1 < smallest)
{
smallest = num1;
}
else
{
smallest = smallest;
}
or better
if (num1 < smallest)
{
smallest = num1;
}

Related

can someone help me idk which mean to 1 do

I tried to do this:
#include <stdio.h >
int main(void) {
int common = 0;
int num1;
int num2;
int big;
int small;
int i;
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
if (num1 <= num2) {
num1 = small;
num2 = big;
}
else{
num1 = big;
num2 = small;
}
for (i = small ; ; i++)
{
}
I don't know what is meant by to 1 do. I'm beginner in algorithms
but I think it means i++ isn't?
The algorithm says to count from small down to 1. So the repetition should be i--, not i++. Use i >= 1 as the repetition condition to keep looping until you get to 1.
for (i = small; i >= 1; i--)

C programming find min,max and sum without using recursion and arrays

I am trying to write a program that get bunch of numbers, n >= 5
The program asks from the user to enter n non-negative numbers and calculate min,max and the sum of the numbers.
In case the user enter negative numbers, the function asks to enter a positive number.
I have a problem with the first negative number, any clue what's wrong with the following code?
void main()
{
int x;
printf("Enter number:\n");
scanf("%d", &x);
if (x >= 5)
{
int max = 0, min, num1;
printf("Enter numbers: \n");
scanf("%d", &num1); //here was the error
min = num1;
int sum = num1;
for (int i = 1; i < x; i++)
{
scanf("%d", &num1);
while (num1 < 0)
{
{
printf("Enter again number: /n");
scanf("%d", &num1);
}
}
if (num1 > max)
max = num1;
else if (num1 < min)
min = num1;
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d", max, min, sum);
}
else
printf("invalid number!");
}
output:
Enter number:
8
Enter numbers:
-8
7
6
9
10
6
7
6
The max number is 10, and the min is -8, and the sum is 43
check if min<0 assign new entered value of num1 to it.
for (int i = 1; i < x; i++)
{
scanf("%d", &num1);
if (min < 0)
min = num1;
while (num1 < 0){
//rest of the code}
}
I have a problem with the first negative number, any clue what's wrong with the following code?
Do not read first number with special code, which lacks a negative test. Simplify and read just like other numbers.
Simply start min,max with extremes.
#include <limits.h>
...
int max = INT_MIN;
int min = INT_MAX;
int sum = 0;
printf("Enter numbers: \n");
for (int i = 0; i < x; i++) { // start at 0
int num1;
scanf("%d", &num1);
while (num1 < 0) {
printf("Enter again number: /n");
scanf("%d", &num1);
}
if (num1 > max)
max = num1;
// else if (num1 < min)
if (num1 < min)
min = num1;
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d",
max, min, sum);
you should put while after taking the input from the user to prevent any negative number also inside while there isn't need to put double cure prates
#include <stdio.h>
int main()
{
int x;
printf("Enter number:\n");
scanf("%d", &x);
if (x >= 5)
{
int max = 0, min, num1,sum;
printf("Enter numbers:\n");
sum = 0;
min = num1;
for (int i = 1; i<=x; i++)
{
scanf("%d",&num1);
while(num1<0)
{
printf("Enter again number: \n ");
scanf("%d", &num1);
}
if (num1 > max)
{ max = num1;}
if (num1<min)
{ min = num1;}
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d", max, min, sum);
}
else
printf("invalid number!");
return 0;
}
````````````

Printing Fibonacci numbers up to n in C

The following code uses a user define function to print fibonacci numbers up to the user's choosing. The only problem is that the output includes the first n fibonacci numbers, followed by a zero. (e.g. 1,1,2,3,5,8,0) How can I get rid of the zero?
#include <stdio.h>
int fibonacci(int a);
int main()
{
int j, number;
printf("Enter the number of terms: ");
scanf("%d", &number);
printf("Fibonacci Series: ");
j = fibonacci(number);
printf("%d", j);
}
int fibonacci (int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
return 0;
}
I learned that we are supposed to return something when using functions other than main.
If you learned that, you should look for a better learning source. You could well have written:
void fibonacci(int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
}

Sum of the odd digits and even digits from the input and compare them. If both are equal print stat1 else stat2

This is the code, this code is not working. For loop needs to be used for this program. I need help to make this work.
Program prints same statement for different numbers. Kindly debug this and help me to understand the concept.
#include<stdio.h>
int main()
{
int n,i,sum=0,sum1=0,rem;
printf("enter values\n");
scanf("%d",&n);
for(i=n;i<=n;)
{
rem=n%10;
if(rem%2 == 0)
{
sum=sum+rem;
}
else
{
sum1=sum1+rem;
}
n=n/10;
}
if(sum==sum1)
printf("I will win the Card Game");
else
printf("I will not win the Card Game");
return 0;
}
#include<stdio.h>
int main()
{
int n,i,sum=0,sum1=0,rem;
printf("enter values\n");
scanf("%d",&n);
for(i = 0; i < n;)
{
rem=n%10;
if(rem%2 == 0)
{
sum=sum+rem;
}
else
{
sum1=sum1+rem;
}
n=n/10;
}
if(sum==sum1)
printf("I will win the Card Game\n");
else
printf("I will not win the Card Game\n");
return 0;
}
Try this:
Use Array to get multiple values and compare them like below.
#include<stdio.h>
int main()
{
int n,*arr,i,sum=0,sum1=0;
printf("how many numbers\n");
scanf("%d",&n);
printf("enter values");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
if(arr[i]%2 == 0)
sum=sum+1;
else
sum1=sum1+1;
}
if(sum==sum1)
printf("I will win the Card Game");
else
printf("I will not win the Card Game");
return 0;
}
Calculate the length of user's number, and you will know how much numbers you got with this loop:
temp = n;
length = 0;
while (temp > 0) {
length++;
temp /= 10;
}
Then insert this code before your main loop, and in your main 'for' loop head you can use full 'for' syntax:
for(i = 0; i < length; i++)
Put all together:
int main()
{
int n, i, sum = 0, sum1 = 0, rem, length, temp;
printf("enter values\n");
scanf("%d",&n);
temp = n; // Temp variable to calculate digits' count
length = 0; // Digits' counter
while (temp > 0) { // Loop to calculate digits' count
length++; // increase counter
temp /= 10; // remove the unity digit, untill the number equal to 0.
}
for(i = 0; i < length; i++) // go through all digits
{
rem = n % 10;
if(rem % 2 == 0)
{
sum = sum + rem;
}
else
{
sum1 = sum1 + rem;
}
n = n / 10;
}
if(sum == sum1)
printf("I will win the Card Game, sum: %d", sum);
else
printf("I will not win the Card Game, sum: %d, sum1: %d", sum, sum1);
return 0;
}

printf is only printing "1537" rather than the value of the integer

I'm just now learning programming, so this might seem a tiresome question. But when I try to print the value of an integer, it prints "1537" instead. Here is the function, feel free to scrutinize.
void compare(void) {
int num1;
int num2;
int num3;
int num4;
int smallest;
printf("Please enter four integers:\n");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
num1 = smallest;
if (num2 < smallest)
num2 = smallest;
if (num3 < smallest)
num3 = smallest;
if (num4 < smallest)
num4 = smallest;
printf("%d is the smallest\n", smallest);
}
you need to assign the values to smallest rather than from it.
Change it to
smallest=num1;
if (num2 < smallest)
smallest= num2;
if (num3 < smallest)
smallest=num3;
if (num4 < smallest)
smallest=num4;
~Sometimes its tough to catch the smallest mistakes~ :)
You don't initialize smallest. Actually, you don't write to it at all, anywhere.

Resources