Tuesday, 31 July 2018

Question: 

To find the number of vowels present in a sentence.

Test Case:

Solution:

#include <stdio.h>
#include <conio.h>
int main()
{
     int cha,i,c=0;
     char ch,str[40]="";
     printf("enter string : ");
     scanf("\n %s",str);
     for(i=0;str[i]!='\0';i++)
     {
     cha = str[i];
     switch(cha){
          case'a' :
          case'A' :
          case'e' :
          case'E' :
          case'i' :
          case'I' :
          case'o' :
          case'O' :
          case'u' :
          case'U' :
          c++;
     }
     }

     printf("\nTotal number of vowels is %d",c);
return 0;
}
Number Challenge
Mike set off with great zeal to the "Kracker Jack Fun Fair 2017". There were numerous activities in the fair, though Mike being a math expert, liked to participate in the Number Challenge.

Mike was given a string D of numbers containing only digits 0's and 1's. His challenge was to make the number to have all the digits same. For that, he should change exactly one digit, i.e. from 0 to 1 or from 1 to 0. If it is possible to make all digits equal (either all 0's or all 1's) by flipping exactly 1 digit then he has to output "Yes", else print "No" (without quotes).

Write a program to help Mike win over his challenge.


Input Format:
First and only input contains a string D of numbers made of only digits 1's and 0's.

Output Format:
Output “Yes" or a "No", depending on whether its possible to make it all 0s or 1s or not.
Refer sample input and output for formatting specifications.


Sample Input1:
101
Sample Output1:
Yes

Sample Input2:
11
Sample Output2:
No


Solution:
#include<stdio.h>
#include<string.h>
int main()
{
    int sum=0,i,l;
    char d[50];
    scanf("%s",d);
    l=strlen(d);
    for(i=0;i<l;i++)
    if(d[i]=='1')
    sum=sum+1;
    if(sum==1||(sum==(l-1)))
    printf("Yes");
    else
    printf("No");
    return 0;
} 
Alternating Code
It is IPL Season and the first league match of Dhilip’s favorite team, "Chennai Super Kings". The CSK team is playing at the IPL after 2 years and like all Dhoni lovers, Dhilip is also eagerly awaiting to see Dhoni back in action.

After waiting in long queues, Dhilip succeeded in getting the tickets for the big match. On the ticket, there is a letter-code that can be represented as a string of upper-case Latin letters.

Dhilip believes that the CSK Team will win the match in case exactly two different letters in the code alternate. Otherwise, he believes that the team might lose. Please see note section for formal definition of alternating code.

You are given a ticket code. Please determine, whether CSK Team will win the match or not based on Dhilip’sconviction. Print "YES" or "NO" (without quotes) corresponding to the situation.

Note:
Two letters x, y where x != y are said to be alternating in a code, if code is of form "xyxyxy...".


Input Format:
First and only line of the input contains a string S denoting the letter code on the ticket.

Output Format:
Output a single line containing "Yes" (without quotes) based on the conditions given and "No" otherwise.
Refer sample input and output for formatting specifications.


Sample Input1:
ABABAB
Sample Output1:
Yes

Sample Input2:
ABC
Sample Output2:
No


Solution:
 #include<stdio.h>
#include<string.h>
int main()
{
    int i;
    char a[50];
    scanf("%s",a);
    for(i=0;(a[i]!='\0');i++)
    {
        if(a[i]!=a[i+2]||a[i+2]=='\0'||a[i]==a[i+1])
        break;
    }
    if((a[i+2]=='\0'))
    printf("Yes");
    else
    printf("No");
    return 0;
 }

Friday, 27 July 2018

Count
 
Mars Spell Bee is the largest self-motivated spelling competition held for school children. The children from different schools who are participating in the competition will be given a registration code.  The registration is on a first come first serve basis to a maximum of N entries.
 
The competition is conducted in two different galleries of the venue.  Just for the ease of their management, the Event organizers have announced to divide the children into two groups, to attend the competition in the two chosen galleries. By that note, all those children who have their registration code as an even number will be put in one gallery and those with odd number will be in another gallery.
Help the organizers to find count of number of even registration codes and odd registration codes from the total N.
 
Note: The registration code need not be unique as each child will have a unique school code.

 
Input Format:
The first line of input consists of a single integer N denoting the number of registration codes issued for the competition.
The second line of input consists of N space separated integers, denoting the registration codes of each child.


Output Format:
Output a single with the count of even numbers and odd numbers from N, separated by a single space.
Refer sample input and output for formatting specifications.


Sample Input 1:
3
1 4 10


Sample Output 1:
2 1

Sample Input 2:
5
2 6 23 12 11


Sample Output 2:
3 2


Solution:
#include<stdio.h>
int main()
{
    int i,n,a[100],z=0,y=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        if(a[i]%2==0)
        {
            z++;
        }
        else
        y++;
    }
    printf("%d %d",z, y);
    return 0;

}
Cookery Contest
 
Suzanne is participating in the Cookery Contest to be held at her Company. Suzanne is just a beginner in cooking but is more creative. She wanted to give a good show though she is going to cook for the first time. So she decided to cook only a small portion of her recipe, which has the same ratios of ingredients, but makes less food.
 
Suzanne however, does not like fractions. The original recipe contains only whole numbers of ingredients, and Suzanne wants the reduced recipe to only contain whole numbers of ingredients as well. Help her determine how much of each ingredient to use in order to make as little food as possible.

 
Input Format:
The first line of the input consists of a positive integer N, which corresponds to the number of ingredients.
Next line contains N space-separated integers, each indicating the quantity of a particular ingredient that is used.


Output Format:
Output exactly N space-separated integers on a line that gives the quantity of each ingredient that Suzanne should use in order to make as little food as possible.
Refer sample input and output for formatting specifications.


Sample Input 1:
2
4 4


Sample Output 1:
1 1

Sample Input 2:
3
2 3 4


Sample Output 2:
2 3 4


Solution:
#include<stdio.h>
int main()
{
    int n,a[10],i,j,k,g=1;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    k=a[0];
    for(i=1;i<n;i++)
    {
        if(a[i]<k)
        k=a[i];
    }
    for(i=1;i<=k;i++)
    {
        for(j=0;j<n;j++)
        {
            if(a[j]%i!=0)
            {
                break;
            }
        }
        if(j==n)
        g=i;
    }
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]/g);
    }
    return 0;

}
Lucky Pairs
 
Richie and Riya are participating in a game called "Lucky pairs" at the Annual Game Fair in their Company. As per the rules of the contest, two members form a team and Richie initially has the number A and Riya has the number B.
There are a total of N turns in the game, and Richie and Riya alternatively take turns. In each turn the player whose turn it is, multiplies his or her number by 2. Richie has the first turn. Suppose after the entire N turns, Richie’s number has become C and Riya’s number has become D. The final score of the team will be the sum of the scores (C+D) of both the players after N turns.
 
Write a program to facilitate the quiz organizers to find the final scores of the teams.

 
Input Format:
The only line of input contains 3 integers AB, and N.

Output Format:
Output a single line which contains the integer that gives the final score of the team which will be the sum of the scores of both the players after N turns.
Refer sample input and output for formatting specifications.


Sample Input 1:
1 2 1

Sample Output 1:
4

Sample Input 2:
3 2 3

Sample Output 2:
16


Solution:
#include<stdio.h>
int main()
{
    int A,B,N,G,i;
    scanf("%d%d%d",&A,&B,&N);
    for(i=0;i<N;i++)
    {
        if((i%2)==0)
        A=A*2;
        else
        B=B*2;
    }
    G=A+B;
    printf("%d",G);
    return(0);

}
Team Event
 
Super Quiz Bee is a famous quiz Competition that tests students on a wide variety of academic subjects. This week’s competition was a Team event and students who register for the event will be given a unique registration code N. The participants are teamed into 10 teams and the team to which a participant is assigned to depends on the absolute difference between the first and last digit in the registration code.
 
The event organizers wanted your help in writing an automated program that will ease their job of assigning teams to the participants. If the registration number given is less than 10, then the program should display "Invalid Input".

 
Input Format:
The only line of input contains an integer N.

Output Format:
Output the absolute difference between the first and last digit of N.
Refer sample input and output for formatting specifications.


Sample Input 1:
345

Sample Output 1:
2

Sample Input 2:
9

Sample Output 2:
Invalid Input


Solution:
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,s,i[10],a,g;
    scanf("%d",&n);
    if(n/10==0)
    printf("Invalid Input");
    else
    {
    for(a=0;n!=0;a++)
    {
    s=n%10;
   
    n=n/10;

    i[a]=s;
    }
    
    g=abs(i[0]-i[a-1]);
    printf("%d",g);
    }
    return(0);

}
Candy Game
 
Mona set off with great zeal to the "Fun Fair 2017". There were numerous activities in the fair, though Mona liked the Candy game. Delicious candies were wrapped in colourful foiled sheets with some random numbers on each of the candies. The game coordinators then formed many groups of few candies together, such that each candy group makes an integer and hid them all around the room. The objective of the game is that the players should look for the occurrences of number four anywhere in the integers (candy groups) placed in the room.
 
Mona started off with the game where there are many such integers, for each of them she should calculate the number of occurrences of the digit 4 in the decimal representation. Can you please help her in succeeding the game?

 
Input Format:
The only line of input contains a single integer from the candy group.

Output Format:
Output should contain the number of occurrences of the digit 4 in the respective integer from the candy groups that Mona gets.
Refer sample input and output for formatting specifications.


Sample Input 1:
447474

Sample Output 1:
4

Sample Input 2:
12

Sample Output 2:

0


Solution:
#include<stdio.h>
int main()
{
    int n,i,c=0;
    scanf("%d",&n);
    while(n|=0)
    {
        i=(n%10);
        n=n/10;
        if(i==4)
        c++;
    }
    printf("%d",c);
    return(0);
    

}
Peter and Casino
 
Hotel Royal Gardenia has arranged for an elite business party for the lead industrialists and celebrities of the City. Followed by a dinner buffet, the Event coordinators planned for some casino game events for the high-toned crowd. Peter was a visitor at the party and he takes some number of rubles to the casino with the intention of becoming rich. He plays three machines in turn. Unknown to him, the machines are entirely predictable. Each play costs one ruble. The first machine pays 20 rubles every 25th time it is played; the second machine pays 80 rubles every 120th time it is played; the third pays 8 rubles every 12th time it is played.
Given the number of rubles with Peter initially (there will be at least one and fewer than 1000), and the number of times each machine has been played since it last paid, write a program that calculates the number of times Peter plays until he goes broke.


Input Format:
First line of the input is an integer that corresponds to the number of rubles with Peter initially.
Next 3 lines of the input is an integer that corresponds to the number of times each machine has been played since it last paid.

Output Format:
Output a single line that gives the number of times Peter plays until he goes broke.
Refer sample input and output for formatting specifications.
Sample Input 1:
48
3
12
4

Sample Output 1:
Peter plays 56 times before going broke

Solution:
#include<stdio.h>
int main()
{
    int r,a,b,c,count=0;
    scanf("%d%d%d%d",&r,&a,&b,&c);
    while(r>0)
    {
        if(a==25)
        {
            r=r+20;
        }
        r--;
        count++;
        if(r==0)
        break;
       
        if(b==120)
        {
            r=r+80;
        }
        r--;
        count++;
        if(r==0)
        break;
        
        if(c==12)
        {
            r=r+8;
        }
        r--;
        count++;
        if(r==0)
        break;
        a++,b++,c++;
        
    }
    
    printf("Peter plays %d times before going broke",count);
    return 0;
    
    
}
Insomnia cure
«One dragon. Two dragon. Three dragon», — the princess was counting. She had trouble falling asleep, and she got bored of counting lambs when she was nine.
However, just counting dragons was boring as well, so she entertained herself at best she could. Tonight she imagined that all dragons were here to steal her, and she was fighting them off. Every k-th dragon got punched in the face with a frying pan. Every l-th dragon got his tail shut into the balcony door. Every m-th dragon got his paws trampled with sharp heels. Finally, she threatened every n-th dragon to call her mom, and he withdrew in panic.
Write a program to find how many imaginary dragons suffered moral or physical damage tonight, if the princess counted a total of d dragons?
 
Input and Output Format:
Input data contains integer numbers k, l, m, n and d, each number in a separate line (1 ≤ k, l, m, n ≤ 101 ≤ d ≤ 105).
The output displays the number of damaged dragons.
Sample Input 1:
1
2
3
4
12

Sample Output 1:
12

Sample Input 2:
2
3
4
5
24

Sample Output 2:
17

Solution:
  #include<stdio.h>
int main()
{
    int k,i,l,m,n,d,c=0;
    scanf("%d%d%d%d%d",&k,&l,&m,&n,&d);
    for(i=1;i<=d;i++)
    {
        if(i%k==0||i%l==0||i%m==0||i%n==0)
        {
            c++;
        }
    }
printf("%d",c);
return(0);
    
}
Character Pattern 8
Write a program to generate the following pattern.
*********
b******* b
bb***** bb
bbb***bbb
bbbb*bbbb


Input and Output Format:
Input consists of a single integer that corresponds to n, the number of rows. n is always an odd number.

Sample Input :
5

Sample Output :
*********
b******* b
bb***** bb
bbb***bbb
bbbb*bbbb
 
Solution:
  #include<stdio.h>
int main()
{
    int n,i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=1;j<n*2;j++)
        {
            if(i>=j||j>=n*2-i)
            printf("b");
            else
            printf("*");
        }
        printf("\n");
    }
        return(0);
}
Target Practice
Drona normally trains his disciples using a board which consists of concentric circles. When the student correctly hits the center of the concentric circles, his score is 100. The score gets reduced depending on where the students hit on the board. When the student hits outside the board, his score is 0. Drona will not allow a student to have his food unless he scores 100. Arjuna will always hit the target in his first attempt and he will leave early.
Others may take more turns to reach the score of 100.
Can you write a program to determine the number of turns a disciple takes to reach the target score of 'n' ?

Input Format:
Input consists of a list of positive integers. The first integer corresponds to the target score 'n'. Assume that all the other integers input are less than or equal to n.

Output Format:
Output consists of a single line. Refer sample output for format details.

Sample Input 1:
100
4
40
60
Sample output 1:
The number of turns is 3

Sample Input 2:
1000
1000
Sample output 2:
The number of turns is 1

Solution:
#include<stdio.h>
int main()
{
    int n,a,i=0,c=0,s=0;
    scanf("%d",&n);
    do
    {
        c++;
        scanf("%d",&a);
        s+=a;
        i++;
    }
    while(s<n);
    printf("The number of turns is %d",c);
    return 0;
}
Print 1
Write a program to print all the numbers between a and b (a and b inclusive) using a for loop.
 
Input Format:
Input consists of 2 integers. The first integer corresponds to a and the second integer corresponds to b. Assume a>=b.
 
Output Format:
Refer Sample Input and Output for formatting specifications.
 
[All text in bold corresponds to input and the rest corresponds to output]
Sample Input and Output:
Enter the value of a
4
Enter the value of b
10
4
5
6
7
8
9
10
Solution:
#include<stdio.h>
int main()
{
    int a,b,i;
    printf("Enter the value of a");
    scanf("%d",&a);
    printf("\nEnter the value of b");
    scanf("%d",&b);
    
    
         for(i=a;i<=b;i++)
        {
            printf("\n%d",i);
        }
    

    return 0;
}
DATA MINING - while
In the University Examinations conducted during the past 5 years, the toppers registration numbers were  7126, 82417914, 7687 and 6657. Your father is an expert in data mining and he could easily infer a pattern in the toppers registration numbers. In all the registration numbers listed here, the sum of the odd digits is equal to the sum of the even digits in the number. He termed the numbers that satisfy this property as Probable Topper Numbers.
 
Write a program to find whether a given number is a probable topper number or not.

Input Format:
Input consists of a single integer.

Output Format:
Output consists of a single line. Refer sample output for details.

Sample Input 1:
143


Sample Output 1:
yes
 
Sample Input 2:
344

Sample Output 2:
no

Solution:
#include<stdio.h>
int main()
{
    int n,n1,sum=0,sum1=0;
    scanf("%d",&n);
    while(n>0)
    {
        n1=n%10;
        if (n1%2==0)
        {
            sum=sum+n1;
        }
        else
        {
            sum1=sum1+n1;
        }
        n=n/10;
    }
    if(sum==sum1)
    {
        printf("yes");
    }
    else
        printf("no");
        return 0;

}

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...