Looping Constructs
S3P1- CANDY GAME
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:
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:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
long num=sc.nextLong();
String s=String.valueOf(num);
int count=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='4')
{
count++;
}
}
System.out.println(count);
}
}
S3P5-COOKERY CONTEST
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.
Note: if it is not possible to reduce as whole ratio from the original ratio she will use the original ratio.
Input Format:
The first line of the input consists of a positive integer N, which corresponds to the number of ingredients.
The 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 to 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
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.
Note: if it is not possible to reduce as whole ratio from the original ratio she will use the original ratio.
Input Format:
The first line of the input consists of a positive integer N, which corresponds to the number of ingredients.
The 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 to 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:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n,i,j,k,g=1;
int[] a=new int[20];
n=sc.nextInt();
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
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++)
{
System.out.println(a[i]/g+" ");
}
}
}
S3P7- AES NUMBERS
AES Numbers
Varun is the founder of Event Management Company, "Sparsh Services". In the company all the financial transactions are carried out online and Varun has strictly insisted his staff to do any transactions on web browsers that supports AES encryption numbers.
A number is an AES number if it has exactly four divisors. In other words, there are exactly four numbers that divide into it evenly. For example, 10 is an AES number because it has exactly four divisors (1, 2, 5, 10). 12 is not an AES number because it has too many divisors (1, 2, 3, 4, 6, 12). 11 is not an AES number either. There is only one AES number in the range 10...12.
Given a range of numbers, write a program that counts how many numbers from that range are AES numbers.
Input Format:
The input consists of 2 space-separated integers, which corresponds to the lower limit and the upper limit of the number range.
You may assume that the numbers in the range are less than 1000.
Output Format:
Output a single line that gives the count of AES numbers from the given range.
Refer sample input and output for formatting specifications.
Sample Input 1:
1 20
Sample Output 1:
5
Sample Input 2:
50 100
Sample Output 2:
17
A number is an AES number if it has exactly four divisors. In other words, there are exactly four numbers that divide into it evenly. For example, 10 is an AES number because it has exactly four divisors (1, 2, 5, 10). 12 is not an AES number because it has too many divisors (1, 2, 3, 4, 6, 12). 11 is not an AES number either. There is only one AES number in the range 10...12.
Given a range of numbers, write a program that counts how many numbers from that range are AES numbers.
Input Format:
The input consists of 2 space-separated integers, which corresponds to the lower limit and the upper limit of the number range.
You may assume that the numbers in the range are less than 1000.
Output Format:
Output a single line that gives the count of AES numbers from the given range.
Refer sample input and output for formatting specifications.
Sample Input 1:
1 20
Sample Output 1:
5
Sample Input 2:
50 100
Sample Output 2:
17
Solution:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a,b;
a=sc.nextInt();
b=sc.nextInt();
int count=0;
int exactFour=0;
for(int i=a;i<=b;i++)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==4)
{
exactFour++;
}
}
System.out.println(exactFour);
}
}
S3P11-KIDS LOVE MUFFINS
Kids Love Muffins
Louis was celebrating his 10th Birthday and his parents have promised to make the best party ever for him. He will be very happy if he can invite all his friends to this party (he has many friends), but unfortunately his parents can't invite everyone because they have a limited number of muffins, and they want everyone to be happy.As we all know, kids love to eat a lot of muffins of the same type, let's say a kid will be happy only if he can eat at least K muffins of the same type.
Given K, and the number of available muffins of each type, calculate the maximum number of kids where Louis’s parents can make all of them happy by giving each one at least K muffins of the same type.
Input Format:
The first line of the input contains two space-separated integers N, the number of different muffins (1 ≤ N ≤ 100), and K, the minimum number of muffins which will make a kid happy as described above (1 ≤ K ≤ 100).
The second line of input contains N integers, separated by a single space, which are the available number of muffins of each type. There will be at least 1 muffin and at most 100 muffins of each type.
Output Format:
Output on a single line one integer, the maximum number of kids Louis’ parents can make happy by giving each one at least K muffins of the same type.
Refer sample input and output for formatting specifications.
Sample Input 1:
3 2
4 5 7
Sample Output 1:
7
Sample Input 2:
3 8
4 5 7
Sample Output 2:
0
Solution:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n,k;
n=sc.nextInt();
k=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int count=0;
for(int i=0;i<n;i++)
{
count+=arr[i]/k;
}
System.out.println(count);
}
}
S3P16-SERIES1
Series 1
The Event Organizing Company "Buzzcraft" focuses event management in a way that creates a win-win situation for all involved stakeholders. Buzzcraft don't look at building one time associations with clients, instead, aim at creating long-lasting collaborations that will span years to come. This goal of the company has helped them to evolve and gain more clients within notable time.
The number of clients of the company from the start day of their journey till now is recorded sensibly and is seemed to have followed a specific series like: 2,3,5,7,11,13,17,19, 23 ...
Write a program which takes an integer N as the input and will output the series till the Nth term.
Input Format:
First line of the input is an integer N.
Output Format:
Output a single line the series till Nth term, each separated by a comma.
Refer sample input and output for formatting specifications.
Sample Input 1:
5
Sample Output 1:
2 3 5 7 11
Sample Input 2:
10
Sample Output 2:
2 3 5 7 11 13 17 19 23 29
The number of clients of the company from the start day of their journey till now is recorded sensibly and is seemed to have followed a specific series like: 2,3,5,7,11,13,17,19, 23 ...
Write a program which takes an integer N as the input and will output the series till the Nth term.
Input Format:
First line of the input is an integer N.
Output Format:
Output a single line the series till Nth term, each separated by a comma.
Refer sample input and output for formatting specifications.
Sample Input 1:
5
Sample Output 1:
2 3 5 7 11
Sample Input 2:
10
Sample Output 2:
2 3 5 7 11 13 17 19 23 29
Solution:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int i=3,j,count=1;
int n=sc.nextInt();
int a[]=new int[50];
System.out.print("2 ");
x: for(i=3;count!=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
continue x;
}
}
System.out.print(i+" ");
count++;
}
}
}
S3P14-SEVEN SEGMENT DISPLAY
Seven Segment Display
The Event Organizing Company "Buzzcraft" wanted to procure seven segment displays to display any numeric information display boards, scrolling ad banners, etc., to place it in their Events. The Company contracted out their order to MDC team at Orange labs who designs embedded sensing nodes and provides connectivity to tie them to the internet of things.They are working on building seven segment displays. But the Company wanted to know how many seven segment displays will they need to represent an Integer x. They use one seven segment display to represent one digit of an Integer. For example: Integer "100" needs "3" seven segment boards to be represented.
Help them find out how many displays are needed?
Input Format:
First and only line consists of one positive integer that needs to be represented using seven segment displays.
Output Format:
Output a single line containing the number of digits of that integer.
Refer sample input and output for formatting specifications.
Sample Input 1:
1
Sample Output 1:
1
Sample Input 2:
1000
Sample Output 2:
4
Comments
Post a Comment