Friday, October 6, 2017

Perfect Number

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

Solution 1:
n,s,i=int(input()),1,2
while i*i<=n:
    if n%i==0:
        s+=i+n/i
    i+=1
print('yes') if n==s else print('no')

Solution 2:
#include <iostream>
using namespace std;
int main()
{
 int i,n,s=1;
 cin>>n;
 for(i=2;i*i<=n;i++)
  if(n%i==0)
   s+=i+n/i;
 s==n?cout<<"yes":cout<<"no";
}
Share:

Thursday, October 5, 2017

Second Largest Value

The program must accept a set of N integer values as command line arguments and print the second largest value. If there is no second largest value, print -1 as the output.

Example Input/Output 1:
Input:
20 30 30 20 30 15 12 11 10 6
Output:
20

Example Input/Output 2:
Input:
19 19 19 19 19 19
Output:
-1

Solution:
#include <stdio.h>
#include <stdlib.h>
void main(int c,char *a[])
{
    int i,f=-1,s,n;
    for(i=1;i<c;i++)
     {
         n=atoi(a[i]);
        if(n>f)
        {
            s=f;
            f=n;
        }
        else if(n>s&&n<f)
         s=n;
     }
     printf("%d",s);
}
Share:

Wednesday, October 4, 2017

Only Alphabets and Space

Given a String S, remove all the special characters and numbers from S and print.

Input Format:
The first line contains the string S

Output Format:
The first line contains the string with only alphabets and spaces.

Boundary Conditions: 1 < Length of S < 1000

Example Input/Output 1:
Input:
Hello #$ World!
Output:
Hello World

Solution 1:
import re
print(''.join(re.findall('[A-Za-z]+|\s',input())))

Solution 2:
s=input()
for i in s:
    if i.isalpha() or i==' ':
       print(i,end='')
Share:

Print Largest Even Number - Digits

Given a number N as the input, print the largest even number E that can be formed using the digits present in the number. (There will be at least one even digit).

Input Format:
The first line contains N.

Output Format:
The first line contains E.

Example Input/Output 1:
Input:
1902
Output:
9210

Solution:
n=list(input())
e=min(filter(lambda x:(ord(x)-ord('0'))%2==0,n))
n.remove(e)
n.sort(reverse=True)
n.append(e)
print(''.join(n))
Share:

Reverse Number

The program must accept a number N using command line and prints the reverse of the number N.

Example Input/Output 1:
Input:
12345
Output:
54321

Example Input/Output 2:
Input:
10900
Output:
901

Solution:
#include <stdio.h>
#include <stdlib.h>
void main(int c,int *s[])
{
    int r=0,n=atoi(s[1]);
    while(n)
    {
        r=r*10+n%10;
        n/=10;
    }
    printf("%d",r);
}
Share:

Integer - Sum of Digits

The program must accept an integer value N as a command line argument and print the sum of the digits in N.

Example Input/Output 1:
Input:
14
Output:
5

Example Input/Output 2:
Input:
1932
Output:
15

Solution:
#include <stdio.h>
#include <stdlib.h>
void main(int c,char *n[])
{
    int a=atoi(n[1]),s=0;
    while(a)
    {
        s+=a%10;
        a/=10;
    }
    printf("%d",s);
}
Share:

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: