In this tutorial, we are going to solve the HackerRank Hello World in c problem or provide a solution. in this challenge or problem, we need to print the Hello, World! text on a single line, and then we need to print the value of a string using the function printf(). so using this problem we will learn how to print the value of any variable and a static string value on the screen in the c programming.
HackerRank Hello World! in C Programming solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
char s[100];
scanf("%[^\n]%*c", &s);
printf("Hello, World!\n");
printf("%s",s);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Explanation
Here in the above code, we have used a string of length 100 to hold the value that we are going to scan or read from the input screen and scan a value from the user input screen using the scanf() function and then we printed the value Hello, World! in first line using the printf() function. and then we used the \n code to go to next line to print another value
After that, we again used the printf() function to print the value of a variable.
Second solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
char s[100];
fgets(s, sizeof(s), stdin);
printf("Hello, World!\n%s", s);
return 0;
}
Explanation
In this solution first, we included the necessary header file stdio.h, string.h, math.h, stdlib.h, after that in the main() function we defined a character string of length 100 to hold the string value that we will read from the input screen. after that, we use the fgets() function to read the value from the user input screen.
And then we used the printf() function to print the Hello, World! string on the output screen and then \n to go to the next line and %s format specifier to print the value of string s.