Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Type Casting | Tietotyyppien Käsittely
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Perusteet

bookChallenge: Type Casting

Tehtävä

Swipe to start coding

In this challenge, you will practice declaring variables of different numeric types and converting between them using explicit type casting. You will calculate the total cost of items in a shopping cart by declaring an int variable for quantity and a double variable for price per item, converting types as needed, and computing the total price.

Follow these steps:

  • Declare and initialize an int variable called quantity (set it to 4);
  • Declare and initialize a double variable called pricePerItem (set it to 3.75);
  • Convert quantity to a double and store it in a variable named quantityAsDouble;
  • Convert pricePerItem to an int and store it in a variable named priceAsInt;
  • Calculate the total cost by multiplying quantity and pricePerItem and store the result in a variable named totalCost;
  • Print all variables and their converted values to the console.

Example output:

Quantity: 4
Price per item: 3.75
Quantity as double: 4
Price per item as int: 3
Total cost: 15

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 16
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

close

bookChallenge: Type Casting

Pyyhkäise näyttääksesi valikon

Tehtävä

Swipe to start coding

In this challenge, you will practice declaring variables of different numeric types and converting between them using explicit type casting. You will calculate the total cost of items in a shopping cart by declaring an int variable for quantity and a double variable for price per item, converting types as needed, and computing the total price.

Follow these steps:

  • Declare and initialize an int variable called quantity (set it to 4);
  • Declare and initialize a double variable called pricePerItem (set it to 3.75);
  • Convert quantity to a double and store it in a variable named quantityAsDouble;
  • Convert pricePerItem to an int and store it in a variable named priceAsInt;
  • Calculate the total cost by multiplying quantity and pricePerItem and store the result in a variable named totalCost;
  • Print all variables and their converted values to the console.

Example output:

Quantity: 4
Price per item: 3.75
Quantity as double: 4
Price per item as int: 3
Total cost: 15

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 16
single

single

some-alt