#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i = 0; // declare integer and assigned to 0
int n;
srand(time(NULL)); // initialize random number generator
n = rand() % 14+2; // choose random number to place in n. high 14, no less than 2
printf("i is 0 to begin, so add 1 to the value of n for total executions\n");
printf("the value of n for i to reach is %d\n", n);
while (i <= n)
{
printf("loop executed value is %d\n", i);
i++; // increment loop 1 at a time
}
printf("while loop has executed %d times\n", i);
return 0;
}
i get 15 sometimes with rand(), should be a number from 2 to 14.
Thank you. That's all very helpful. Now to be a little more specific. I am starting with a user input (a whole number). The goal is to us a loop to decrement the input number by 10. What I want to out put is the final number of iterations ONLY.
#include <iostream>
usingnamespace std;
int main()
{
int ticketsWon;
int num; num = 0;
//ask user to input number of tickets won
cout << "Enter the number of tickets you won: ";
cin >> ticketsWon;
//if number entered is >= 10 subtract 10
while(ticketsWon >= 10)
{
ticketsWon = ticketsWon - 10;
cout << "Candy Bars = " << ++num << " \n";
}
return 0;
}
It's basically doing what I want, except it's printing every iteration. Like this: