HackerRank solutions in C (easy)

HackerRank is the place where you can make yourself perfect in coding. But in case you’re finding it difficult to solve those questions, you can use these solutions. All these solutions are successfully compiled and passed all the tests.

Playing with String


#include <stdio.h>
#include <string.h>

int main()
{

char ch,s[20];
char sen[100];
scanf("%cn", &ch);
scanf("%sn",s);
scanf("n");
scanf("%[^n]%*c", sen);
printf("%cn",ch);
printf("%sn",s);
printf("%s",sen);
return 0;
}

Sum and Difference of two numbers:

Task:

Your task is to take two numbers of int data type, two numbers of float data type as input and output their sum:

  1. Declare 4 variables: two of type int and two of type float.
  2. Read 2 lines of input from stdin (according to the sequence given in the ‘Input Format’ section below) and initialize your 4 variables.
  3. Use the + and – operator to perform the following operations:
    • Print the sum and difference of two int variable on a new line.
    • Print the sum and difference of two float variable rounded to one decimal place on a new line.

Input Format:

The first line contains two integers.
The second line contains two floating-point numbers.

Output Format

Print the sum and difference of both integers separated by a space on the first line, and the sum and difference of both float (scaled to 1 decimal place) separated by a space on the second line.


#include <stdio.h>

int main()
{
int a,b;
float c,d;
scanf("%d %d", &a, &b);
scanf("%f %f", &c, &d);
printf("%d %dn", a + b, a - b);
printf("%.1f %.1fn", c + d, c - d);
return 0;
}

Functions in C


#include <stdio.h>

int max_of_four(int a, int b, int c, int d);

int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);

return 0;
}
int max_of_four(int a, int b, int c, int d)
{
int result;

if (a > b && a>c && a>d)
result = a;
if (b>c && b>d && b>a)
result=b;
if (c>d && c>a && c>b)
result=c;
if (d>a && d>b && d>c)
result=d;
return result;
}

Pointer in C


#include <stdio.h>

void update(int *a,int *b) {
// Complete this function
int add, sub;
add = *a + *b;
if (*a > *b) {
sub = *a - *b;
}
else sub = *b - *a;
*a = add;
*b = sub;
}

int main() {
int a, b;
int *p = &a, *q = &b;

scanf("%d %d", &a, &b);
update(p, q);
printf("%dn%d", a, b);

return 0;
}

Conditional Statement


#include <stdio.h>

static char *strings[] = {"one","two","three","four","five",
"six","seven","eight","nine"};
int main()
{
int n = 0;
if (scanf("%d",&n) < 1)
return 1;

if (n >= 1 && n <= 9)
printf("%s",strings[n-1]);
else
printf("Greater than 9");

return 0;
}

For loop in C


#include <stdio.h>
int main()
{
int a, b;
scanf("%dn%d", &a, &b);
// Complete the code.
for (int i=a; i<b+1; i++) {
switch(i) {
case 1: printf("onen"); break;
case 2: printf("twon"); break;
case 3: printf("threen"); break;
case 4: printf("fourn"); break;
case 5: printf("fiven"); break;
case 6: printf("sixn"); break;
case 7: printf("sevenn"); break;
case 8: printf("eightn"); break;
case 9: printf("ninen"); break;
default:
if (i % 2)
printf("oddn");
else
printf("evenn");
}
}

return 0;
}

Sum of Digits of a Five Digit Number


#include <stdio.h>

int main()
{

int n;
scanf("%d", &n);
int sum = 0;
//Complete the code to calculate the sum of the five digits on n.
while (n>0) {
sum += (n%10);
n=n/10;
}
printf("%dn",sum);
return 0;
}

Bitwise Operator


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.


void calculate_the_maximum(int n, int k) {
int maxAnd = 0;
int maxOr = 0;
int maxXor = 0;

for (int i=1; i<=n; i++) {
for (int j=i+1; j<=n; j++) {
if (((i&j) > maxAnd) && ((i&j) < k)) {
maxAnd = i&j;
}
if (((i|j) > maxOr) && ((i|j) <k)) {
maxOr = i|j;
}
if (((i^j) > maxXor) && ((i^j) < k)) {
maxXor = i^j;
}
}
}

printf("%dn%dn%dn", maxAnd, maxOr, maxXor);
}

int main() {
int n, k;

scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);

return 0;
}

Calculate the Nth term


#include <stdio.h>
//Complete the following function.

int find_nth_term(int n, int a, int b, int c) {
//Write your code here.
int sum=0;
for (int i; i<n; i++) {
sum=find_nth_term(n-1,a,b,c)+find_nth_term(n-2,a,b,c)+find_nth_term(n-3,a,b,c); //finding the sum as per given in the explanation
}
return sum;
}

int main() {
int n, a, b, c;

scanf("%d %d %d %d", amp;&n, &a, &b, &c);
int ans = find_nth_term(n, a, b, c);

printf("%d", ans);
return 0;
}

student Marks Sum


#include <stdio.h>

//Complete the following function.

int marks_summation(int* marks, int number_of_students, char gender) {
//Write your code here.
int sum = 0;
for (int i = !(gender == 'b'); i < number_of_students; i = i + 2)
// ! changes anything true to 0 and 0 to 1.
sum += *(marks + i);

return sum;
}

int main() {
int number_of_students;
char gender;
int sum;

scanf("%d", &number_of_students);
int *marks = (int *) malloc(number_of_students * sizeof (int));

for (int student = 0; student < number_of_students; student++) {
scanf("%d", (marks + student));
}

scanf(" %c", &gender);
sum = marks_summation(marks, number_of_students, gender);
printf("%d", sum);
free(marks);

return 0;
}

Box through a tunnel


#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 41

struct box
{
int length, width, height;
};

typedef struct box box;

int get_volume(box b) {
int volume= b.length*b.width*b.height;
return volume;
}

int is_lower_than_max_height(box b) {
if (b.height<41) {
return 1;
}
else {
return 0;
}
}

int main()
{
int n;
scanf("%d", &n);
box *boxes = malloc(n * sizeof(box));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
}
for (int i = 0; i < n; i++) {
if (is_lower_than_max_height(boxes[i])) {
printf("%dn", get_volume(boxes[i]));
}
}
return 0;
}

So, that’s all the codes for C of easy level. If you want to see medium level, then click here.

To get regular notification subscribe to our newsletter.

Hey, do you want to know the logic behind these codes?
If yes then tell me that on the comment section.

Snehasish Konger
Snehasish Konger

Hey, I'm Snehasish Konger, the guy behind this website.
Read my blogs from here-

Articles: 112

Leave a Reply

Your email address will not be published.

You cannot copy content of this page