Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære if-else Operator Practice | Control Statements
C Basics

book
if-else Operator Practice

A Program Alerting High Temperature

c

main

copy
#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

#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?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 2
#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

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

We use cookies to make your experience better!
some-alt