Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Setting Iterations with the While Loop in C | While Loop
C Loops for Beginners

bookChallenge: Setting Iterations with the While Loop in C

A while loop in C repeatedly executes a block of code as long as a given condition is true. Here's a simple example to help you:

main.c

main.c

copy
123456
int i = 0; while (i < 3) { printf("Iteration %d\n", i); i++; }
Opgave

Swipe to start coding

Write a function in C called powerCounter that takes two integer parameters a and b. The function should calculate the value of a raised to the power of b (that is, a^b) using a while loop. For each multiplication performed in the loop, increment a counter variable. The function must return the final value of the counter variable, which should be equal to b (the exponent).

  • Use a while loop to repeat the multiplication process exactly b times.
  • Start with a result variable set to 1.
  • For each iteration, multiply the result by a and increment the counter.
  • After the loop, return the counter variable.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookChallenge: Setting Iterations with the While Loop in C

Stryg for at vise menuen

A while loop in C repeatedly executes a block of code as long as a given condition is true. Here's a simple example to help you:

main.c

main.c

copy
123456
int i = 0; while (i < 3) { printf("Iteration %d\n", i); i++; }
Opgave

Swipe to start coding

Write a function in C called powerCounter that takes two integer parameters a and b. The function should calculate the value of a raised to the power of b (that is, a^b) using a while loop. For each multiplication performed in the loop, increment a counter variable. The function must return the final value of the counter variable, which should be equal to b (the exponent).

  • Use a while loop to repeat the multiplication process exactly b times.
  • Start with a result variable set to 1.
  • For each iteration, multiply the result by a and increment the counter.
  • After the loop, return the counter variable.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
single

single

some-alt