coder18s,blogspot.com

Powered by Blogger.

7.For Loop in C Hackerrank Solution

Task

For each integer  in the interval  (given as input) :

  • If , then print the English representation of it in lowercase. That is "one" for , "two" for , and so on.
  • Else if  and it is an even number, then print "even".
  • Else if  and it is an odd number, then print "odd".

Sample Input

8
11

Sample Output

eight
nine
even
odd
                 Program Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{   
    int a, b;
    scanf("%d\n%d", &a, &b);
   
    char *str[] = {"one""two""three""four""five""six""seven",  "eight""nine"};
    
    for (int i=a; i<=b; i++)// i= 8 
    {
            if (i <= 9
        {
                printf ("%s\n", str[i-1]);
        }
            else if(i%2==0)
        {
                printf ("even\n");
        }
        else 
        {
            printf("odd\n");
        }
    }
    
    return 0;
}

8.Sum of Digits of a Five Digit Number Hackerrank Solution

Task

Given a five digit integer, print the sum of its digits.

Sample Input 

10564

Sample Output 

16
                Program Code 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    
     int n,rem,sum = 0;
    scanf("%d", &n);
 
    while(n > 0)
    {
        rem = n %10;
        sum = sum + rem;
        n = n/10;
    }
    printf("%d",sum);
    return 0;
}

9.Bitwise Operators in C Hackerrank Solution

Task

Print the maximum values for the andor and xor comparisons, each on a separate line.

Example

The results of the comparisons are below:

a  b  and or  xor
1  2   0    3   3
1  3   1    3   2
2  3   2    3   1

Sample Input 0

5    4

Sample Output 0

2
3
3
                Program Code

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

int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    int mxAnd = 0, mxOr = 0, mxXor = 0;
    
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            if(mxAnd < (i & j) && (i & j) < k)
                mxAnd = i & j;
            if(mxOr < (i | j) && (i | j) < k)
                mxOr = i | j;
            if(mxXor < (i ^ j) && (i ^ j) < k)
                mxXor = i ^ j;
        }
    }
    printf("%d\n", mxAnd);
    printf("%d\n", mxOr);
    printf("%d\n", mxXor);
 
    return 0;
}

 

1D Arrays in C Hackerrank Solution

Task

Print the sum of the integers in the array.

Sample Input 0

6
16 13 7 2 1 12 

Sample Output 0

51

Sample Input 1

7
1 13 15 20 12 13 2 

Sample Output 1

76
                Program Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n,i,j,arr[1000],sum = 0;
    scanf("%d",&n);
    
    for(i=0;i<n;i++)
    {
        scanf("%d ",&arr[i]);
    }
         for(i=0;i<n;i++)
    {
        sum = sum+arr[i];
    }
          printf("%d ",sum);
    return 0;
}

Array Reversal HackerRank Solution in C

 TASK

Given an array, of size , reverse it.

Example: If array, , after reversing it, the array should be, .

Input Format

The first line contains an integer, , denoting the size of the array. The next line contains  space-separated integers denoting the elements of the array.

Constraints


, where  is the  element of the array.


Sample Input 0

6
16 13 7 2 1 12 

Sample Output 0

12 1 2 7 13 16 
              PROGRAM CODE
#include <stdio.h>
#include <stdlib.h>
void reversearr(int arr[], int start,int end)
{
    while(start<end)
    {
        int temp =arr[start];
        arr[start]=arr[end];
        arr[end]= temp;
        start++;
        end--;
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    reversearr(arr,0,n-1);
    for(int i=0;i<n;i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}