Code For Alls..!

get the code!!

Saturday 7 October 2017


Objective
Today, we're learning about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video!
Task
Given an array, , of  integers, print 's elements in reverse order as a single line of space-separated numbers.
Input Format
The first line contains an integer,  (the size of our array).
The second line contains  space-separated integers describing array 's elements.
Constraints
  • , where  is the  integer in the array.
Output Format
Print the elements of array  in reverse order as a single line of space-separated numbers.
Sample Input
4
1 4 3 2
Sample Output
2 3 4 1
Program:
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;


int main(){
    int n;
    cin >> n;
    vector<int> arr(n);
    for(int i = 0;i < n;i++){
       cin >> arr[i];
    }
       for(int i = n-1;i>=0;i--){
    cout<< arr[i]<<"\t";
       }
    return 0;
}

Friday 6 October 2017

Two string values S1 and S2 are passed as input. The program must print the output as S1 Technologies S2.
Input Format:
The first line denotes the value of S1.
The second line denotes the value of S2.
Output Format:
The first line contains the output as per the description. There must be exactly one space between the words.
Boundary Conditions:
Length of S1 is from 1 to 100.
Length of S2 is from 1 to 100.
Example Input/Output 1:
Input:
Wipro
Bangalore
Output:
Wipro Technologies Bangalore
Example Input/Output 2:
Input:
ABC
Mumbai
Output:
ABC Technologies Mumbai

Program:
print(input()+" Technologies "+input())
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;
}



Monday 18 September 2017

                                                                          Three Strings
Three Strings


Given N string values, the program must print 3 string values as the output as described in the Example Input/Output section.
Input format:
The first line will contain N denoting the number of string values.
Next N lines will contain the N string values.
Output format:
Three lines containing string values as described in the Example Input/Output section.
Example Input/Output 1:
Input:
3
JOHN
JOHNY
JANARDHAN
Output:
JJOJAN
OHHARD
NNYHAN
Example Input/Output 2:
Input:
4
JOHN
JOHNY
JANARDHAN
MIKESPENCER
Output:
JJOJANMIKE
OHHARDSPE
NNYHANNCER
C++ Program:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string s,s1="",s2="",s3="";
int n;
cin>>n;
for(int i=0;i<n;i++)
{
    cin>>s;
    s1+=s.substr(0,i+1);
    s2+=s.substr(i+1,s.length()-2*(i+1));
    s3+=s.substr(s.length()-(i+1));
}
cout<<s1<<"\n"<<s2<<"\n"<<s3;
}