Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to write this program by only using if-else, else if instead of while or for
and when the user writes the largest number twice, it should show the second-largest number as the largest number too like;
Enter five numbers: 53 -99 53 14 22
The largest number is: 53
The second-largest number is: 53

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int sayi =0 ;
int sayac = 1;
printf("Sayiyi Girin:");
scanf("%d",&sayi);

int enbuyuk = sayi ;
int ikinci_buyuk = sayi;
while (sayac != 5)
{
sayac++;
printf("Sayiyi Girin:");
scanf("%d",&sayi);

    if (sayi  > enbuyuk )
{
    ikinci_buyuk  = enbuyuk;
    enbuyuk = sayi;
}
    else if (sayi < enbuyuk)
    {
        
    if ((sayac == 2))
    {
        ikinci_buyuk = sayi;
    }
    else if (sayi > ikinci_buyuk)
    {
        ikinci_buyuk = sayi;
        }
    
    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;
}
Posted
Updated 16-Nov-19 22:45pm
v2

1 solution

Basically, don't.
If...else is not a loop construct, (do, while, and for are all loop constructs) so you can't directly replace while with if unless you also use goto - and you will be deservedly ridiculed if you do that, and told be all right thinking people to forget goto exists until you have been coding for at least five years.
 
Share this answer
 
Comments
Member 14657861 16-Nov-19 13:11pm    
OriginalGriff Thank you for your answer. I'll keep that in my mind. and can you check the code again please. when I write the largest number twice, it shows a different number as the second-largest. (though ı want it to show the same number if the largest ones are both equal.)
OriginalGriff 16-Nov-19 13:40pm    
I don't have access to your data - and that is likely to be relevant.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900