0

whileを使用する代わりにif-else、else ifステートメントのみを使用してこのプログラムを作成する方法はありますか?

また、すべての入力を1行にまとめたいのですが、代わりに

enter the number1:
enter the number2:
enter the number3:
enter the number4:
enter the number5:

それは次のようになるはずです

Enter 5 numbers: _ _ _ _ _

そして、同じ最大の数を 2 回書いた場合、このプログラムには最大の数を 2 番目に大きい数として表示してもらいたいのです。

例えば:

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.

これは私のコードです:

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

2 に答える 2

1

シングルでプロンプトを出してprintf()5 つの変数、またはシングルで 5 つの配列要素を読み取るだけで、5 つの数値を読み取ることができます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;
}
于 2019-11-16T20:14:44.787 に答える