Saturday, September 30, 2017

Area of a Circle

The program must accept the diameter of the circle as a command line argument and print the area of the circle rounding it up to 2 decimal places.

Example Input/Output 1:
Input:
14
Output:
154.00

Example Input/Output 2:
Input:
14.42
Output:
163.38

Solution:
#include <stdio.h>
#include <stdlib.h>
void main(int c,char *b[])
{
    float a = atof(b[1])/2;
    printf("%.2f",3.142857*a*a);
}
Share:

Friday, September 29, 2017

Perfect Squares - Till N

Given a positive integer N as the input, print all the perfect squares till N (inclusive of N).

Input Format:
The first line contains N.

Output Format:
The first line contains the perfect squares till N separated by a space.

Boundary Conditions: 1 <= N <= 9999999

Example Input/Output:
Input:
20
Output:
1 4 9 16

Solution:
#include <stdio.h>
void printSquare(int n)
{
 for(int i=1;i*i<=n;i++)
  printf("%d ",i*i);
}
void main()
{
int N;
scanf("%d",&N);
printSquare(N);
}
Share:

Thursday, September 28, 2017

Pattern Printing - Start Number

Given an integer N as the input and a start integer S, print the pattern as given in the Example Input/Output section.

Input Format:
The first line contains S and N, each separated by a space.

Output Format:
2N lines containing the desired pattern.

Boundary Conditions: 2 <= N <= 50 1 <= S <= 20

Example Input/Output:
Input:
3 4
Output:
3
4 4
5 5 5
6 6 6 6
6 6 6 6
5 5 5
4 4
3

Solution:
#include<stdio.h>
void printPattern(int b,int n)
{
    for(int i=0;i<2*n;i++)
    {
        int k=i<n?b+i:(n-i%n)+b-1;
        for(int j=0;j<=k-b;j++)
         printf("%d ",k);
        printf("\n");
    }
}
int main()
{
    int start, N;
    scanf("%d%d",&start,&N);
    printPattern(start,N);
    return 0;
}


Share:

Wednesday, September 27, 2017

String - Repeating Alphabets

Given a string S as the input, print the distinct alphabets in S that occur more than once. The alphabets must be printed based on the order of their occurrence in S.

Input Format:
The first line contains S.

Output Format:
The first line contains the distinct alphabets in S that occur more than once.

Boundary Conditions: 2 <= LENGTH of S <= 200

Example Input/Output 1:
Input:
Apple
Output:
p

Example Input/Output 2:
Input:
environment
Output:
en

Solution:
#include <bits/stdc++.h>
using namespace std;
int main()
{
 string s,x;
 cin>>s;
 map<char,int> m;
 for(int i=0;i<s.length();i++)
  {
      m[s[i]]++;
      if(m[s[i]]==1)
       x+=s[i];
  }
  for(int i=0;i<x.length();i++)
      if(m[x[i]]>1)
       cout<<x[i];
}
Share:

Tuesday, September 26, 2017

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:
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

Solution:
n = int(input())
for i in range(1,2*n):
    for j in range(2*n-1):
        if j==n-i or j==i-n:
            print(i,end=' ')
        elif j==n+i-2 or j==3*n-i-2:
            print(4*n-2-i,end=' ')
        else:
            print("0",end=' ')
    print()
Share:

Monday, September 25, 2017

Pattern - ZigZag Number Square

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:
N lines containing the desired pattern.

Boundary Conditions: 2 <= N <= 50

Example Input/Output:
Input:
5
Output:
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25

Solution:
#include<stdio.h>
void printPattern(int n)
{
 for(int i=1;i<=n;i++)
 {
    for(int j=(i*n)-n+1;j<=i*n&&i%2==1;j++)
      printf("%d ",j);
    for(int j=(i*n);j>(i*n)-n&&i%2==0;j--)
      printf("%d ",j);
    printf("\n");
 }
}
int main()
{
    int N;
    scanf("%d",&N);
    printPattern(N);
    return 0;
}


Share:

Saturday, September 23, 2017

Second Largest Value among N integers

The program must accept N integers and print the second largest value among the N integers.

Input Format:
The first line denotes the value of N. Next N lines will contain the N integer values.

Output Format:
The first line contains the second largest integer.

Boundary Conditions: 2 <= N <= 100 The value of the integers will be from -999999 to 999999.

Example Input/Output:
Input:
3
100
2200
345
Output:
345

Solution:
#include <iostream>
using namespace std;
int main()
{
 int a,n,f,s;
 cin>>n;
 for(int i=0;i<n;i++)
 {
     cin>>a;
     i==0?f=a:f<a?s=f,f=a:a<f&&a>s?s=a:s=s;
 }
 cout<<s;
}
Share:

Thursday, September 21, 2017

First Repeating Character From Last

A string S is passed as the input. S has at least one repeating character. The program must print the first repeating character C from the last.

Input Format:
The first line contains S.

Output Format:
The first line contains C.

Boundary Conditions: Length of S will be from 3 to 100.

Example Input/Output 1:
Input:
abcdexyzbwqpooplj
Output:
p

Solution:
#include <bits/stdc++.h>
using namespace std;
int main()
{
 string s;
 getline(cin,s);
 int i;
 map<char,int> m;
 for(i=s.length()-1;i>=0;i--)
        m[s[i]]++;
 for(i=s.length()-1;i>=0;i--)
        if(m[s[i]]>1)
             break;
 i==0?cout<<"0":cout<<s[i];
}
Share:

Wednesday, September 20, 2017

Prime's Sum

Given a number N.  Find if it can be expressed as sum of two prime numbers.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N as input.

Output:
For each test case, In new line print "Yes" if it can be expressed, Otherwise print "No".

Constraints:
1<=T<=2000
1<=N<=106

Example:
Input:
2
34
23
Output:
Yes
No

Solution:
#include <iostream>
using namespace std;
int main() {
          int n,j=0;
          cin>>n;
          for(int i=0;i<n;i++)
          {
              int a;
              cin>>a;
              if(a%2==0&&a>2)
               cout<<"Yes"<<endl;
              else
               {
                   a=a-2;
                   for(j=2;j<=a/2;j++)
                    if(a%j==0)
                     break;
                   if(j==a/2+1)
                    cout<<"Yes"<<endl;
                    else
                     cout<<"No"<<endl;
               }
          }
          return 0;
}
Share:

Matrix Row Sum

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

Solution:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
 int i,j,n,m,a,s;
 cin>>n>>m;
 for(i=0;i<n;i++)
 {
     s=0;
     for(j=0;j<m;j++)
     {
         cin>>a;
         s+=a;
     }
    cout<<s<<endl;
 }
}
Share:

Monday, September 18, 2017

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

Solution:
x,a,b,c=int(input())," "," "," "
s=[input().strip() for _ in range(x)]
for i in range(x):
    a+=s[i][0:i+1]
    b+=s[i][i+1:len(s[i])-i-1]
    c+=s[i][len(s[i])-i-1:]
print(a.strip(),'\n',b.strip(),'\n',c.strip())
Share:

Sunday, September 17, 2017

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.

Solution-1:
h,l=int(input()),int(input())
print(h-(l-2*h)//2,(l-2*h)//2)

Solution-2:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
  int h,l;
  cin>>h>>l;
  cout<<h-(l-2*h)/2<<" "<<(l-2*h)/2;
}
Share:

Saturday, September 16, 2017

Average Speed

A single line L with a set of space separated values indicating distance travelled and time taken is passed as the input. The program must calculate the average speed S (with precision up to 2 decimal places) and print S as the output.

Note:
The distance and time taken will follow the format DISTANCE@TIMETAKEN. DISTANCE will be in kilometers and TIMETAKEN will be in hours.

Input Format:
The first line contains L.

Output Format:
The first line contains the average speed S. Boundary Conditions: Length of L will be from 3 to 100.

Example Input/Output 1:
Input:
60@2 120@3
Output:
36.00 kmph
Explanation:
Total distance = 60+120 = 180 km. Total time taken = 2+3 = 5 hours. Hence average speed = 180/5 = 36.00 kmph

Solution:
s,d,t = input().split(),0,0
for i in s:
    m = [int(x) for x in i.split('@')]
    d,t=d+m[0],t+m[1]
print('%.2f'%(d/t),'kmph')
Share:

Friday, September 15, 2017

Morning Walk Distance

A man in order to reduce his weight walks along the boundary of a rectangular plot every morning. Depending on his energy level he walks for N rounds on a given day. Given the length L and breadth B of the rectangular plot along with the number of rounds N, the program must print the total distance D covered in the morning walk.

Note: Do not worry about the units like meters or kilometers.

Input Format:
The first line contains L and B separated by a space. The second line contains N.

Output Format:
The first line contains D.

Boundary Conditions:
1 <= N <= 100 1 <= L, B <= 999999

Example Input/Output 1:
Input:
10 20
5
Output:
300

Solution:
l,b=[int(i) for i in input().split()]
n=int(input())

print(2*(l+b)*n)
Share:

Thursday, September 14, 2017

Matrix Pattern Printing - First/Last Higher

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:
N lines containing the desired pattern.

Boundary Conditions: 2 <= N <= 100

Example Input/Output 1:
Input:
3
Output:
1 1 1 2
3 2 2 2
3 3 3 4

Solution:
#include <stdio.h>
void printPattern(int n) {
for(int i=1;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(i%2==1&&j==n||i%2==0&&j==0)
printf("%d ",i+1);
else
printf("%d ",i);
}
printf("\n");
}
}
int main()
{
int N;
scanf("%d",&N);
printPattern(N);
}
Share:

Wednesday, September 13, 2017

String Letters Frequency

A string value S containing N unique letters is passed as the input. The program must print the letters in the string based on the count of their occurrence. The letters of higher frequency of occurrence must appear first. If two letters have same frequency of occurrence then they are arranged as per alphabetical order.

Input Format:
The first line contains S.

Output Format:
N lines containing letters based on their frequency of occurrence.

Boundary Conditions:
2 <= LENGTH(S) <= 10000

Example Input/Output 1:
Input:
MANAGEMENT
Output: A2 E2 M2 N2 G1 T1

Example Input/Output 2:
Input: ArrangemENt
Output: r2 A1 E1 N1 a1 e1 g1 m1 n1 t1

Solution:
#include <bits/stdc++.h>
using namespace std;
int m[26]={0};
bool fun(char a,char b)
{
    if(m[a-'A']==m[b-'A'])
      return a<b;
    else
      return m[a-'A']>m[b-'A'];
}
int main(int argc, char** argv)
{
 int i;
 string s,a;
 cin>>s;
 for(i=0;i<s.length();i++)
  {
      m[s[i]-'A']++;
      if(m[s[i]-'A']==1)
       a+=s[i];
  }
  sort(a.begin(),a.end(),fun);
  for(i=0;i<a.length();i++)
   cout<<a[i]<<m[a[i]-'A']<<" ";
}
Share:

Monday, September 11, 2017

GCD of N Integers

N integers are passed as input to the program. The program must print the GCD (HCF) of these N integers.

Input Format:
The first line contains N. The second line contains N integers each separated by a space.

Output Format:
The first line contains the HCF of these N numbers.

Boundary Conditions:
2 <= N <= 100

Example Input/Output 1:
Input:
4
15 20 30 50
Output:
5

Solution:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
 int n,t;
 cin>>n;
 int a[n];
 for(int i=0;i<n;i++)
  cin>>a[i];
 for(int i=0;i<n-1;i++)
  {
      while(a[i+1]!=0)
       {
          t=a[i+1];
          a[i+1]=a[i]%a[i+1];
          a[i]=t;
       }
       a[i+1]=a[i];
  }
  cout<<a[n-1];
}
Share: