We are going to find the Day 1 Data Types Hackerrank Solution in C programming with complete logic and explanation. So we have a problem statement and write a program to find the data type's day 1 solution in c language with output. This is the second solution of 30 days of code hackerrank challenges.
For double also have to repeat the same process. and for string first, we have to print the already initialize string which is "HackerRank " and after the "HackerRank," the user string will be shown. for this problem, I am going to explain the program step by step.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i = 4;
double d = 4.0;
char s[] = "HackerRank ";
int i2;
double d2;
char s2[100]; // this is not scalable for input of unknown size
// Read inputs from stdin
scanf("%d", & i2);
scanf("%lf", & d2);
scanf("%*[\n] %[^\n]", s2);
// Print outputs to stdout
printf("%d\n", i + i2);
printf("%.01lf\n", d + d2);
printf("%s%s", s, s2);
return 0;
}
Explanation of Data Types Hackerrank Solution
as we know that int i = 4; is already defined so we have to take another integer j, after that we will take user input and store the value in j then we will add i + j value like below.
int i = 4;
int j = 12;
cout << i + j <<endl;
So the i + j value will be added and print the sum of both numbers.
Now repeat the same process for the double data type so add both double value fixed value and user input value and print the output but remember as you know that the double-precision value is more than 1.
So this is a problem for avoiding this type of problem we can fix the double value by using set precision(value) means how many digits you want after the pointer and fraction number. Here you can see that we fixed the value to one that we use set precision(1).
double d = 4.0;
double e = 4.0;
cout<<fixed<<setprecision(1)<<d<<endl;
Now next step is for the string to be very simple we can print the first string and then the user input sting but we have one problem we can not accept the space user input string so for that we use getline(cin,t). if you don't know about getline() function see the example. Day 0 Hello World. Hackerrank Solution.
string s = " HackerRank ";
string t ; // user input.
cout<<s<<t;
I hope now you can understand the full problem if you have any queries send an email. You can directly submit your solution copy the coloured area code and paste it into Hackerrank Editor.
0 Comments: