cl-creator
cl-creator

Reputation: 25

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

Upvotes: 0

Views: 58

Answers (2)

chqrlie
chqrlie

Reputation: 144750

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

Upvotes: 1

the busybee
the busybee

Reputation: 12600

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 */
    }
}

Upvotes: 1

Related Questions