简体   繁体   中英

How to increment months loop. Then display from then

Hell everyone ! I'm a noob to programming. I've tried to search something similar to what I'm looking for but I can't fully understand similar examples.

What I'm trying to do is have an user input a number of years, convert years to months. Then display from month 1 to full amount of months in a list while using the months variable. I need the loop to continue until it reaches the the full amount of months, then stop there. Below is what I know so far and what I've learned. I suspect that I may need to use some type of counter variable but not exactly sure how to do it.

int main(){
int years, months;
printf("Enter years ");
scanf_s("%d", &years);

months = years * 12;
printf("Months is %d ", months); 

do {
    printf("Month", ); //Month 1,2,3,4........24 up to full amount that was converted from years//
} while ();
return 0;
int count = 1;
while (month != 0) {
   printf("%d, ", count);
   month = month - 1;
   count = count + 1;
} 

Take a look at loops, while, for and do-while. Here is a link to a simple and direct tutorial: https://www.tutorialspoint.com/cprogramming/c_loops.htm

For your problem just create an extra variable initialized with 1 and loop until it passes the value of months.

int years = 0;
int months = 0;
int counter = 1;

printf("Enter years ");
scanf("%d", &years);

months = years * 12;
printf("Months is %d ", months);

printf("Months: ");

do {
    printf("%d ", counter); //Month 1,2,3,4........24 up to full amount that was converted from years//
    counter++;
} while (counter <= months);

return 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM