Showing posts with label Conditional Statements. Show all posts
Showing posts with label Conditional Statements. Show all posts

Friday, 27 July 2018

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;

}
Question: Thrill ride
 
"Fantasy Kingdom" is a brand new Amusement park that is going to be inaugurated shortly in the City and is promoted as the place for breath-taking charm. The theme park has more than 30 exhilarating and thrilling rides and as a special feature of the park, the park Authorities has placed many Booking Kiosks at the entrance which would facilitate the public to purchase their entrance tickets and ride tickets.
 
There are few rides in the park which are not suitable for Children and aged people, hence the park Authorities wanted to program the kiosks to issue the tickets based on people’s age. If the age given is less than 15 (Children) or greater than 60 (Aged), then the system should display as "Not Allowed", otherwise it should display as "Allowed". Write a block of code to help the Authorities program this functionality.

 
Input Format:
First line of the input is an integer that corresponds to the age of the person opting for the ride.

Output Format:
Output should display "Allowed" or "Not Allowed" based on the conditions given.
Refer sample input and output for formatting specifications.

Sample Input 1:
20

Sample Output 1:
Allowed

Sample Input 2:
12

Sample Output 2:
Not Allowed
Solution:
#include <stdio.h>
int main()
{
    int a;
    scanf("%d",&a);
    if(a<15||a>60)
    printf("\nNot Allowed");
    else
    printf("\nAllowed");
    return 0;
}

Thursday, 26 July 2018

Base 6-

Problem Description

Given a sequence of distinct numbers a1, a2, ….. an, an inversion occurs if there are indices i<j such that ai > aj .
For example, in the sequence 2 1 4 3 there are 2 inversions (2 1) and (4 3).
The input will be a main sequence of N positive integers. From this sequence, a Derived Sequence will be obtained using the following rule. The output is the number of inversions in the derived sequence.
Rule for forming derived sequence
An integer may be represented in base 6 notation. In base 6, 10305 is 1x64 + 3x62 + 5 =1409. Note that none of the digits in that representation will be more than 5.
The sum of digits in a base 6 representation is the sum of all the digits at the various positions in the representation. Thus for the number 1409, the representation is 10305, and the sum of digits is 1+0+3+0+5=9. The sum of digits may be done in the decimal system, and does not need to be in base 6
The derived sequence is the sum of the digits when the corresponding integer is represented in the base 6 form number will be expressed in base 6, and the derived sequence is the sum of the digits of the number in the base 6 representation.

Constraints

N <= 50
Integers in sequence <= 107

Input Format

The first line of the input will have a single integer, which will give N.
The next line will consist of a comma separated string of N integers, which is the main sequence

Output

The number of inversions in the derived sequence formed from the main sequence.

Example 1
Input
5
55, 53, 88, 27, 33
Output
2
Explanation
The number of integers is 5, as specified in the first line. The given sequence is 55, 53, 88, 27, 33.
The base 6 representation is 131, 125, 224, 43, 53 The derived sequence is 5,8,8,7,8 (corresponding to the sum of digits). The number of inversions in this is 2, namely (8, 7), (8, 7)
Example 2
Input
8
120,21,47,64,72,35,18,98
Output
11


Explanation
The base 6 representation of this is 320,33,115,144,200,55,30,242, and the derived sequence (sum of digits) is 5,6,7,9,2,10,3,8. The number of inversions is 11 (5,2), (5,3),(6,2) (6,3), (7,2), (7,3) (9,2),(9,3) (9,8),(10,3), (10,8)


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

Wednesday, 25 July 2018

Question: Welcome
 
A recently launched attraction at the "Events Square" entertainment fair is the "Carnival of Terror" which is an interactive fun zone featuring scary, horror and Halloween stories.

The Entry tickets for the show is to be printed with a Welcome message along with an additional message for Children stating they should be accompanied by an adult. Given the age of the person visiting the scary house, the ticket should carry the additional message only for Children whose age is less than 15 years. The show organizers wanted your help to accomplish this task. Write a program that will get age as the input and display the appropriate message on the tickets.


Input Format:
First line of the input is an integer that corresponds to the age of the person.

Output Format:
Output should display the additional message "Please note that you should be accompanied by an adult" for Children less than 15 years. Otherwise it should print only the Welcome message.
Refer sample input and output for formatting specifications.

Sample Input 1:
20

Sample Output 1:
Welcome to the show

Sample Input 2:
14

Sample Output 2:
Welcome to the show
Please note that you should be accompanied by an adult

 
Solution:
#include <stdio.h>
int main()
{
    int a;
    scanf("%d",&a);
    if(a<15)
    printf("\nWelcome to the show\nPlease note that you should be accompanied by an adult");
    else
    printf("Welcome to the show");
 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...