Code For Alls..!

get the code!!

Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Monday 9 October 2017

                                          Interlace odd / even from A to B.
Interlace odd / even from A to B.
Two numbers A and B are passed as input. The program must print the odd numbers from A to B (inclusive of A and B) interlaced with the even numbers from B to A.
Input Format:
The first line denotes the value of A.
The second line denotes the value of B.
Output Format:
The odd and even numbers interlaced, each separated by a space.
Boundary Conditions:
1 <= A <= 9999999
A <  B <= 9999999
Example Input/Output 1:
Input:
5
11
Output:
5 10 7 8 9 6 11
Explanation:
The odd numbers from 5 to 11 are 5 7 9 11
The even numbers from 11 to 5 (that is in reverse direction) are 10 8 6
So these numbers are interlaced to produce 5 10 7 8 9 6 11
Example Input/Output 2:
Input:
4
14
Output:
14 5 12 7 10 9 8 11 6 13 4
Explanation:
The odd numbers from 4 to 14 are 5 7 9 11 13
The even numbers from 14 to 4 (that is in reverse direction) are 14 12 10 8 6 4
So these numbers are interlaced to produce 14 5 12 7 10 9 8 11 6 13 4
(Here as the even numbers count are more than the odd numbers count we start with the even number in the output)
Example Input/Output 3:
Input:
3
12
Output:
3 12 5 10 7 8 9 6 11 4
Explanation:
The odd numbers from 3 to 12 are 3 5 7 9 11
The even numbers from 12 to 3 (that is in reverse direction) are 12 10 8 6 4
So these numbers are interlaced to produce 3 12 5 10 7 8 9 6 11 4
program:
#include<stdio.h>
#include <stdlib.h>

int main()
{
int a,b;
scanf("%d\n%d",&a,&b);
int o,e;
if(b%2==0)
e=b;
else
e=b-1;
if(a%2==1)
o=a;
else
o=a+1;
int p=0,t=b-a+1;
while(p<t)
{
    if(a%2==0&&b%2==0)
    {
        if(e>=a)
        {
            printf("%d ",e);
            p++;
            e=e-2;
        }
        if(o<=b)
        {
            printf("%d ",o);
            p++;
            o=o+2;
        }
    }
    else
    {
        if(o<=b)
        {
            printf("%d ",o);
            p++;
            o=o+2;
        }
        if(e>=a)
        {
            printf("%d ",e);
            p++;
            e=e-2;
        }
    }
}
}

Saturday 7 October 2017

                                            Print Calendar Month in Words
The program must accept an integer value N and print the corresponding calendar month in words.
1 - January, 2 - February, .... , 11 - November, 12 - December
If any value apart from 1 to 12 is passed as input, the program must print "Invalid Input".
Input Format:
The first line denotes the value of N.
Output Format:
The first line contains the output as per the description. (The output is CASE SENSITIVE).
Boundary Conditions:
1 <= N <= 12
Example Input/Output 1:
Input:
5
Output:
May
Example Input/Output 2:
Input:
12
Output:
December
Example Input/Output 3:
Input:
105
Output:
Invalid Input
program:
#include<stdio.h>
#include <stdlib.h>

int main()
{
int n;
scanf("%d",&n);
    switch(n)
    {
        case 1:
            printf("January");
            break;
        case 2:
            printf("February");
            break;
        case 3:
            printf("March");
            break;
        case 4:
            printf("April");
            break;
        case 5:
            printf("May");
            break;
        case 6:
            printf("June");
            break;
        case 7:
            printf("July");
            break;
        case 8:
            printf("August");
            break;
        case 9:
            printf("September");
            break;
        case 10:
            printf("October");
            break;
        case 11:
            printf("November");
            break;
        case 12:
            printf("December");
            break;
        default:
            printf("Invalid Input");
    }

}

Friday 6 October 2017

Given a positive integer N as the input, the program must print yes if N is a perfect number. Else no must be printed.
Input Format:
The first line contains N.
Output Format:
The first line contains yes or no
Boundary Conditions:
1 <= N <= 999999
Example Input/Output 1:
Input:
6
Output:
yes
Example Input/Output 2:
Input:
8
Output:
no

Program:
#include<stdio.h>
#include <stdlib.h>

int main()
{
int n,s=0;
scanf("%d",&n);
for(int i=1;i<=n/2;i++)
{
    if(n%i==0)
    {
        s=s+i;
    }
}
if(s==n)
{
    printf("yes");
}
else
{
    printf("no");
}
}

Monday 25 September 2017

                                                   Pattern Printing - Diamond Numbers
Pattern Printing - Diamond Numbers
Given an integer N as the input, print the pattern as given in the Example Input/Output section.
Input Format:
The first line contains N.
Output Format:
2N-1 lines containing the desired pattern.
Boundary Conditions:
2 <= N <= 50
Example Input/Output 1:
Input:
3
Output:
0 0 1 0 0
0 2 0 8 0
3 0 0 0 7
0 4 0 6 0
0 0 5 0 0
Example Input/Output 2:
Input:
5
Output:
0 0 0 0 1 0 0 0 0
0 0 0 2 0 16 0 0 0
0 0 3 0 0 0 15 0 0
0 4 0 0 0 0 0 14 0
5 0 0 0 0 0 0 0 13
0 6 0 0 0 0 0 12 0
0 0 7 0 0 0 11 0 0
0 0 0 8 0 10 0 0 0
0 0 0 0 9 0 0 0 0
c program:
#include<stdio.h>
#include <stdlib.h>

int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<2*n-1;i++)
{
    for(int j=0;j<2*n-1;j++)
    {
        if(i==n-j-1)
        {
            printf("%d ",i+1);
        }
        else if(i==j-n+1)
        {
            printf("%d ",4*n-i-3);
        }
        else if(i==j+n-1)
        {
            printf("%d ",i+1);
        }
        else if(n-1==i+j+2-2*n)
        {
          printf("%d ",4*n-i-3);  
        }
        else
        {
            printf("0 ");
        }
    }
    printf("\n");
}
}

Tuesday 19 September 2017

                                             Day 6: Let's Review-hackerrank-solution
Day 6: Let's Review-hackerrank-solution
Task
Given a string, SS, of length NN that is indexed from 00 to N−1N−1, print its even-indexed and odd-indexed characters as 22 space-separated strings on a single line (see the Sample below for more detail).
Note: 00 is considered to be an even index.
Input Format
The first line contains an integer, TT (the number of test cases).
Each line ii of the TT subsequent lines contain a String, SS.
Constraints
  • 1≤T≤101≤T≤10
  • 2≤length of S≤100002≤length of S≤10000
Output Format
For each String SjSj (where 0≤j≤T−10≤j≤T−1), print SjSj‘s even-indexed characters, followed by a space, followed by SjSj‘s odd-indexed characters.
Sample Input
2
Hacker
Rank
Sample Output
Hce akr
Rn ak
Explanation
Test Case 0: S=“Hacker”
S[0]=“H”
S[1]=“a”
S[2]=“c”
S[3]=“k”
S[4]=“e”
S[5]=“r”
The even indices are 0, 2, and 4, and the odd indices are 1, 3, and 5. We then print a single line of 2 space-separated strings; the first string contains the ordered characters from S‘s even indices (Hce), and the second string contains the ordered characters from S‘s odd indices (akr).
Test Case 1: S=“Rank”
S[0]=“R”
S[1]=“a”
S[2]=“n”
S[3]=“k”
The even indices are 0 and 2, and the odd indices are 1 and 3. We then print a single line of 2 space-separated strings; the first string contains the ordered characters from S‘s even indices (Rn), and the second string contains the ordered characters from S‘s odd indices (ak).


C program:

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

int main() {

   int t;
    scanf("%d",&t);
    while(t)
        {
        char a[10000];
        scanf("%s",a);
        int i=0;
        while(a[i]!='\0')
        {if(i%2==0)
            printf("%c",a[i]);
        i++;}
       i=0;
        printf(" ");
        while(a[i]!='\0')
        {if(i%2!=0)
            printf("%c",a[i]);
        i++;}
        t--;
        printf("\n");
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}
                                               Day 5: Loops-hackerrank-solution
Day 5: Loops-hackerrank-solution
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
  •  2<=n<=20
Output Format
Print 10 lines of output; each line  (where 1<=i<=10) contains the n x i 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

C code:

#include <stdio.h>
int main(){
    int N,i; 
    scanf("%d",&N);
    for(i=1;i<=10;i++)
    printf("%d x %d = %d\n",N,i,N*i);
    return 0;
}



Sunday 17 September 2017

                                                Matrix Row Sum (Id-3108)
Matrix Row Sum (Id-3108)
Given a R*C matrix (R - rows and C- Columns), print the sum of the values in each row as the output.
Input Format:
First line will contain R and C separated by a space.
Next R lines will contain C values, each separated by a space.
Output Format:
R lines representing the sum of elements of rows 1 to R.
Boundary Conditions:
2 <= R, C <= 50
Example Input/Output 1:
Input:
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
10
26
42
58
 C program:
#include<stdio.h>
#include <stdlib.h>

int main()
{
int m,n,t,s;
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++)
{
    s=0;
    for(int j=0;j<n;j++)
{
   scanf("%d",&t);
   s+=t;
}
printf("%d\n",s);
}
}

Saturday 16 September 2017

                                                Head Count - Birds and Animals
Head Count - Birds and Animals

In a zoo there are some birds and animals. All birds have two legs and all animals have four legs.
GIven the head count and leg count of both birds and animals taken together, the program must print the head count of birds and animals separated by a space as output.
Input Format:
First line will contain the integer value H representing the head count of both birds and animals taken together.
Second line will contain the integer value L representing the leg count of both birds and animals taken together.
Output Format:
First line will contain the integer values of the head count of birds and animals separated by a space.
Constraints:
0 < H < 1000
1 < L < 2000
Sample Input/Output:
Example 1:
Input:
27
84
Output:
12 15
Explanation:
There are 12 birds and 15 animals.
Example 2:
Input:
114
256
Output:
100 14
Explanation:
There are 100 birds and 14 animals.
C program:
#include<stdio.h>
#include <stdlib.h>

int main()
{
int h,l;
scanf("%d%d",&h,&l);
int a=l/2-h;
printf("%d %d",h-a,a);
}

Thursday 14 September 2017

                                                                  Day 2: Operators



Task
Given 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 Formata
Print The total meal cost is totalCost dollars., where  is the rounded integer result of the entire bill ( with added tax and tip).
Sample Input
12.00
20
8
Sample Output
The total meal cost is 15 dollars.
Explanation
Given:
, , 
Calculations:  



We round  to the nearest dollar (integer) and then print our result:
The total meal cost is 15 dollars.

C Program:

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

int main() {
         float mc,tp,tx,tc;
    int tip,tax,l,m;
    scanf("%f\n%d\n%d",&mc,&tip,&tax);
    tp=mc*tip/100;
    tx=mc*tax/100;
    tc=mc+tp+tx;
    l=tc;
    m=(tc*100)-(l*100);
    l=(m>=50)?l+1:l;
    printf("The total meal cost is %d dollars.",l);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}