Java Full Coding
Java Full Coding
Java Full Coding
IRC_JAVA_COD_BASICS
Attempt : 1
Total Mark : 150
Marks Obtained : 150
Section 1 : CODING
1. Problem statement:
eg:
Input
Hi
Welcome
Output
Hi and Welcome
Answer
import java.util.Scanner;
class Hi
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
String a,b;
a=obj.nextLine();
b=obj.nextLine();
System.out.println(a+" and "+b);
}
}
2. Problem Statement:
Answer
import java.util.Scanner;
class Hi
{
public static void main(String[] b)
{
Scanner obj=new Scanner(System.in);
int a;
a=obj.nextInt();
System.out.println(a);
}
}
3. Problem Statement:
Answer
import java.util.Scanner;
class Fl
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
float a=sc.nextFloat();
System.out.printf("%.3f\n",a);
System.out.printf("%.2f\n",a);
System.out.printf("%.1f",a);
}
}
4. Problem Statement:
Answer
import java.util.Scanner;
class Hi
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
char a=sc.next().charAt(0);
System.out.print(a);
}
}
5. Problem Statement:
Write a java program to convert the integer data type to float data type.
Answer
import java.util.Scanner;
class Hi
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
System.out.print((float)a);
}
}
6. Problem Statement:
Answer
import java.util.Scanner;
class Hi
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
System.out.print((char)a);
}
}
7. Problem Statement:
Answer
import java.util.Scanner;
class Hi
{
public static void main(String s[])
{
Scanner sc=new Scanner(System.in);
char a=sc.next().charAt(0);
System.out.print((int)a);
}
}
As a part of this requirement, Nikhil wanted to write a piece of code for his
company’s Examly Event Management System that will display customized
welcome messages by taking Customers’ name as input. Help Nikhil on
the task.
Answer
Main.java
import java.util.*;
class Main {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
System.out.println("Hello "+a+" ! Welcome to Examly Event Management
System");
}
}
9. Problem statement:
Write a simple code by declaring three variables where two variables are of
integer type and one variable in double. Add the two integer variables and
store the result in the remaining variable(double).
Answer
Main.java
import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
double b=sc.nextDouble();
System.out.print(a+b);
}
}
Write a java program to get different types of data from the user and
display the values.
Question Instructions:
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
double b=sc.nextDouble();
boolean c=sc.nextBoolean();
char d=sc.next().charAt(0);
sc.nextLine();
String e=sc.nextLine();
System.out.println("Integer value = "+a);
System.out.println("Double value = "+b);
System.out.println("Boolean value = "+c);
System.out.println("char value = "+d);
System.out.println("String value = "+e);
}
}
Write a program that will get the input of the event details like name of the
event, type of the event, number of people expected, a string value (Y/N)
telling whether the event is going to be a paid entry and the projected
expenses (in lakhs) for the event. The program should then display the
input values as formatted output.
Question Instructions:
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
String b=sc.nextLine();
int c=sc.nextInt();
char d=sc.next().charAt(0);
float e=sc.nextFloat();
System.out.println("Event Name : "+a);
System.out.println("Event Type : "+b);
System.out.println("Expected Count : "+c);
System.out.println("Paid Entry : "+d);
System.out.printf("Projected Expense : "+e+"L");
}
}
Write a program to obtain and display the newly joined student name and
age detail.
Question Instructions:
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
int b=sc.nextInt();
System.out.print(a+" age is "+b);
}
}
Question Instructions:
"Pine Tree" Company has signed up a big time Event Management deal
from the Rotary Youth Club for a Trade Fair organized at Codissia Complex,
wherein all startup companies in the Software industry are demonstrating
their latest products and services and meet with industry partners and
Customers.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
System.out.print("Number of events hosted in Codissia is "+a);
}
}
Answer
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int d=sc.nextInt();
int e=a+b+c+d;
float k=(float)e;
float f=(float)a/1000;
float g=(float)b/1000;
float h=(float)c/1000;
float i=(float)d/1000;
System.out.printf("Total expenses : Rs.%.2f",k);
System.out.printf("\nBranding expenses percentage : %.2f",f);
System.out.print("%");
System.out.printf("\nTravel expenses percentage : %.2f",g);
System.out.print("%");
System.out.printf("\nFood expenses percentage : %.2f",h);
System.out.print("%");
System.out.printf("\nLogistics expenses percentage : %.2f",i);
System.out.print("%");
}
}
IRC_JAVA_COD_OPERATORS
Attempt : 1
Total Mark : 200
Marks Obtained : 200
Section 1 : CODING
If Somu eats x of his apples and Ramu eats y of Somu's oranges, how
many apples and oranges are left in total?
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int m1=sc.nextInt();
int n1=sc.nextInt();
int x=sc.nextInt();
int y=sc.nextInt();
System.out.printf("%d %d",(m+m+m1-x),(n+n+n1-y));
}
}
Answer
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int c=(a-800)/130;
int d=c+10;
System.out.print("Number of weekday hours is "+d);
System.out.print("\nNumber of weekend hours is "+c);
}
}
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n1=sc.nextInt();
float r1=sc.nextFloat();
int n2=sc.nextInt();
float r2=sc.nextFloat();
int n3=sc.nextInt();
float r3=sc.nextFloat();
float r=((n1*r1)+(n2*r2)+(n3*r3))/(n1+n2+n3);
System.out.printf("The overall average rating for the show is %.2f",r);
}
}
4. Problem Statement:
Note: Display the output to two decimal places and round the remuneration
value.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
double a=sc.nextDouble();
double b=sc.nextDouble();
double c=b-a;
double d=c*25;
System.out.printf("%.2f",c);
System.out.printf("\n%.0f",d);
}
}
6. Problem Statement:
Pranav, an enthusiastic kid visited the "Fun Fair 2017" along with his family.
His father wanted him to purchase entry tickets from the counter for his
family members. Being a little kid, he is just learning to understand units of
money. Pranav has paid some amount of money for the tickets but he
wants your help to give him back the change of Rs. N using the minimum
number of rupee notes.
Consider a currency system in which there are notes of seven
denominations, namely, Rs. 1, Rs. 2, Rs. 5, Rs. 10, Rs. 50, Rs. 100. If the
change is given to Pranav Rs. N is input, write a program to computer
smallest number of notes that will combine to give Rs. N.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b,c,d,e,f,g,h;
b=a/100;a%=100;
c=a/50;a%=50;
d=a/10;a%=10;
e=a/5;a%=5;
f=a/2;a%=2;
g=a;
h=b+c+d+e+f+g;
System.out.print(h);
}
}
7. Problem statement
Alice wanted to start a business and she was looking for a venture
capitalist. Through her friend Bob, she met the owner of a construction
company who is interested to invest in an emerging business. Looking at
the business proposal, the owner was very much impressed with Alice's
work. So he decided to invest in Alice's business and hence gave a green
signal to go ahead with the project. Alice bought Rs. X for a period of Y
years from the owner at R% interest per annum. Find the rate of interest
and the total amount to be given by Alice to the owner. The owner
impressed by the proper repayment of the financed amount decides to give
a special offer of 2% discount on the total interest at the end of the
settlement. Find the amount given back by Alice and also find the total
amount. (Note: All rupee values should be in two decimal points).
Example 1
Input
100
1
10
Output
10.00
110.00
0.20
109.80
Explanation
When we substitute the values of the principal amount, rate of interest, and
number of years in the Simple Interest formula we get the output values.
Example 2
Input
40
1
10
Output
4.00
44.00
0.08
43.92
Explanation
When we substitute the values of the principal amount, rate of interest, and
a number of years in the Simple Interest formula in order to get the output
values.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
float p=sc.nextInt();
float n=sc.nextInt();
float r=sc.nextInt();
float i=(p*n*r)/100;
System.out.printf("%.2f",i);
System.out.printf("\n%.2f",i+p);
System.out.printf("\n%.2f",(i*2/100));
System.out.printf("\n%.2f",(i+p)-(i*2/100));
}
}
8. Problem statement
Dhoni joined the group of 3 Musketeers and now their group is called four
Musketeers. Meanwhile, Dhoni also moved to a new house in the same
locality nearby to the other three. Currently, the houses of Sachin, Dravid
and Ganguly are located in the shape of a triangle. When the three
musketeers asked Dhoni about the location of his house, he said that his
house is equidistant from the houses of the other 3. Can you please help
them find out the location of the house? Given the 3 locations {(a1,b1),
(a2,b2) and (a3,b3)} of a triangle, write a program to determine the point
which is equidistant from all the 3 points.
Example 1
Input
2
4
10
15
5
8
Output
5.66667
9
Explanation
Apply the formula to find the equidistant form 3 points in order to obtain
the output.
Example 2
Input
4
3
33
12
1
2
Output
12.6667
5.66667
Explanation
Apply the formula to find the equidistant form 3 points in order to obtain
the output.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
float a1=sc.nextFloat();
float b1=sc.nextFloat();
float a2=sc.nextFloat();
float b2=sc.nextFloat();
float a3=sc.nextFloat();
float b3=sc.nextFloat();
float m=(a1+a2+a3)/3;
float n=(b1+b2+b3)/3;
System.out.println(m);
System.out.println(n);
}
}
9. Problem statement
Aamir Khan is suffering from short-term memory loss and he has forgotten
how to add, subtract, multiply, modulo division and division. Write a
program to help Aamir to perform the basic arithmetic operations.
Input
5
2
Output
7
3
10
1
2
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a%b);
System.out.println(a/b);
}
}
Question Instructions:
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
double b=1.8*a+32;
System.out.printf("%.1f",b);
}
}
Question Instructions:
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.print(a+b);
}
}
12. Write a program to find the square, cube, and square root of a number.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
double b=Math.sqrt(a);
System.out.println("Square of "+a+" is: "+a*a+".0");
System.out.println("Cube of "+a+" is: "+a*a*a+".0");
System.out.println("Square Root of "+a+" is: "+b);
}
}
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=a|b;
System.out.print(c);
}
}
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=a<<2;
System.out.print(b);
}
}
15. Given two integers N1 and N2, interchange the values of the variables
without using a third variable and print it.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.printf("%d %d",b,a);
}
}
Trade Fairs are important for companies to present their products and to
get in touch with its customers and business parties. One such grandeur
Trade Fair Event was organized by the Confederation of National Large
Scale Industry.
Number of people who attended the event on the first day was x. But as
days progressed, the event gained good response and the number of
people who attended the event on the second day was twice the number of
people who attended on the first day. Unfortunately due to heavy rains on
the third day, the number of people who attended the event was exactly
half the number of people who attended on the first day.
Given the total number of people who have attended the event in the first 3
days, find the number of people who have attended the event on day 1, day
2 and day 3.
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=(a*2)/7;
System.out.println("Number of attendees on day 1 : "+b);
System.out.println("Number of attendees on day 2 : "+(b*2));
System.out.println("Number of attendees on day 3 : "+(b/2));
}
}
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
double m=sc.nextDouble();
double n=sc.nextDouble();
double c=Math.pow(m,n);
System.out.printf("%.2f",c);
}
}
18. Write a program to obtain the length and breadth of a triangle as input
and calculate the area and perimeter.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("Area : "+(a*b));
System.out.println("Perimeter : "+(2*(a+b)));
}
}
19. If the marks of Alice in 3 subjects are mark1, mark2, mark3. Calculate
the total and average.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int m1=sc.nextInt();
int m2=sc.nextInt();
int m3=sc.nextInt();
int t=m1+m2+m3;
float avg=t/3;
System.out.println("Total : "+t);
System.out.printf("Average : %.2f",avg);
}
}
20. The Acme Tennis Ball Company is designing a new box to ship its
products. The marketing department wants a triangular box that can hold
4 balls, as in the illustration below. The balls fit exactly inside the box, just
touching all three walls and the end caps of the container. All 3 walls of the
box are the same size (equilateral triangle). Assume a tennis ball is 6 cm in
diameter, and ignore the thickness of the box material. What will be the
length and breadth of the material used to make the box? Write a program
that finds the measure of a side of the box. Program must also display the
length and breadth of the material used to make the triangular box.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
double side=a*(Math.sqrt(3));
double l=side*3;
double b=a*4;
System.out.printf("Side : %.2f",side);
System.out.printf("\nLength : %.2f",l);
System.out.printf("\nBreadth : %.2f",b);
}
}
IRC_JAVA_COD_CS_DECISION
Attempt : 1
Total Mark : 150
Marks Obtained : 150
Section 1 : CODING
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if(a<0)
System.out.println("Invalid");
else if(a>=85)
System.out.print("A");
else if(a>=75)
System.out.print("B");
else if(a>=65)
System.out.print("C");
else
System.out.print("D");
}
}
2. It is IPL season and the Preity's favorite team is "Kings XI Punjab". She
decided to check with Astrologer to know the performance of players in
advance. Astrologer asked the zodiac sign of each player. But Preity knows
only date of birth. So astrologer suggests to check the below chart and tell
the zodiac sign. So Preity asked Manish, her personal assistant to do this
task. Manish is good at programming and he decided to solve this using
Java program. Help Manish to complete this task. Tell if there is any Invalid
Date/Month.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int date=sc.nextInt();
int mon=sc.nextInt();
if(date>31||mon>12)
System.out.print("Invalid Date/Month");
else
{
if(mon==1)
{
if(date<=19)
System.out.print("Astrological sign for "+date+"-"+mon+" is Capricorn");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Aquarius");
}
else if(mon==2)
{
if(date<=18)
System.out.print("Astrological sign for "+date+"-"+mon+" is Aquarius");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Pisces");
}
else if(mon==3)
{
if(date<=20)
System.out.print("Astrological sign for "+date+"-"+mon+" is Pisces");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Aries");
}
else if(mon==4)
{
if(date<=19)
System.out.print("Astrological sign for "+date+"-"+mon+" is Aries");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Taurus");
}
else if(mon==5)
{
if(date<=20)
System.out.print("Astrological sign for "+date+"-"+mon+" is Taurus");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Gemini");
}
else if(mon==6)
{
if(date<=20)
System.out.print("Astrological sign for "+date+"-"+mon+" is Gemini");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Cancer");
}
else if(mon==7)
{
if(date<=22)
System.out.print("Astrological sign for "+date+"-"+mon+" is Cancer");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Leo");
}
else if(mon==8)
{
if(date<=22)
System.out.print("Astrological sign for "+date+"-"+mon+" is Leo");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Virgo");
}
else if(mon==9)
{
if(date<=22)
System.out.print("Astrological sign for "+date+"-"+mon+" is Virgo");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Libra");
}
else if(mon==10)
{
if(date<=22)
System.out.print("Astrological sign for "+date+"-"+mon+" is Libra");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Scorpio");
}
else if(mon==11)
{
if(date<=21)
System.out.print("Astrological sign for "+date+"-"+mon+" is Scorpio");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is
Sagittarius");
}
else if(mon==12)
{
if(date<=21)
System.out.print("Astrological sign for "+date+"-"+mon+" is
Sagittarius");
else
System.out.print("Astrological sign for "+date+"-"+mon+" is Capricorn");
}
}
}
}
3. Lucky Winner
It was the inaugural ceremony of "Fantasy Kingdom" Amusement park and
the park Management has announced some lucky prizes for the visitors on
the first day. Based on this, the visitors whose ticket number has the last
digit as 3 or 8, are declared as lucky winners and attracting prizes are
awaiting to be presented for them.
Write a program to find if the last digit of the ticket number of visitors is 3
or 8.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int s=a%10;
if(s==3||s==8)
System.out.print("Lucky Winner");
else
System.out.print("Not a Lucky Winner");
}
}
Status : Correct Marks : 10/10
4. Ticket type
"FantasyKingdom" 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 craziest
rides and as a special feature of the park, the park Authorities has placed
many Ticketing Kiosks at the entrance which would facilitate the public to
purchase their entrance tickets and ride tickets.
The Entrance Tickets are to be issued typically based on age, as there are
different fare for different age groups. There are 2 types of tickets – Child
ticket and Adult ticket. If the age given is less than 15, then Child ticket is
issued whereas for age greater than equal to 15, Adult ticket is issued.
Write a piece of code to program this requirement in the ticketing kiosks.
Answer
import java.util.*;
class Main
{
public static void main(String[] a)
{
Scanner sc=new Scanner(System.in);
int age=sc.nextInt();
if(age>=15)
System.out.print("Adult Ticket");
else
System.out.print("Child Ticket");
}
}
5. Triangle Game
The Westland Game Fair is the premier event of its kind for kids interested
in some intellectual and cognitive brain games. Exciting games were
organized for kids between age group of 8 and 10. One such game was
called the "Triangle game", where different number boards in the range 1 to
180 are available. Each kid needs to select three number boards, where the
numbers on the boards correspond to the angles of a triangle.
If the angles selected by a kid forms a triangle, he/she would receive Prize
1. If the angles selected by a kid forms a right triangle, he/she would
receive Prize 2. If the angles selected by the kids form an equilateral
triangle, he/she would receive Prize 3. If the angles selected by a kid do
not form even a triangle, then he/she will not receive any prizes. Write a
program for the organizers to fetch the result based on the number boards
selected by the kids.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
if((a+b+c)!=180)
System.out.print("No prize");
else
{
if(a==90||b==90||c==90)
System.out.print("Prize 2");
else if(a==b&&a==c)
System.out.print("Prize 3");
else
System.out.print("Prize 1");
}
}
}
6. Grades of Rides
“AquaticaCarnival” is the most successful event dedicated to children and
families. The Event has more than 20 rides for children and adults and the
organizers always ensure not to compromise on the safety of the visitors.
To ensure the safety of the rides, the organizers have graded the rides in
the fair according to the following conditions:
Hurl Factor must be greater than 50.
Spin Factor must be greater than 60.
Speed factor must be greater than 100.
Answer
import java.util.*;
class Main
{
public static void main(String[] a)
{
Scanner sc=new Scanner(System.in);
int hurl=sc.nextInt();
int spin=sc.nextInt();
int speed=sc.nextInt();
if(hurl>50&&spin>60&&speed>100)
System.out.print("10");
else if(hurl>50&&spin>60)
System.out.print("9");
else if(spin>60&&speed>100)
System.out.print("8");
else if(hurl>50&&speed>100)
System.out.print("7");
else if(hurl>50||spin>60||speed>100)
System.out.print("6");
else
System.out.print("5");
}
}
7. Calendar Quiz
Super Quiz Bee is a famous quiz Competition that tests students on a wide
variety of academic subjects. This week’s participants were kids of age 12
to 15 and the quiz questions were based on Gregorian calendar.
In the first round of the competition, the Host of the event told the
participants that it was Monday on the date 01/01/2001. Later he
questioned each one of the participant what would be the day on the 1st
January, giving them a particular year. Write a program to help the Host
validate the answers given by the participants.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int yr=sc.nextInt();
int dY=1900;
int c=yr-dY- 1;
int leap=c/4;
int nonLeap=c-leap;
int totalDays=(365*nonLeap)+(366*leap)+ 1;
int day=totalDays%7;
if(day==0)
System.out.print("Monday");
else if(day==1)
System.out.print("Tuesday");
else if(day==2)
System.out.print("Wednesday");
else if(day==3)
System.out.print("Thursday");
else if(day==4)
System.out.print("Friday");
else if(day==5)
System.out.print("Saturday");
else if(day==6)
System.out.print("Sunday");
}
}
8. Hanging Bridge
At the annual "KrackerJack Karnival", there was a newest attraction ever in
the City, the "Hanging Bridge". Visitors will be able to walk 200ft on the
bridge, hanging around 50ft above the ground, and enjoy a wide-angle view
of the breathtaking greenery.
The Hanging Bridge was inaugurated successfully in co-ordination with the
Event Manager Rahul. There is a limit on the maximum number of people
on the bridge and Rahul has to now ensure the count of people on the
bridge currently should not exceed the limit. He then approximately
estimated that C adults and D kids who came to the show, were on the
hanging bridge. He also noticed that there are L legs of the people
touching the bridge.
Rahul knows that kids love to ride on the adults and they might ride on the
adults, and their legs won't touch the ground and hence he would miss
counting their legs. Also Rahul knew that the adults would be strong
enough to ride at max two kids on their back.
Rahul is now wondering whether he counted the legs properly or not.
Specifically, he is wondering is there some possibility of his counting being
correct. Please help Rahul in finding it.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int d=(a*2)+(b*2);
if(c==d)
System.out.print("yes");
else
System.out.print("no");
}
}
9. Salary Computation
Danny has recently got his job offer as an Event Concept Creator at Sparsh
Event Services. The Company has sent him a detailed salary structure with
details of his basic salary, HRA and DA. The Company has promised to pay
him as under:
If his basic salary is less than Rs. 15000, then HRA = 15% of basic salary
and DA = 90% of basic salary.
If his basic salary is either equal to or above Rs. 15000, then HRA = Rs.
5000 and DA = 98% of basic salary.
If the Danny’s salary is given as input, write a program to find his gross
salary.
Note : Gross Salary = Basic Salary+HRA+DA
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
double basic=sc.nextDouble();
double hra,da,gross;
if(basic<15000)
{
hra=basic*0.15;
da=basic*0.9;
gross=basic+hra+da;
System.out.printf("%.2f",gross);
}
else if(basic>=15000)
{
hra=5000;
da=basic*0.98;
gross=basic+hra+da;
System.out.printf("%.2f",gross);
}
}
}
10. Write a program to check whether the given year is leap year or not.
Note: Use nested if else statement.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int yr=sc.nextInt();
int a=yr%4;
if(a==0)
System.out.println(yr+" Leap year");
else
System.out.println(yr+" Not leap year");
}
}
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
char a=sc.next().charAt(0);
switch(a)
{
case 'E':
case 'e':
System.out.print("Early Bird Ticket");break;
case 'D':
case 'd':
System.out.print("Discount Ticket");break;
case 'V':
case 'v':
System.out.print("VIP Ticket");break;
case 'S':
case 's':
System.out.print("Standard Ticket");break;
case 'C':
case 'c':
System.out.print("Children Ticket");break;
}
}
}
Answer
Main.java
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
if(a>b&&a>c)
System.out.print(a);
else if(b>a&&b>c)
System.out.print(b);
else
System.out.print(c);
}
}
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int temp=sc.nextInt();
if(temp<0)
System.out.print("Freezing weather");
else if(temp<10)
System.out.print("Very cold weather");
else if(temp<20)
System.out.print("Cold weather");
else if(temp<30)
System.out.print("Normal in temperature");
else if(temp<40)
System.out.print("Its hot");
else if(temp>=40)
System.out.print("Its very hot");
}
}
The renowned book fair of the season "Publishers Federation Book Expo"
is back, it promises to be bigger and better with a spread of about a million
books on display. It is organized in a wide space this year on the topmost
floor N of Hotel Grand Regency.
Williams, an ardent book lover visits the fair and wants to minimize the
time it takes him to go from the N-th floor to ground floor. He can either
take the elevator or the stairs.
The stairs are at an angle of 45 degrees and Williams's velocity is V1 m/s
when taking the stairs down. The elevator on the other hand moves with a
velocity V2 m/s. Whenever an elevator is called, it always starts from
ground floor and goes to N-th floor where it collects Williams (collecting
takes no time), it then makes its way down to the ground floor with
Williams in it.
The elevator cross a total distance equal to N meters when going from N-
th floor to ground floor or vice versa, while the length of the stairs is sqrt(2)
* N because the stairs is at angle 45 degrees. Williams has requested your
help to decide whether he should use stairs or the elevator to minimize his
travel time. Can you help him out?
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int v1=sc.nextInt();
int v2=sc.nextInt();
if(v2<v1*(float)Math.sqrt(2))
System.out.print("Stairs");
else
System.out.print("Elevator");
}
}
15. Carrom
If Cricket is the most popular outdoor game in India, Carrom is not too
behind as one of most played indoor games.
Let's now make use of our knowlege in operators & conditional
statements to compute the points scored at the end of a round in Carrom
Game.
Carrom is a board game where two participants (teams) play. It consists of
9 white coins, 9 black coins and a red coin. The first team that finishes all
their coins wins (given that red has been pocketed by one of the teams).
The points are awarded based on the number of left-over coins of the
opposition (loser) in the board. If the winning team has pocketed the red,
they get an additional 5 points. Write a program to compute the score of
winner at the end of a round.
If the number of coins left on the board is either less than 1 or greater than
9 display "Invalid Input".
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if(a<1 || a>9){
System.out.println("Invalid Input");
System.exit(0);}
char b=sc.next().charAt(0);
if(b=='y')
System.out.print(a+5);
else
System.out.print(a);
}
}
IRC_JAVA_COD_CS_LOOPING
Attempt : 1
Total Mark : 150
Marks Obtained : 150
Section 1 : CODING
1. Problem Statement :
Lucas Sequence
a = 0, b=0, c=1 are the 1st three terms. All other terms in the Lucas
sequence are generated by the sum of their 3 most recent predecessors.
Write a program to generate the first n terms of a Lucas Sequence.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a=0,b=0,c=1,i,d;
System.out.print(a+" "+b+" "+c);
for(i=3;i<n;i++)
{
d=a+b+c;
System.out.print(" "+d);
a=b;
b=c;
c=d;
}
}
}
2. Problem Statement :
Trendy Numbers
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt(), d, temp=a;
if(a>99 && a<1000)
{
temp/=10;
d=temp%10;
if(d%3==0)
System.out.print(a+" is trendy number");
else
System.out.print(a+" is not a trendy number");
}
else
System.out.print(a+" is not a trendy number");
}
}
Status : Correct Marks : 10/10
3. Problem Statement:-
Write a program to generate the first 'n' terms of the following series 1, 4, 9,
16, 25,...
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i;
for(i=1;i<=n;i++)
{
System.out.print((i*i)+" ");
}
}
}
4. Problem Statement:-
Write a program to generate the first 'n' terms of the following series 6, 11,
21, 36, 56,...
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i;
int a=6,b=5;
for(i=1;i<=n;i++)
{
System.out.print(a+" ");
a+=b;
b+=5;
}
}
}
5. Problem Statement :
Kaprekar Number
Consider an n-digit number k. Square it and add the right n digits to the left
n or n-1 digits. If the resultant sum is k, then k is called a Kaprekar number.
For example, 9 is a Kaprekar number since 92 = 81 & 8+1=9. and 297
is a Kaprekar number since 2972 = 88209 & 88+209 = 297
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),count=0,left=0,right=0;
int a=n*n;
int temp=a,temp2=a,fp=0,sp=0;
while(temp!=0)
{
temp/=10;
count++;
}
if(count%2!=0)
{
left=count/2;
right=count-left;
}
else
left=right=count/2;
sp=temp2%((int)Math.pow(10,right));
fp=temp2/((int)Math.pow(10,right));
System.out.print((sp+fp)==n?"Kaprekar Number":"Not a Kaprekar Number");
}
}
6. Problem Statement:-
Write a program to generate the first n terms in series 3, 9, 27, and 81,...
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i;
for(i=1;i<=n;i++)
System.out.print((int)Math.pow(3,i)+" ");
}
}
7. Problem Statement :
Target Practice
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int s1=sc.nextInt();
int s2=sc.nextInt();
int s3=sc.nextInt();
if(s1==n)
System.out.print("The number of turns is 1");
else if(s1+s2==n)
System.out.print("The number of turns is 2");
else if(s2+s3==n)
System.out.print("The number of turns is 3");
}
}
8. Problem Statement:-
Write a program to generate the first 'n' terms of the following series 0.5,
1.5, 4.5, 13.5,...
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i;
double a=0.5;
for(i=0;i<n;i++)
{
System.out.print(a+" ");
double d=Math.pow(3,i);
a+=d;
}
}
}
9. Problem Statement :
SPECIAL NUMBER
Write a program to find all special numbers between given range m and
n(both inclusive). Assume that m and n are 2-digit numbers.
A 2-digit number is said to be a special number if the sum of its digits and
the products of its digits is equal to the number itself.
The digits in 19 are 1 and 9. The sum of the digits is 10 and the product of
the digits is 9.
10+9 = 19.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int i,f,l,sum,pro,tot;
for(i=a;i<b;i++)
{
l=i%10;
f=i/10;
sum=l+f;
pro=l*f;
tot=sum+pro;
if(tot==i)
System.out.println(tot);
}
}
}
Write a program to generate the first 'n' terms of the following series 121,
225, 361,...
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,a=11;
for(i=0;i<n;i++)
{
System.out.print((a*a)+" ");
a+=4;
}
}
}
Status : Correct Marks : 10/10
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int i;
for(i=a;i<=b;i++)
{
System.out.print(a+"\n");
a++;
}
}
}
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,a;
for(i=1;i<=n;i++)
{
if(i%2==1)
{
a=(i*i)-1;
System.out.print(a+" ");
}
else
{
a=(i*i)-2;
System.out.print(a+" ");
}
}
}
}
Write a program to generate the first 'n' terms of the following series 4, 5, 9,
18, 34,...
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,a=4;
for(i=1;i<=n;i++)
{
System.out.print(a+" ");
a+=(i*i);
}
}
}
Write a program to generate the first 'n' terms of the following series 1, 2, 3,
6, 9, 18, 27,...
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,a=1,b=2;
System.out.print(a+" "+b+" ");
for(i=3;i<=n;i++)
{
if(i%2==1){
a*=3;
System.out.print(a+" ");}
else{
b*=3;
System.out.print(b+" ");}
}
}
}
Handshakes
It was Stefan's first day at school. His teacher Elena Gilbert asked the
students to meet every other student in the class and introduce
themselves. The teacher asked them to handshake each other when they
meet. If there are n number of students in the class then find the total
number of handshakes made by the students.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a=n*(n-1)/2;
System.out.print(a);
}
}
IRC_JAVA_COD_CS_PATTERN
Attempt : 1
Total Mark : 100
Marks Obtained : 100
Section 1 : CODING
1. Problem statement:
write a java program to print this pattern.
*
**
***
****
*****
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
System.out.print("* ");
System.out.print("\n");
}
}
}
2. Problem statement:
Write a java program to print this pattern.
*****
****
***
**
*
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,j;
for(i=n-1;i>=0;i--)
{
for(j=i;j>=0;j--)
System.out.print("* ");
System.out.print("\n");
}
}
}
Status : Correct Marks : 10/10
3. Problem statement:
Write a java program to print the pattern.
*
**
***
****
*****
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,j;
for(i=0;i<n;i++)
{
for(j=0;j<(n-i);j++)
System.out.print(" ");
for(j=0;j<=i;j++)
System.out.print("*");
System.out.println();
}
}
}
4. Problem statement:
Write a java program to print a half pyramid using numbers.1
12
123
1234
12345
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(j+" ");
System.out.print("\n");
}
}
}
5. Problem statement:
Write a program to print half pyramid using alphabets.
Input: 5
Output
A
BB
CCC
DDDD
EEEEE
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,j;
char a=65;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(a+" ");
}
System.out.print("\n");
a++;
}
}
}
6. Problem statement:
Write a java program to print the pascal's triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,j,a=1;
for(i=0;i<n;i++)
{
for(int space=1;space<n-i;space++)
System.out.print(" ");
for(j=0;j<=i;j++)
{
if(j==0||i==0)
a=1;
else
a=a*(i-j+1)/j;
System.out.printf("%4d",a);
}
System.out.println();
}
}
}
7. Problem statement:
Write a java program to print Floyd's Triangle.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a=sc.nextInt(),i,j;
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
System.out.print(a+" ");
a++;
}
System.out.println();
}
}
}
8. Problem statement:
Write a java program to program for half diamond pattern printing using
numbers and stars.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,j,a=1;
for(i=0;i<n;i++)
{
int k=0;
for(j=0;j<n;j++)
{
if(i>=j)
System.out.print(a);
if(k<i){
System.out.print("*");
k++;}
}
a++;
System.out.println();
}
a--;
for(i=0;i<n;i++)
{
int k=0;
for(j=0;j<n;j++)
{
if(i<=j)
{
System.out.print(a);
if(k<n-1-i){
System.out.print("*");
k++;
}
}
}
System.out.println();
a--;
}
}
}
9. Problem statement:
Write a java program to print a palindrome pyramid patterns using
numbers.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(j+" ");
for(j=i-1;j>=1;j--)
System.out.print(j+" ");
System.out.print("\n");
}
}
}
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,j,star=8;
int a=1,space=n,count=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=star;j++)
System.out.print("*");
for(j=1;j<i;j++)
System.out.print(a+"*");
System.out.print(a);
for(j=1;j<=star;j++)
System.out.print("*");
System.out.println();
a++;
star--;
}
}
}
}
}
}
}
if(c==0)
{
System.out.print("There are no repeating elements");
}
}
}
The Pan Am 73 flight from Bombay to New York en route Karachi and
Frankfurt was hijacked by a few Palestinian terrorists at the Karachi
International Airport.
int min=a[0],max=a[0];
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(a[i]<min)
{
min=a[i];
}
}
}
System.out.print(min+" ");
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(max<a[i])
{
max=a[i];
}
}
}
System.out.print(max);
}
}
7.New Year is shortly arriving and the students of St. Philip’s College
of Business are eager to receive the freshers for the coming year.
import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n,k;
n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
k=s.nextInt();
for(int i=0;i<n;i++)
{
if(a[i]==k)
{
System.out.print(i+" ");
break;
}
}
for(int i=n-1;i>=0;i--)
{
if(a[i]==k)
{
System.out.print(i);
break;
}
}
}
}
IRC_JAVA_COD_ARRAYS2D
Attempt : 1
Total Mark : 150
Marks Obtained : 150
Section 1 : Coding
Answer
// You are using Java
import java.lang.Math;
import java.util.*;
class v
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n,i,j,k=0;
n=in.nextInt();
int a[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
k=k+(a[i][j]*a[i][j]);
}
}
double r=Math.sqrt(k);
System.out.print((int)r);
}
}
Answer
// You are using Java
import java.util.*;
class v
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n,i,j,k;
n=in.nextInt();
int a[][]=new int[n][n];
int b[][]=new int[n][n];
int c[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Answer
// You are using Java
import java.util.*;
class v
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n,i,j;
n=in.nextInt();
int a[][]=new int[n][n];
int b[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
int sum=0;
sum=a[i][j]-b[i][j];
System.out.print(sum+" ");
}
System.out.println();
}
}
}
Answer
// You are using Java
import java.util.*;
class v
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n,m,i,j;
n=in.nextInt();
m=in.nextInt();
int a[][]=new int[n][m];
int b[][]=new int[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
b[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
int sum;
sum=a[i][j]+b[i][j];
System.out.print(sum+" ");
}
System.out.println();
}
}
}
5. Write a program to obtain a matrix and find the sum of its diagonal
elements.
Note: Only square matrix.
Answer
// You are using Java
import java.util.*;
class v
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n,m,i,j,s=0;
n=in.nextInt();
m=in.nextInt();
int a[][]=new int[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
{
s=s+a[i][j];
}
}
}
System.out.print(s);
}
}
6. Write a program to obtain a matrix and find the sum of the elements in
the lower triangular matrix(i.e., the elements on the diagonal and the lower
elements).
Note: Only square matrix
Answer
// You are using Java
import java.util.*;
class v
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n,m,i,j,s=0;
n=in.nextInt();
m=in.nextInt();
int a[][]=new int[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(j<=i)
{
s=s+a[i][j];
}
}
}
System.out.print(s);
}
}
7. Raja was arranging gift boxes in his store. He decided to arrange the
gift boxes in an order so that the number of gift boxes in each row is equal
to the row number number. Each gift box is numbered in ascending order.
He imagined how the arrangement would be.
Write a Java program to print such pattern.
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,k=2,count=0,n=in.nextInt();
for(i=0;i<n;i++)
{
if(count==0)
System.out.print("[1]");
else
{
System.out.print("["+k);
k++;
for(j=0;j<i;j++)
{
System.out.print(", "+k);
k++;
}
System.out.print("]");
}
count++;
System.out.println();
}
}
}
8. Snake Pattern:
Write a program to print the matrix in a zig_zag form.
Answer
import java.util.*;
class v
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int m,n,i,j;
m=in.nextInt();
n=in.nextInt();
int a[][]=new int[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<m;i++)
{
if(i%2==0)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
}
else
{
for(j=n-1;j>=0;j--)
{
System.out.print(a[i][j]+" ");
}
}
}
}
}
9. Write a program to obtain a matrix and find the sum of each row and
each column.
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,r_sum=0,c_sum=0,m=in.nextInt(),n=in.nextInt();
int a[][]=new int[m][n];
for(i=0;i<n;i++)
{
r_sum=0;
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
r_sum+=a[i][j];
}
System.out.println("Sum of the row "+i+" = "+r_sum);
}
for(i=0;i<n;i++)
{
c_sum=0;
for(j=0;j<n;j++)
{
c_sum+=a[j][i];
}
System.out.println("Sum of the column "+i+" = "+c_sum);
}
}
}
10. Write a program to obtain a matrix and find the sum of the elements in
the upper triangular matrix(i.e., the elements on the diagonal and the upper
elements).
Note: Only square matrix
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,sum=0,r=in.nextInt(),c=in.nextInt();
int a[][]=new int[r][c];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i<=j)
sum+=a[i][j];
}
}
System.out.print(sum);
}
}
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,count=0,coll=0,m=in.nextInt(),n=in.nextInt();
int a[][]=new int[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
count=0;
for(j=0;j<m;j++)
{
if(a[j][i]==1)
{
count++;
}
}
count-=1;
coll+=((count)*(count+1))/2;
}
System.out.print(coll);
}
}
12. Johnsy wants to create a matrix in which the elements are formed
differently. The elements are formed by adding the values of their index
positions. Write a program that obtains the order of the matrices and
creates a matrix by adding the values of their index positions.
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,m=in.nextInt(),n=in.nextInt();
int a[][]=new int[m][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=i+j;
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,n=in.nextInt();
int a[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=20)
{
if(a[i][j]>10)
{
System.out.println("No");
return;
}
}
}
}
System.out.println("Yes");
}
}
Neerja very well knew that she would not be able to evacuate all
passengers dodging the hijackers. So she wanted to hand over the
responsibility of evacuating in the senior citizens(above 60 years of age)
and children(below 18 years of age) in the flight to the mid-aged
passengers seated in the diagonals.
Given n the number of rows of seats and the number of seats in a row and
the ages of passengers in each seat can you find the number of mid-aged
passengers seated in the main diagonals.
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,j,count=0,n=in.nextInt();
int a[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
if(a[i][j]>18 && a[i][j]<60)
count++;
}
}
}
System.out.print(count);
}
}
She asked Ankit to add two matrix, Banu to subtract two matrix and Janu
to multiply two matrix. Ankit, Banu and Janu approached Karthick to
complete their task.
Karthick is ready to help all his friends with single program. So he asked
his friends to give a square matrix only. Help Karthick to write the program.
Answer
// You are using Java
import java.util.*;
class v
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n,i,j,k;
n=in.nextInt();
int a[][]=new int[n][n];
int b[][]=new int[n][n];
int c[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=in.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=in.nextInt();
}
}
System.out.print("Sum\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
int s=0;
s=a[i][j]+b[i][j];
System.out.print(s+" ");
}
System.out.println();
}
System.out.print("Difference\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
int sum=0;
sum=a[i][j]-b[i][j];
System.out.print(sum+" ");
}
System.out.println();
}
System.out.print("Multiply\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
IRC_JAVA_COD_Strings
Attempt : 1
Total Mark : 100
Marks Obtained : 100
Section 1 : Coding
Answer
// You are using Java
import java.util.Scanner;
class First{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int i,vcount=0;
for(i=0;i<str.length();i++)
{
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||
str.charAt(i)=='u')
{
vcount++;
}
}
System.out.println(vcount);
}
}
Status : Correct Marks : 10/10
2. Write a program to display the middle value of the string. If the string is
odd then display the single element in the middle, if even then display two
elements from the middle.
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String str=in.next();
int len=str.length();
if(len%2!=0)
{
System.out.println(str.charAt(len/2));
}
else
{
System.out.print(str.charAt((len/2)-1));
System.out.print(str.charAt(len/2));
}
}
}
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String str1=in.next();
StringBuilder str2=new StringBuilder(str1);
str2.reverse();
String str3=str2.toString();
if(str1.equals(str3))
{
System.out.print(str1+" :palindrome");
}
else
{
System.out.print(str1+" :not a palindrome");
}
}
}
Answer
// You are using Java
import java.util.Scanner;
class Four{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String nospaceStr=str.replaceAll("\\s","");
System.out.println(nospaceStr);
}
}
Answer
// You are using Java
import java.util.Scanner;
class Five{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str1=sc.nextLine();
String str2=sc.nextLine();
System.out.println(str1.compareTo(str2));
}
}
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,count=0;
String str=in.next();
int n=str.length();
for(i=0;i<n;i++)
{
if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o'
|| str.charAt(i)=='u')
{
if(count==0)
{
System.out.print(str.charAt(i));
count=1;
}
}
else
{
count=0;
System.out.print(str.charAt(i));
}
}
}
7. toLowerCase()
When we get the usernames for the user in their profiles, some may enter
in uppercase, some in lowercase, some in mixed cases. Now we don't want
that, we want uniformity in the usernames if we are gonna present a legible
report. So let's write a program that converts all the letters of the
username into lowercase.
Answer
// You are using Java
import java.util.Scanner;
class Seven{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String uppstr=sc.nextLine();
String uppstr1=uppstr.toLowerCase();
System.out.println(uppstr1);
}
}
8. toCharArray()
This method converts the given string into a character array i.e first it will
calculate the length of the given Java String including spaces and then
create an array of char type Create a main class and obtain a string,
convert it into an character array and print the same.
Answer
// You are using Java
import java.util.Scanner;
class Eight{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
System.out.println(str);
}
}
Create a driver class called Main. In the Main method, obtain the inputs
from the console (Refer I/O) and prompt whether the gibberish text is
present in the main text.
Answer
// You are using Java
import java.util.*;
import java.lang.*;
class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String str1,str2;
str1=in.nextLine();
str2=in.nextLine();
if(str1.contains(str2))
{
System.out.print("String is found in the sentence");
}
else
{
System.out.print("String is not found in the sentence");
}
String str3=str2.trim();
}
}
10. Write a program to validate domain names of the email address. The
fair organizers have listed the accepted domains as "com", "in", "net", and
"org". Write a program to validate email addresses that have the above
listed domain names.
Create a driver class called Main. In the Main method, obtain the inputs
from the console and validate the email address.
Answer
// You are using Java
import java.util.Scanner;
class Ten{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.print(s+"\n");
for(int i=0;i<s.length();i++)
{
if(s.contains("com")||s.contains("in")||s.contains("net")||s.contains("org"))
{
System.out.print("Valid email address");
return;
}
else
{
System.out.print("Invalid email address");
return;
}
}
}
}
IRC_JAVA_COD_Strings_Set2
Attempt : 1
Total Mark : 100
Marks Obtained : 100
Section 1 : Coding
Create a driver class called Main. In the Main method, obtain the inputs
from the console (Refer I/O) and prompt whether the gibberish text is
present in the main text.
Answer
// You are using Java
import java.util.*;
class Dk
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String str1,str2;
str1=in.nextLine();
str2=in.nextLine();
str2.trim();
if(str1.contains(str2))
{
System.out.print("String is found in the sentence");
}
else
{
System.out.print("String is not found in the sentence");
}
}
}
2. 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...".
Answer
// You are using Java
import java.util.Scanner;
class Dk
{
public static void main(String[] args)
{
Scanner ee = new Scanner(System.in);
String a = ee.nextLine();
int f =1;
for(int i=0;i<a.length()-2; i++)
{
if(a.charAt(i)==a.charAt(i+2)&&a.charAt(i)!=a.charAt(i+1))
{
continue;
}
else
{
f=0;
break;
}
}
if(f==1)
System.out.print("Yes");
else
System.out.print("No");
}
Answer
// You are using Java
import java.util.*;
class Dk
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String a=in.next();
if(a.substring(0,3).compareTo("+91")==0 && a.substring(3).length()==10)
{
System.out.print("Mobile number valid");
}
else
{
System.out.print("Mobile number invalid");
}
}
}
4. Camel case
Camel case (stylized as camelCase or CamelCase) is the practice of
writing compound words or phrases such that each word or abbreviation in
the middle of the phrase begins with a capital letter, with no intervening
spaces or punctuation. Event names should be entered in camel case
format. But many users failed to follow this convention. To maintain
uniformity, you have to change all the event names into camel case. Write
a program to convert event names to camel case format.
Create a driver class called Main. In the Main method, obtain the inputs
from the console and print the names of the events in camel case.
Answer
// You are using Java
import java.util.*;
class Dk
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String str=in.nextLine();
int i,c=0;
System.out.print(Character.toUpperCase(str.charAt(0)));
for(i=1;i<str.length();i++)
{
if(str.charAt(i)==' ')
c++;
else if(c==1)
{
System.out.print(Character.toUpperCase(str.charAt(i)));
c=0;
continue;
}
else
System.out.print(str.charAt(i));
}
}
}
Answer
// You are using Java
import java.util.Scanner;
class Dk
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int c=0;
String str1,str2;
str1 = in.next();
str2 = in.next();
int l = str1.length();
for(int i=0;i<l;i++)
{
if(str1.charAt(i)==str2.charAt(i))
{
if(str1.charAt(i)=='W')
System.out.print("B");
else
System.out.print("W");
}
else
{
c++;
if(c%2==0)
System.out.print(str2.charAt(i));
else
System.out.print(str1.charAt(i));
}
}
}
}
Answer
// You are using Java
import java.util.*;
class Dk
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,n=in.nextInt();
String str[]=new String[n];
for(i=0;i<n;i++)
{
str[i]=in.next();
}
if(str[n-1].equals("cookie"))
{
System.out.print("No");
System.exit(0);
}
for(i=0;i<n-1;i++)
{
if(str[i].equals("cookie")&&!(str[i+1].equals("juice")))
{
System.out.println("No");
System.exit(0);
}
}
System.out.println("Yes");
}
}
7. Adjacent characters
Given a string, write a program to compute a new string where identical
chars that are adjacent in the original string are separated from each other
by a "*"
Answer
import java.util.*;
class Dk
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String str=in.next();
char temp='.';
for(int i=0;i<str.length();i++)
{
if(temp==str.charAt(i))
{
System.out.print("*"+str.charAt(i));
temp=str.charAt(i);
continue;
}
temp=str.charAt(i);
System.out.print(str.charAt(i));
}
}
}
8. Caption Contest
Exeter Caption Contest is a competition open to all writers worldwide. The
entrants will have one day to compose and submit a caption that will be
based on the theme posted on the competition page.
Robin, a creative writer had penned two captions for the contest but he
unknowingly misplaced them. After searching long, he managed to locate
his captions, but some letters in them have become unreadable. The
captions were in two very old sheets of paper, each of which originally
contained a string of lowercase English letters. The strings on both the
sheets have equal lengths.
Robin would like to estimate the difference between these strings. Let's
assume that the first string is named S1, and the second S2. The
unreadable symbols are specified with the question mark symbol '?'. The
difference between the strings equals to the number of positions i, such
that S1i is not equal to S2i, where S1i and S2i denote the symbol at the i th
position in S1 and S2, respectively.
Robin would like to know the minimal and the maximal difference between
the two strings, if he changes all unreadable symbols to lowercase English
letters. Robin is not an expertise in programming and so he needs your
help solving this problem!
Answer
// You are using Java
import java.util.*;
class Dk
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String s1,s2;
s1=in.next();
s2=in.next();
int i,c=0,t=0;
for(i=0;i<s1.length();i++)
{
if(s1.charAt(i)!='?' && s2.charAt(i)!='?')
{
if(s1.charAt(i)!=s2.charAt(i))
{
c++;
t++;
}
}
else
{
t++;
}
}
System.out.print(c+" "+t);
}
}
Find the list of soldiers who are surviving at last or find if all of them are
killed.
Example:
soldiers: xAbcyAAbcbAbccz
pattern: Abc
Iteration:
0: xAbcyAAbcbAbccz
1: xyAAbcbAbccz
2: xyAbAbccz
3: xyAbcz
4: xyz
Output: xyz
Answer
import java.util.*;
class FindLeftSoldiers {
// You are using Java
static String defeatSoldiers(String soldiers,String pattern)
{
while(soldiers.contains(pattern))
{
soldiers = soldiers.replaceAll(pattern, "") ;
}
return soldiers ;
}
in.close();
if (result.length() == 0) {
System.out.println("Defeat");
} else {
System.out.println(result);
}
Answer
// You are using Java
import java.util.Scanner;
class Ten{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
for(int i=0;i<s.length();i++)
{
if(!Character.isDigit(s.charAt(i)))
{
System.out.print("0");
System.exit(0);
}
}
int i=Integer.parseInt(s);
System.out.println(i);
}
}
IRC_JAVA_COD_REGEX
Attempt : 1
Total Mark : 100
Marks Obtained : 100
Section 1 : CODING
1. Problem statement:
Write a java program to find whether the password is valid or invalid using
the regular expression.
Note:
Password should be less than or equal to 15 and more than 8 characters in
length.Password should contain at least one upper case and one lower
case alphabet. Password should contain at least one number.Password
should contain at least one special character.
Answer
import java.util.*;
import java.io.BufferedReader;
class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String str = in.next();
boolean b1=str.matches(".*(\\d).*");
boolean b2=str.matches(".*[a-z].*");
boolean b3=str.matches(".*[A-Z].*");
boolean b4=str.matches(".*(\\W).*");
boolean b5=str.matches(".{8,15}");
if(b1&&b2&&b3&&b4&&b5)
{
System.out.print(str+" is a valid password");
}
else
{
System.out.println(str+" is a invalid password");
}
}
}
2. Problem statement:
Write a java program to check whether the given content is present in the
pattern or not using the regex concept.
Answer
// You are using Java
import java.util.*;
import java.io.*;
class code
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String reg=sc.nextLine();
String str=sc.next();
boolean b;
if(reg.matches(str)==true)
{
b=true;
System.out.print(reg+" contains "+str+" : "+b);
}
else
{
b=false;
System.out.println(reg+" contains "+str+" : "+b);
}
}
}
3. Problem statement :
Write a Java program to check whether a string contains only a certain set
of characters (in this case a-z, A-Z, and 0-9)
Answer
// You are using Java
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
System.out.println(str);
if(str.matches("[a-zA-Z0-9]+"))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
Answer
// You are using Java
import java.util.*;
import java.io.*;
class demo
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
System.out.println(str);
if(str.matches(".*[0-9]$")==true)
{
System.out.println("Found a match!");
}
else
{
System.out.println("Not matched!");
}
}
}
5. Problem statement:
Write a Java program to count the number of vowels in a given string using
regular expressions.
Answer
// You are using Java
import java.util.*;
import java.io.*;
class demo
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
System.out.println("Original string: "+str);
String s1=str.replaceAll("[^aeiouAEIOU]","");
int c=s1.length();
System.out.println("New string: "+c);
}
}
6. Problem statement:
Write a java program to find the number of occurrences of characters from
the two strings.
Answer
// You are using Java
import java.util.*;
import java.io.*;
class main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
String s2=sc.nextLine();
int l1=s1.length();
int l2=s2.length();
String s3=s2.replaceAll(s1,"");
int l3=s3.length();
int t=(l2-l3)/l1;
System.out.println("The no of occurences: "+t);
}
}
7. Problem statement:
Write a regular expression to represent all valid identifiers in java language.
Rules:
The allowed characters are:
1. a to z, A to Z, 0 to 9, -,#
2. The 1st character should be an alphabet symbol only.
3. The length of the identifier should be at least 2.
Answer
// You are using Java
import java.util.*;
import java.util.regex.*;
class demo
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String s1="[a-zA-Z]{1}[a-zA-z0-9-#&&[^\\W]]{2,}";
int l=str.length();
String s2="";
if(l<2)
{
}
else
{
Pattern p=Pattern.compile(s1);
Matcher m=p.matcher(str);
while(m.find())
{
s2+=m.group();
}
if(str.compareTo(s2)==0)
{
System.out.println(str+":Valid Identifier");
}
else
{
System.out.println(str+":Invalid Identifier");
}
}
}
}
8. Problem statement:
Write a regular expression to represent all mobile numbers.
1. Should contain exactly 10 digits.
2. The 1st digit should be 7 to 9.
Answer
// You are using Java
import java.util.*;
import java.util.regex.*;
class main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String a=sc.next();
String p="[7-9][0-9]{9}";
if(Pattern.matches(p,a))
{
System.out.print(a+" : Valid Number");
}
else
{
System.out.print(a+" : Invalid Number");
}
}
}
9. Problem statement:
Write a java program finding data type of user input using Regular
Expression.
Answer
// You are using Java
import java.util.*;
import java.util.regex.*;
class main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String a=in.next();
if(Pattern.matches("[0-9]*",a))
{
System.out.print("The datatype of "+a+" is: java.lang.Integer");
}
else if(Pattern.matches("[0-9]+[.][0-9]+",a))
{
System.out.print("The datatype of "+a+" is: java.lang.Double");
}
else if(Pattern.matches("[0-9]{2}[/][0-9]{2}[/][0-9]{2,4}",a))
{
System.out.print("The datatype of "+a+" is: java.util.Date");
}
else if(Pattern.matches("[0-9]{2}[-][a-z]{3,}[-][0-9]{2,4}",a))
{
System.out.print("The datatype of "+a+" is: java.util.Date");
}
else
{
System.out.print("The datatype of "+a+" is: java.lang.String");
}
}
}
Answer
// You are using Java
import java.util.regex.*;
import java.util.*;
class demo
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
String s2=sc.nextLine();
String str=".*['](\\w*)['].*",fin;
Pattern p=Pattern.compile(str);
Matcher m1=p.matcher(s1);
Matcher m2=p.matcher(s2);
if(m1.matches())
{
System.out.println("First Extracted part: "+m1.group(1));
}
if(m2.matches())
{
System.out.println("Second Extracted part: "+m2.group(1));
}
}
}
IRC_JAVA_COD_DATEANDTIMEAPI
Attempt : 1
Total Mark : 50
Marks Obtained : 40
Section 1 : CODING
1. Problem statement:
Write a Java program to count the number of days between two given
years.
Answer
// You are using Java
import java.util.*;
import java.time.*;
class main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int init=sc.nextInt(),fin=sc.nextInt();
if(init>fin)
{
System.out.print("End year must be greater than first year!");
System.exit(0);
}
for(int i=init;i<=fin;i++)
System.out.println("Year: "+i+ " = " +(Year.isLeap(i)?366:365));
}
}
2. Problem statement:
Write a Java program to create a Date object using the Calendar class.
Note:
Jan -0 to Dec -11
Answer
// You are using Java
import java.util.*;
import java.util.Calendar;
class main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int y= sc.nextInt();
int m=sc.nextInt();
int d=sc.nextInt();
Calendar c=new GregorianCalendar(y,m,d);
System.out.println(c.getTime());
}
}
3. Problem statement:
Write a Java program to extract the date, and time from the date string.
Answer
// You are using Java
import java.util.*;
import java.time.*;
import java.text.*;
class demo
{
public static void main(String[] args) throws ParseException
{
Scanner sc=new Scanner(System.in);
String dt=sc.nextLine();
Date date1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dt);
String str=new SimpleDateFormat("MM/dd/yyyy, H:mm:ss").format(date1);
System.out.println(str);
}
}
4. Problem statement:
write a program to find the instant method of clock class.
Answer
-
Status : - Marks : 0/10
5. Problem statement:
Write a program to find the day and add the day to get the exact day of the
week.
Answer
// You are using Java
import java.util.*;
class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
}}