Calculating min/max from the terminal in C - c

i want to create a program which prints biggest first number and biggest second number when i enter 5 number.(1.value of the number is=0) .there is no problem when i enter 5 numbers bigger than 0, but when i enter 5 numbers smaller than 0 , program prints first biggest number is 0 and second biggest number is 1.
#include <stdio.h>
int main() {
int number = 0, first, second, numbercounter = 0;
first = number;
while (numbercounter < 5) {
printf("5 tamsayi girin:", ++numbercounter);
scanf("%d", &number);
if (number > first) {
second = first;
first = number;
} else if (number > second)
second=number;
}
printf("En buyuk sayi: %d \n", first);
printf("En buyuk ikinci sayi:%d \n", second);
if (first % second == 0) {
printf("%d, %d'nin tam katidir.\n", first, second);
} else {
printf("%d, %d'nin tam kati degildir.\n", first, second);
}
if (first == second) {
printf("%d ve % esittir.\n", first, second);
} else {
printf("%d ve %d esit degildir.\n", first, second);
}
if (first % 2 != 0) {
printf("%d tektir.\n", first);
} else {
printf("%d cifttir.\n", first);
}
return 0;
}

There are two problems:
first starts out as 0. If number is negative, if (number > first) is false.
second is used uninitialised if number <= first.
Use INT_MIN for your initial values, this is the most negative integer you can have. Use this declaration
#include <stdio.h>
#include <limits.h>
int main()
{
int number = 0, first = INT_MIN, second = INT_MIN, numbercounter = 0;
Also get rid of the line
first = number;

Related

c program for finding the highest divisible number is not working

So I am new to C programming. The purpose of this program is to use a function to find the largest number divisible. Three numbers should be given and the answer should be the number that is higher than the first number and lower than the second number and should be the largest number between them that can be divided evenly by the third number.
#include <stdio.h>
#include <stdlib.h>
int my_function(int first, int second, int third) {
int i, answer;
for (i = second; i <= 0; i--) {
if (i < first || i > third) {
answer = 0;
break;
}
if (i % third == 0 && i != 0 && i > first)
answer = i;
}
return answer;
}
int main() {
printf("enter number one:\n");
int one, two, three, final;
scanf("%d", &one);
printf("enter number two\n");
scanf("%d", &two);
printf("enter number three\n");
scanf("%d", &three);
final = my_function(one, two, three);
printf("the number is %d", final );
return 0;
}
This program is not working. Can someone help me with my mistake?
Your function does not work for multiple reasons:
the loop test is always false if second is a positive number.
the test i > third will cause an early exit from the loop for no good reason.
when your find a divisible number, you should then exit the loop.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int my_function(int first, int second, int third) {
int i;
for (i = second - 1; i > first; i--) {
if (i % third == 0)
return i;
}
return 0;
}
int main() {
int one = 0, two = 0, three = 0, final;
printf("enter number one:\n");
scanf("%d", &one);
printf("enter number two\n");
scanf("%d", &two);
printf("enter number three\n");
scanf("%d", &three);
final = my_function(one, two, three);
printf("the number is %d\n", final);
return 0;
}

i want to take number from user then print if it prime or not but i can't find what is error where result always not prime so what is problem?

i want to take number from user then print if it prime or not but i can't find what is error where result always not prime so what is problem??
#include <stdio.h>
int main(void)
{
int number;
char flag = 0;
printf("Please enter the number:");
scanf("%d",&number);
for (int i = 1; i <= number; i++) {
if (number %i == 0) {
flag = 1;
break;
}
}
if (number==1)
printf("%d neither prime nor not prime", number);
if (flag==1)
printf("%d is not prime",number);
else
printf("%d is prime",number);
return 0;
}
Take a look at the loop condition here:
for(int i=1;i<=number;i++)
With <=, i goes up to number. So the last check will be if(number%number==0), which is always true: your program says that 5 is not a prime number because 5 divided by 5 has remainder 0. The same applies to dividing the number by 1 (which also results in no remainder), so this check should start at 2. This line should be:
for(int i=2;i<number;i++)
Typically i only has to go up to sqrt(number) because no two numbers bigger than the root of number multiplied will result in number.
Also, if the number entered is 1, you get two of the three possible outputs instead of just the first one. To fix this, put an else before the if (flag == 1).
edit to work successfully
#include <stdio.h>
int main(void)
{
int number ;
char flag=0;
printf("please enter the number:");
scanf("%d",&number);
for(int i=2;i<number;i++)
{
if(number%i==0)
{
flag=1;
break;
}
}
if (number==1)
{ printf("%d neither prime nor not prime", number);}
else if (flag==1)
{ printf("%d is not prime",number);}
else
{ printf("%d is prime",number);}
return 0;
}
Just check reminder either it is 0 or 1, if you divide any number in the end it will give you either 0 or 1 in reminder.If reminder is 1 so number is prime or 0 number is not prime.
#include <stdio.h>
int main(void)
{
int number ;
char flag=0;
printf("please enter the number:");
scanf("%d",&number);
if((number>2)&&(number%2==0))
{
flag=1;
}
if (number==1)
{ printf("%d neither prime nor not prime", number);}
if (flag==1)
{ printf("%d is not prime",number);}
else
{ printf("%d is prime",number);}
return 0;
}

Using if else statements instead of while

Is there any way to write this program by only using if-else, else if statements instead of using while.
And I also want all the inputs just in one line, instead of
enter the number1:
enter the number2:
enter the number3:
enter the number4:
enter the number5:
it should be like
Enter 5 numbers: _ _ _ _ _
And when I write the same largest number twice, I want this program to show me the largest number as the second-largest number, too.
For example:
Enter 5 integers: -88 53 41 53 -17
The largest one is: 53
The second largest one is: 53
53 is the multiple of 53
53 and 53 is equal to each other.
53 is an odd number.
This is my code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int sayi = 0;
int sayac = 1;
printf("Sayiyi Girin:");
scanf("%d", &sayi);
//ilk sayinin en buyuk oldugunu kabul ediyoruz.
int enbuyuk = sayi;
int ikinci_buyuk = sayi;
while (sayac != 5)
{
sayac++;
printf("Sayiyi Girin:");
scanf("%d", &sayi);
/*kitapligi ilk sayinin en buyuk oldugunu farz ediyor
* eger ikinci sayi daha buyukse buyuk olanın yerini alacak
* ayrica ikincisinide kontrol edecek
*/
if (sayi > enbuyuk)
{
ikinci_buyuk = enbuyuk;
enbuyuk = sayi;
}
else if (sayi < enbuyuk)
{
// This to avoid if numbers are arranges descending
if (sayac == 2)
{
ikinci_buyuk = sayi;
}
else if (sayi > ikinci_buyuk)
{
ikinci_buyuk = sayi;
}
//This to avoid if the user entered two equal numbers
else if (enbuyuk == ikinci_buyuk)
{
ikinci_buyuk = enbuyuk;
}
}
}
printf("sayac: %d\n", sayac);
printf("En buyuk sayi: %d\n", enbuyuk);
printf("İkinci en buyuk sayi: %d\n", ikinci_buyuk);
if (enbuyuk % ikinci_buyuk != 0)
{
printf("%d %d nin tam kati degildir. is not the multiple of", enbuyuk, ikinci_buyuk);
}
else
{
printf(" %d %d nin tam katidir. is the multiple of", enbuyuk, ikinci_buyuk);
}
if (enbuyuk != ikinci_buyuk)
{
printf(" %d ve %d birbirine esit degildir. not equal each other", enbuyuk, ikinci_buyuk);
}
else
{
printf(" %d ve %d birbirine esitir. equal each other", enbuyuk, ikinci_buyuk);
}
if (enbuyuk % 2 != 0)
{
printf("%d tek sayidir. odd number", enbuyuk);
}
else
{
printf("%d cift sayidir.even number", enbuyuk);
}
system("pause");
return 0;
}
From the title of your question:
if-else are a conditional code flow structure without any repetition. Without any other instruction (like goto for example) you can't make it a loop like while.
But I think this is not the core of your question. You seem to want to read 5 numbers and check them. For now you do this in a loop and you like to replace that loop with something else.
You can print the one and only prompt and then call a function for each of the 5 numbers to check them.
Since your variables are not translated and your intend is not clear, I'll leave the code inside the function as an exercise for you.
printf("Enter 5 integers: ");
for (int i = 1; i <= 5; ++i)
{
readnumber(/* you might need arguments */);
}
The function will read and check one number. scanf() will read just one number and leave the remainder of the input line for some next call.
void readnumber(/* see above */)
{
if (scanf("%d", &number) == 1)
{
/* handle the number */
}
else
{
/* handle the scan error */
}
}
You can read 5 numbers simply by prompting with a single printf() and reading into 5 variables, or 5 array elements with a single scanf():
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a[5];
int first, second, i, j;
printf("Enter 5 numbers: ");
if (scanf("%d%d%d%d%d", &a[0], &a[1], &a[2], &a[3], &a[4]) != 5) {
printf("Invalid input\n");
return 1;
}
/* I cannot adapt the rest of the code because I cannot understand your language */
/* Here is my quick implementation from the desired output */
/* select the 2 largest numbers */
for (i = 0; i < 2; i++) {
for (j = i + 1; j < 5; j++) {
if (a[i] < a[j]) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
first = a[0];
second = a[1];
printf("The largest one is: %d\n", first);
printf("The second largest one is: %d\n", second);
if (second != 0 && first % second == 0)
printf("%d is a multiple of %d\n", first, second);
if (first == second)
printf("%d and %d are equal\n", first, second);
if (first % 2 != 0)
printf("%d is an odd number.\n", first);
return 0;
}

For loop inside function does'n seem to work properly in C

I have to create a function in c that one value and two pointers, verify if the value is a prime number or if it is a composite number, show the maximum and minimum divisors and return 1 for composite and 0 for prime. The problem is the minimum value is returned correctly but the maximum value returns something out of for loop restriction, for example:
n = 12
min is 2 max is 61
Why *max = i is returning a number bigger than the variable "value"?
Here is the code:
#include<stdio.h>
int divs(int value, int *max, int *min)
{
for (int i = 2; i < value; i++)
{
if (value % i == 0){
if (*max == 0){
*min = i;
}
*max = i;
}
}
if (*max != 0)
return 1;
else
return 0;
}
int main(void)
{
int n,max = 0,min = 0,result;
printf("type a number bigger than 2\n");
scanf("%d",&n);
divs(n,&max, &min);
result = divs(n,&max, &min);
if (result == 1)
printf("min is: %d max is: %d",min,max);
printf("%d\n",result);
return 0;
}
Your code is fine, you just left out a \n in your printf statement in main, change to this:
printf("min is: %d max is: %d\n",min,max);
Which caused your next printf ('1') to print right next to it.
You did not print a new line:
if (result == 1)
printf("min is: %d max is: %d",min,max);
printf("%d\n",result);
When result is 1, you first print min is: 2 max is: 6, and then you print 1.
Change the first part to:
if (result == 1)
printf("min is: %d max is: %d\n", min, max);

How to print integers in typing order in C - increasing, decreasing or evenly

I have a problem with a c program I'm trying to write. The program must store integers in array (read from the keyboard). The numbers must be printed out in the order of entering, for example if you enter: 3 2 0 5 5 5 8 9, the ouput should be:
3 2 0 - decreasing
5 5 5 - evenly
8 9 - increasing
The problem for me is, that I can't write an algorithm which to be able to work in all cases.
I was trying to "flag" the elements with another array(using the same index, to save for each integer a value 1-for increasing, -1-for decreasing and 0 for evenly), but this works partly.
Have you any other ideas?
Thanks in advance :)
#include <stdio.h>
#include <stdlib.h>
main() {
int array[100];
int flag[100];
int num, i;
printf("Enter how many numbers you want to type: ");
scanf("%d",&num);
for(i=0;i<num;i++) {
scanf("%d",&array[i]);
}
for(i=0;i<num;i++){
if((array[i]<array[i+1])) {
flag[i]=flag[i+1]=1;
}
if(array[i]>array[i+1]) {
flag[i]=flag[i+1]=-1;
}
}
for(i=0;i<num;i++) {
if(array[i]==array[i+1]) {
flag[i]=flag[i+1]=0;
}
}
for(i=0;i<num;i++){
printf("%d ",flag[i]);
}
printf("\n");
for(i=0;i<num;i++) {
if(flag[i]==1) {
do{
if(flag[i]==1){
printf("%d ",array[i]);
i++;
}
}while(flag[i]==1);
printf(" - increasing\n");
}
if(flag[i]==0) {
do{
if(flag[i]==0){
printf("%d ",array[i]);
i++;
}
}while(flag[i]==0);
printf(" - evenly\n");
}
if(flag[i]==-1) {
do{
if(flag[i]==-1) {
printf("%d ",array[i]);
i++;
}
}while(flag[i]==-1);
printf(" - decreasing\n");
}
}
system("pause");
return 0;
}
Thoughts:
You only know if the 'first' number belongs to a descending, even, or ascending sequence after you see the 'second' number.
The 'third' number either belongs to the same sequence as the first two or is the 'first' number of another sequence.
So: check two numbers and assign a sequence type.
Keep checking numbers in the same sequence.
When you cannot assign the same sequence go back to checking two numbers and assigning a sequence.
Something like
int *n1, *n2, *n3;
n1 = <first element>;
n2 = n1 + 1;
n3 = n2 + 1;
/* some kind of loop */
if ((n3 - n2) HAS_SOME_SIGN_AS (n2 - n1)) /* n3 belongs to the sequence */;
n1++;
n2++;
n3++;
/* end loop */
#include <stdio.h>
int status(a, b){
if(a < b) return 1;
if(a > b) return -1;
return 0;
}
int main() {
int array[100]={0};
int old_status, new_status;
int old_pos;
int num, i, j;
const char* status_message[] = {"decreasing","evenly","increasing", "i don't know"};
printf("Enter how many numbers you want to type: ");
scanf("%d",&num);
for(i=0;i<num;i++)
scanf("%d",&array[i]);
//{num | 1 < num <= 100}
old_status =status(array[0], array[1]);
old_pos = 0;
for(i=1;i<num;++i){
new_status = status(array[i-1], array[i]);
if(old_status != new_status){
for(j=old_pos;j<i;++j)
printf("%d ", array[j]);
printf("- %s\n", status_message[1 + old_status]);
old_status = (i<num-1)? status(array[i], array[i+1]):2;
old_pos = i;
}
}
if(old_pos < num){ //not output section output
for(j=old_pos;j<i;++j)
printf("%d ", array[j]);
printf("- %s\n", status_message[1 + old_status]);
}
return 0;
}

Resources