coder18s,blogspot.com

Powered by Blogger.

1."Hello World!" in C Hackerrank Solution

Task

This challenge requires you to print  on a single line, and then print the already provided input string to stdout . If you are not familiar with C, you may want to read about the printf() command.

Example

The required output is:

Hello, World! 

Life is beautiful  

                  Program Code


#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%s", s);    
    return 0;
}






2. Playing With Characters Hackerrank Solution

 Task

You have to print the character, , in the first line. Then print  in next line. In the last line print the sentence.

Sample Input 

C
Language
Welcome To C!!

Sample Output 

C
Language
Welcome To C!!
                        Program Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    char ch , str[50] ,sen[100];
    scanf("%c",&ch);
    printf("%c\n",ch);
    scanf("%s",&str);
    scanf("\n");
    printf("%s\n",str);
    gets(sen);
    printf("%s\n",sen);   
    return 0;
}

3. Sum and Difference of Two Numbers Hackerrank Solution

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  variables: two of type int and two of type float.
  2. Read  lines of input from stdin (according to the sequence given in the 'Input Format' section below) and initialize your  variables.
  3. Use the  and  operator to perform the following operations:
  4. Print the sum and difference of two int variable on a new line.
  5. Print the sum and difference of two float variable rounded to one decimal place on a new line.
 Sample Input
10             4
4.0          2.0

Sample Output

1        4        6
6.0              2.0
     Program Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    int a,b;
    float x,y;
    scanf( "%d %d",&a,&b);
    scanf( "%f %f",&x,&y);
    printf("%d %d\n",a+b,a-b);
    printf("%.1f %.1f",x+y,x-y);
    
    
    return 0;
}

4. Functions in C Hackerrank Solution

Task

Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.


Sample Input

3
4
6
5

Sample Output

6
                   Program Code
#include <stdio.h>
int max_of_four(int a, int b, int c, int d)
{
    if (a>b && a>c && a>d) 

return a;

else if (b>c && b>d) 

return b;

else if (c>d) 

return c;

else

return 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;
}

5.Pointers in C Hackerrank Solution

Task

Complete the function void update(int *a,int *b). It receives two integer pointers, int* a and int* b. Set the value of  to their sum, and  to their absolute difference. There is no return value, and no return statement is needed.

a' = a+b

b' = |a-b|


Sample Input

4

5
Sample Output
9
1
          Program Code
#include <stdio.h>

void update(int *a,int *b) {
    int x,y;
    x = *a + *b;
    y = *a - *b;
    *a = x;
    *b =abs(y); 
}

int main() {
    int a, b;
    int *pa = &a, *pb = &b;
    
    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);

    return 0;
}

6.Conditional Statements in C Hackerrank Solution

  Task

Given a positive integer denoting , do the following:

  • If , print the lowercase English word corresponding to the number (e.g., one for two for , etc.).
  • If , print Greater than 9.

Sample Input #01

8

Sample Output #01

eight

Sample Input #02

44

Sample Output #02

Greater than 9
                Program Code
#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>

int main()
{
    int n;
    scanf("%d",&n);
    if(n==1)
    {
        printf("one");
    }
    else if(n==2)
    {
        printf("two");
    }
    else if(n==3)
    {
        printf("three");
    }
    else if(n==4)
    {
        printf("four");
    }
    else if(n==5)
    {
        printf("five");
    }
    else if(n==6)
    {
        printf("six");
    }
    else if(n==7)
    {
        printf("seven");
    }
    else if(n==8)
    {
        printf("eight");
    }
    else if(n==9)
    {
        printf("nine");
    }
    else 
    {
        printf("Greater than 9");
    }
    return 0;
}