- #1
samarth0157
- 1
- 0
GIVE A program of c to input a name and print the name if the first char of name is a or b else print error message.
samarth0157 said:GIVE A program of c to input a name and print the name if the first char of name is a or b else print error message.
To input a name in a C program, you can use the scanf()
function. This function takes in user input and stores it in a variable. For example, if you want to input a name and store it in a variable called name
, you can use the following code:
scanf("%s", name);
To check if the input name starts with the letter A or B, you can use an if
statement. First, you need to access the first letter of the input name using array indexing. Then, you can compare it with the letters A and B using the ==
operator. For example:
if(name[0] == 'A' || name[0] == 'B') {
// input name starts with A or B
}
If the input name does not start with the letter A or B, you can print an error message using the printf()
function. For example:
printf("Error: Input name does not start with A or B");
To handle multiple inputs in a C program, you can use a while
loop. This loop will continue to run until the user inputs a specific value, such as -1
. Inside the loop, you can use the scanf()
function to take in multiple inputs and perform the necessary operations. For example:
while(input != -1) {
scanf("%d", &input);
// perform operations on input
}
To end a C program, you can use the return
statement. This statement will exit the program and return control to the operating system. For example, if you want to end the program after printing the input name, you can use the following code:
printf("%s", name);
return 0;