if-else Operator Practice
A Program Alerting High Temperature
main
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main()
{
int temperature = 200; // in celsius
if (temperature > 80)
{
printf("Temperature is so high: %d degrees Celsius\n", temperature);
}
else
{
printf("Temperature is normal: %d degrees Celsius\n", temperature);
}
return 0;
}
123456789101112131415161718#include <stdio.h> int main() { int temperature = 200; // in celsius if (temperature > 80) { printf("Temperature is so high: %d degrees Celsius\n", temperature); } else { printf("Temperature is normal: %d degrees Celsius\n", temperature); } return 0; }
Oppgave
Swipe to start coding
Construct a calculator that divides numbers, but only if neither of the numbers is zero.
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
int main()
{
int a = 2;
double b = 3;
if (a != 0 && b != 0)
{
printf(" Answer: %f \n", a / b);
}
else if (a == 0 && b != 0)
printf(" Answer: 0 / b = *an infinitesimal number* \n");
else if (a != 0 && b == 0)
printf("Answer: a / 0 = *an infinite number*\n");
else
{
printf("Answer: 0 / 0 = ???");
}
return 0;
}
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 4. Kapittel 2
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
int main()
{
int a = 2;
double b = 3;
if (___)
{
printf(" Answer: %f \n", a / b); // integer division
}
else if (___)
printf(" Answer: 0 / b = *an infinitesimal number* \n");
else if (___)
printf("Answer: a / 0 = *an infinite number*\n"); // division by zero
else
{
printf("Answer: 0 / 0 = ???"); // division by zero
}
return 0;
}
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår