1

I am confused on how I should format the for loop. I want to print "Happy Birthday" as many times as the user inputs.

#include <stdio.h>

int main(){
    int age;
    int bdays;

    printf("What is your age?");
    scanf("%d", age); 

    for(age = 0; **`age ????`**; age++){
        printf("Happy Birthday\n");
    }
}

3 Answers 3

4

You have to iterate over a loop control variable

#include <stdio.h>

int main()
{
    int age;
    int bdays;

    printf("What is your age?\n");
    scanf("%d", &age);

    for (int i = 0; i < age; i++) {
        printf("Happy Birthday\n");
    }
}

And do not forget to use pointer in scanf()

Sign up to request clarification or add additional context in comments.

Comments

0

you don't know how many times the user will input an age, so instead of a for loop consider a while condition? I will use age 0 as an exit, you can do it in different ways, I doubt you want to print happy birthday every single time so I think you need some sort of condition to meet?

also small nitpick, you want to give scanf the pointer to the variable so it can modify it, so in scanf you want &age instead of age

put the whole thing

 while(age!=0){

 printf("What is your age?");
 scanf("%d", &age);

 printf("Happy Birthday\n");

 }

 return 0;

this way you can do it over and over again, if you input 0 you can exit the program at any time

edit: seems I misunderstood, I thought you meant as many times as the user gives an input not as many times as the age... so the above answer is correct and I am wrong in that case.

2 Comments

For instance, if the user puts the age 25, the for loop will print happy birthday 25 times and give the year beside it from "year 1" to "year 25"
the other answer from vsh gives you that so I guess ignore what I said
0
#include<stdio.h>

main()
{
    int n,i;
    printf("Enter how many time you want to print happy birthday");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    printf("\nHAPPY BIRTHDAY");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.