forked from swapnanildutta/Hackerrank-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into diagonal-difference
- Loading branch information
Showing
32 changed files
with
1,988 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
Objective | ||
In this challenge, we review some basic concepts that will get you started with this series. You will need to use the same (or similar) syntax to read input and write output in challenges throughout HackerRank. Check out the Tutorial tab for learning materials and an instructional video! | ||
Task | ||
To complete this challenge, you must save a line of input from stdin to a variable, print Hello, World. on a single line, and finally print the value of your variable on a second line. | ||
You've got this! | ||
Note: The instructions are Java-based, but we support submissions in many popular languages. You can switch languages using the drop-down menu above your editor, and the | ||
variable may be written differently depending on the best-practice conventions of your submission language. | ||
Input Format | ||
A single line of text denoting | ||
(the variable whose contents must be printed). | ||
Output Format | ||
Print Hello, World. on the first line, and the contents of | ||
on the second line. | ||
Sample Input | ||
Welcome to 30 Days of Code! | ||
Sample Output | ||
Hello, World. | ||
Welcome to 30 Days of Code! | ||
Explanation | ||
On the first line, we print the string literal Hello, World.. On the second line, we print the contents of the | ||
variable which, for this sample case, happens to be Welcome to 30 Days of Code!. If you do not print the variable's contents to stdout, you will not pass the hidden test case. | ||
*/ | ||
|
||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <math.h> | ||
#include <stdlib.h> | ||
|
||
int main() { | ||
// Declare a variable named 'input_string' to hold our input. | ||
char input_string[105]; | ||
|
||
// Read a full line of input from stdin and save it to our variable, input_string. | ||
scanf("%[^\n]", input_string); | ||
|
||
// Print a string literal saying "Hello, World." to stdout using printf. | ||
printf("Hello, World.\n"); | ||
printf("%s",input_string); | ||
// TODO: Write a line of code here that prints the contents of input_string to stdout. | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Objective | ||
Today, we're discussing data types. Check out the Tutorial tab for learning materials and an instructional video! | ||
Task | ||
Complete the code in the editor below. The variables , , and | ||
are already declared and initialized for you. | ||
You must: | ||
Declare variables: one of type int, one of type double, and one of type String. | ||
Read lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your variables. | ||
Use the operator to perform the following operations: | ||
Print the sum of plus your int variable on a new line. | ||
Print the sum of plus your double variable to a scale of one decimal place on a new line. | ||
Concatenate with the string you read as input and print the result on a new line. | ||
Note: If you are using a language that doesn't support using for string concatenation (e.g.: C), you can just print one variable immediately following the other on the same line. The string provided in your editor must be printed first, immediately followed by the string you read as input. | ||
Input Format | ||
The first line contains an integer that you must sum with. | ||
The second line contains a double that you must sum with. | ||
The third line contains a string that you must concatenate with. | ||
Output Format | ||
Print the sum of both integers on the first line, the sum of both doubles (scaled to decimal place) on the second line, and then the two concatenated strings on the third line. | ||
Sample Input | ||
12 | ||
4.0 | ||
is the best place to learn and practice coding! | ||
Sample Output | ||
16 | ||
8.0 | ||
HackerRank is the best place to learn and practice coding! | ||
Explanation | ||
When we sum the integers and , we get the integer.When we sum the floating-point numbers and , we get .When we concatenate HackerRank with is the best place to learn and practice coding!, we get HackerRank is the best place to learn and practice coding!. | ||
You will not pass this challenge if you attempt to assign the Sample Case values to your variables instead of following the instructions above and reading input from stdin. | ||
*/ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <math.h> | ||
#include <stdlib.h> | ||
|
||
int main() { | ||
int i = 4; | ||
double d = 4.0; | ||
char s[] = "HackerRank "; | ||
|
||
|
||
// Declare second integer, double, and String variables. | ||
int integer2; | ||
double double2; | ||
char string2[100]; | ||
|
||
// Read and save an integer, double, and String to your variables. | ||
scanf("%d\n",&integer2); | ||
scanf("%lf\n",&double2); | ||
fgets(string2, 100, stdin); | ||
|
||
// Print the sum of both integer variables on a new line. | ||
printf("%d\n",i+integer2); | ||
|
||
// Print the sum of the double variables on a new line. | ||
printf("%.1lf\n",d+double2); | ||
|
||
// Concatenate and print the String variables on a new line | ||
// The 's' variable above should be printed first. | ||
printf("%s%s",s,string2); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
Objective | ||
In this challenge, you'll work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video! | ||
Task | ||
iven the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost. | ||
Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result! | ||
Input Format | ||
There are lines of numeric input: | ||
The first line has a double, (the cost of the meal before tax and tip). | ||
The second line has an integer, (the percentage of being added as tip). | ||
The third line has an integer, (the percentage of being added as tax). | ||
Output Format | ||
Print the total meal cost, where is the rounded integer result of the entire bill (with added tax and tip). | ||
Sample Input | ||
12.00 | ||
20 | ||
8 | ||
Sample Output | ||
15 | ||
Explanation | ||
Given: | ||
, , Calculations: | ||
We round to the nearest dollar (integer) and then print our result, . | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
char* readline(); | ||
|
||
// Complete the solve function below. | ||
void solve(double meal_cost, int tip_percent, int tax_percent) { | ||
double tip_cost= meal_cost * tip_percent / 100; | ||
double tax_cost= meal_cost * tax_percent / 100; | ||
double total_cost= (meal_cost + tip_cost + tax_cost); | ||
printf("%.0lf",total_cost); | ||
} | ||
|
||
int main() | ||
{ | ||
char* meal_cost_endptr; | ||
char* meal_cost_str = readline(); | ||
double meal_cost = strtod(meal_cost_str, &meal_cost_endptr); | ||
|
||
if (meal_cost_endptr == meal_cost_str || *meal_cost_endptr != '\0') { exit(EXIT_FAILURE); } | ||
|
||
char* tip_percent_endptr; | ||
char* tip_percent_str = readline(); | ||
int tip_percent = strtol(tip_percent_str, &tip_percent_endptr, 10); | ||
|
||
if (tip_percent_endptr == tip_percent_str || *tip_percent_endptr != '\0') { exit(EXIT_FAILURE); } | ||
|
||
char* tax_percent_endptr; | ||
char* tax_percent_str = readline(); | ||
int tax_percent = strtol(tax_percent_str, &tax_percent_endptr, 10); | ||
|
||
if (tax_percent_endptr == tax_percent_str || *tax_percent_endptr != '\0') { exit(EXIT_FAILURE); } | ||
|
||
solve(meal_cost, tip_percent, tax_percent); | ||
|
||
return 0; | ||
} | ||
|
||
char* readline() { | ||
size_t alloc_length = 1024; | ||
size_t data_length = 0; | ||
char* data = malloc(alloc_length); | ||
|
||
while (true) { | ||
char* cursor = data + data_length; | ||
char* line = fgets(cursor, alloc_length - data_length, stdin); | ||
|
||
if (!line) { break; } | ||
|
||
data_length += strlen(cursor); | ||
|
||
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } | ||
|
||
size_t new_length = alloc_length << 1; | ||
data = realloc(data, new_length); | ||
|
||
if (!data) { break; } | ||
|
||
alloc_length = new_length; | ||
} | ||
|
||
if (data[data_length - 1] == '\n') { | ||
data[data_length - 1] = '\0'; | ||
} | ||
|
||
data = realloc(data, data_length); | ||
|
||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Objective | ||
In this challenge, we're getting started with conditional statements. Check out the Tutorial tab for learning materials and an instructional video! | ||
Task | ||
Given an integer , , perform the following conditional actions: | ||
If is odd, print Weird | ||
If is even and in the inclusive range of to , print Not Weird | ||
If is even and in the inclusive range of to , print Weird | ||
If is even and greater than , print Not Weird | ||
Complete the stub code provided in your editor to print whether or not is weird. | ||
Input Format | ||
A single line containing a positive integer, . | ||
Constraints | ||
Output Format | ||
Print Weird if the number is weird; otherwise, print Not Weird. | ||
Sample Input 0 | ||
3 | ||
Sample Output 0 | ||
Weird | ||
Sample Input 1 | ||
24 | ||
Sample Output 1 | ||
Not Weird | ||
Explanation | ||
Sample Case 0: | ||
is odd and odd numbers are weird, so we print Weird. | ||
Sample Case 1: | ||
and is even, so it isn't weird. Thus, we print Not Weird. | ||
*/ | ||
|
||
|
||
#include <assert.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
// If is odd, print Weird | ||
// If is even and in the inclusive range of to , print Not Weird | ||
// If is even and in the inclusive range of to , print Weird | ||
// If is even and greater than , print Not Weird | ||
|
||
|
||
|
||
int main() | ||
{ | ||
int n; | ||
scanf("%d",&n); | ||
|
||
if(n%2 == 1 || (n%2 == 0 && n >= 6 && n <= 20)) | ||
printf("Weird\n"); | ||
else if(n%2 == 0 && ( (n >= 2 && n <=5) || n > 20)) | ||
printf("Not Weird\n"); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Objective | ||
In this challenge, we're going to use loops to help us do some simple math. Check out the Tutorial tab to learn more. | ||
Task | ||
Given an integer, , print its first multiples. Each multiple (where ) should be printed on a new line in the form: n x i = result. | ||
Input Format | ||
A single integer, . | ||
Constraints | ||
Output Format | ||
Print lines of output; each line (where ) contains the of in the form: | ||
n x i = result. | ||
Sample Input | ||
2 | ||
Sample Output | ||
2 x 1 = 2 | ||
2 x 2 = 4 | ||
2 x 3 = 6 | ||
2 x 4 = 8 | ||
2 x 5 = 10 | ||
2 x 6 = 12 | ||
2 x 7 = 14 | ||
2 x 8 = 16 | ||
2 x 9 = 18 | ||
2 x 10 = 20 | ||
*/ | ||
//The prewritten code on the site made it unnecessarily difficult | ||
|
||
|
||
#include <stdio.h> | ||
|
||
int main() | ||
{ | ||
// Two variables n for the input, i for the loop | ||
int n,i; | ||
scanf("%d",&n); | ||
for(i = 1;i <= 10;i++) | ||
{ | ||
printf("%d x %d = %d\n",n,i,n*i); | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.