Monday 25 February 2019

Count of common factors

Given a set of numbers where all other numbers are multiple of the smallest number, the program must find the count of the common factors C excluding 1.

Input Format:
First line will contain the integer value N representing how many numbers are passed as input.
Next N lines will have the numbers.

Output Format:
First line will contain the count of common factors C.

Constraints:
N will be from 2 to 20.

Sample Input/Output:
Example 1:
Input:
2
100
75

Output:
2
Explanation:
The common factors excluding 1 are 5,25. Hence output is 2




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

int main()
{
   int N,A[100],i,cnt=0,j,small;
   scanf("%d",&N);
   for(i=0;i<N;i++)
      scanf("%d",&A[i]);
    small=A[0];
    for(i=0;i<N;i++)
      if (A[i]<small)
        small=A[i];
   j=2;
   while (j<=small)
   {
     for(i=0;i<N;i++)
      if (A[i]%j!=0)
         break;
      if (i==N)
        cnt++;
     j++;
   }
   printf("%d",cnt);

}

OUTPUT
Input:
3
10
20
30
Output:
3

Thursday 21 February 2019

Print from X to Y - Increment by 0.1

The program must accept two float values X and Y as the input. The program must print the values from X to Y where the values must be incremented by 0.1.

Boundary Conditions:
0.0 <= X, Y <= 100.0

Input Format:
The first line contains X and Y separated by a space.

Output Format:
The first line contains the values separated by a space.

Example Input/Output 1:
Input:
3.2 3.8
Output:
3.2 3.3 3.4 3.5 3.6 3.7 3.8

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

int main()
{
   float X,Y;
   int cnt;
   scanf("%f%f",&X,&Y);
   cnt=(Y*10-X*10);
   while(cnt>=0)
   {
      printf("%.1f ",X);
      X=X+0.1;
      cnt--;
   }  
}

Output

9.4 10.2

9.4 9.5 9.6 9.7 9.8 9.9 10.0 10.1 10.2

Thursday 14 February 2019

Expand Alphabets

A string S is passed as input. S will contain multiple integer values with each integer value followed by an alphabet. The program must expand the alphabets based on the related integer value.

Input Format:
The first line contains S.

Output Format:
The first line contains the expanded string value.

Boundary Conditions:
Length of S is from 2 to 100.

Example Input/Output 1:
Input:
4a5h
Output:
aaaahhhhh
Explanation:
As it is 4a and 5h, four a's are printed followed by 5 h's

Example Input/Output 2:
Input:
1k2b4k
Output:
kbbkkkk

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

int main()
{
   char S[100],num[100];
   int i=0,j=0,cnt=0;

   scanf("%s",S);
   while(S[i]!='\0')
   {
       
       if (isdigit(S[i]))
       {
          j=0;
          while(S[i]>='0' && S[i]<='9')
            num[j++]=S[i++];
            num[j]='\0';
            cnt=atoi(num);
        }  
        
            while (cnt>0)
            {
               printf("%c",S[i]);
               cnt--;
            }  
            ++i;
   }  
}

OUTPUT
100q2w
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqww

Wednesday 13 February 2019

Palindrome Missing Alphabet

String S which is a palindrome is passed as the input. But just one alphabet A is missing in S. The program must print the missing alphabet A.
Note: The FIRST alphabet of S will always be present.

Input Format:
The first line contains S.

Output Format:
The first line contains the missing alphabet A.

Boundary Conditions:
The length of the input string S is between 3 to 100.
The FIRST alphabet of S will always be present.

Example Input/Output 1:
Input:
malayaam
Output:
l

Example Input/Output 2:
Input:
abcddcb
Output:
a

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

int main()
{
   char S[100];
   int i,j,a;
   scanf("%s",S);
   a=strlen(S);
   if (S[0]!=S[a-1])
   {
         printf("%c",S[0]);
         exit(0);
   }
   for(i=0,j=a-1;i<a/2;i++,j--)
   {
       if (S[i]!=S[j])  
       {
          if (S[i+1]==S[j])
                   printf("%c",S[i]);
           else
                  printf("%c",S[j]);
           break;
         }
  }     
}

OUTPUT
malayaam
l

Saturday 9 February 2019

Equal First and Second Half String

The program must accept a string S as the input. The program must print yes if the first half of the string is equal to the second half of the string. Else the program must print no as the output.
Note: If the length of the string is odd ignore the character in the middle.

Boundary Condition(s):
1 <= Length of the string <= 1000

Input Format:
The first line contains the string S.

Output Format:
The first line contains either yes or no.

Example Input/Output 1:
Input:
dumdum
Output:
yes
Explanation:
Both the first and second half of the string is same so yes is printed.

Example Input/Output 2:
Input:
YelYe
Output:
yes

Program
#include <stdio.h>

int main()
{
    char S[1000];
    int i,j=0,a;
    scanf("%s",S);
    a=strlen(S);
    if(a%2==0)
       j=a/2;
    else
       j=a/2+1;
    for(i=0;i<a/2;i++)
    {
        if(S[i]!=S[j++])
        {
            printf("no");
            break;
        }
    }    
    if(i==a/2)
        printf("yes");
            
}


Output:
tellet
no

Thursday 7 February 2019

Reverse String Till Underscore

String S is passed as the input to the program. S may or may not have a single underscore embedded in it. The program must reverse the String S till the first underscore and print it as the output.

Input Format:
The first line contains S.

Output Format:
The first line contains the string S modified based on the given conditions.

Boundary Conditions:
Length of S is from 3 to 100.

Example Input/Output 1:
Input:
abcd_pqrs
Output:
dcba_pqrs

Example Input/Output 2:
Input:
_kilo
Output:
_kilo

Example Input/Output 3:
Input:
nounderscore
Output:
erocsrednuon

Program

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

int main()

{
  char s[100],rev[100];
  int i=0,j=0,m,fnd=0;
  scanf("%[^\t\n]s",s);
  
  while(s[i] != '\0' && s[i] != '\n')
  {
     
     if (s[i]!='_')
       rev[j++]=s[i];
     else
        {
            for(j=j-1;j>=0;j--)
            printf("%c",rev[j]);
            fnd=1;
        }
        if (fnd==1)
            printf("%c",s[i]);
    ++i;
  }
  if (fnd==0)
  {
    for(j=strlen(s)-1;j>=0;j--)
    printf("%c",s[j]);
  }



OUTPUT
fsdf 35345 sfsdfsdf_good
fdsfdsfs 54353 fdsf_good

Wednesday 6 February 2019

Print Only Alphabets

A string S is passed as the input. S can contain alphabets, numbers and special characters. The program must print only the alphabets in S.

Input Format:
The first line contains S.

Output Format:
The first line contains only the alphabets in S.

Boundary Conditions:
The length of the input string is between 1 to 1000.

Example Input/Output 1:
Input:
abcd_5ef8!xyz
Output:
abcdefxyz

Program
#include <stdio.h>

int main()
{
    char S[1000];
    int i=0;
    
    scanf("%[^\t\n]s",S);
    
    while (S[i]!='\0' && S[i]!='\n')
    {
        if ((S[i]>='A' && S[i]<='Z') || (S[i]>='a' && S[i]<='z'))
        printf("%c",S[i]);
        ++i;
    }

    return 0;
}


Output:
Input:
jhjkhj564564   jk j|dg

jhjkhjjkjdg