Java Full Coding

Download as pdf or txt
Download as pdf or txt
You are on page 1of 133

Sri Krishna College of Technology

Name :DHIYANESHWAR R Email :[email protected]


Roll no:727821TUCS043 Phone :9790266530
Branch :Sri Krishna College of Technology Department :CSE
Batch :2021-25 Degree :BE-CSE

2021_25 Batch_Fundamentals of Java_IRC

IRC_JAVA_COD_BASICS

Attempt : 1
Total Mark : 150
Marks Obtained : 150

Section 1 : CODING

1. Problem statement:

Write a program to display a string in following format.

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);
}
}

Status : Correct Marks : 10/10

2. Problem Statement:

Write a Java program to get the integer values and print

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);
}
}

Status : Correct Marks : 10/10

3. Problem Statement:

Write a java program to print the floating point value.

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);
}
}

Status : Correct Marks : 10/10

4. Problem Statement:

Write a Java program to print the character value.

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);
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

6. Problem Statement:

Write a Java program to find the conversion of integer value to character


value

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);
}
}

Status : Correct Marks : 10/10

7. Problem Statement:

Write a program to find the conversion of character to integer value.

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);
}
}

Status : Correct Marks : 10/10

8. Customized Welcome Message

Nikhil, the founder of “Pine Tree” company wished to design an Event


Management System that would let its Customers plan and host events
seamlessly via an online platform.

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");
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

10. Display Different Data Types

Write a java program to get different types of data from the user and
display the values.

Question Instructions:

Create a driver class named MainThe solution code should be written


inside the main method() of the Main class

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);
}
}

Status : Correct Marks : 10/10

11. Event Details


Be it a last-minute get-together, a birthday party or a corporate event, the
"Pine Tree" Event Management Company helps you plan and execute it
better and faster. Nikhil, the founder of the company wanted the Examly
Event Management System to get and display the event details from his
Customers for every new order of the Company.

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:

Create a driver class named Main.The solution code should be written


inside the main method() of the Main class

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");
}
}

Status : Correct Marks : 10/10

12. Display Student's Detail

Write a program to obtain and display the newly joined student name and
age detail.

Question Instructions:

Create a driver class named Main.The solution code should be written


inside the main method() of the Main class

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);
}
}

Status : Correct Marks : 10/10

13. Play with Typecasting


Write a simple code by declaring three variables where two variables are of
integer type and one variable is double. Multiply the two integer variables
and store the result in the remaining variable(double).

Question Instructions:

Create a driver class named Main.The solution code should be written


inside the main method() of the Main class
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((double)a*b);
}
}

Status : Correct Marks : 10/10

14. Number of events

"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.

Amphi Event Management System has to be modified to write a piece of


code that will get the input of the number of events to be hosted for the
Fair at Codissia from its users and display the same. Help the company to
accomplish the requirement.

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);
}
}

Status : Correct Marks : 10/10

15. Total Expenses for the Event

The prime functionality of an Event Management System is budgeting. An


Event Management System should estimate the total expenses incurred by
an event and the percentage rate of each of the expenses involved in
planning and executing an event. Nikhil, the founder of "Pine Tree" wanted
to include this functionality in his company’s Amphi Event Management
System and requested your help in writing a program for the same.
The program should get the branding expenses, travel expenses, food
expenses and logistics expenses as input from the user and calculate the
total expenses for an event and the percentage rate of each of these
expenses.

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("%");
}
}

Status : Correct Marks : 10/10


Sri Krishna College of Technology

Name :DHIYANESHWAR R Email :[email protected]


Roll no:727821TUCS043 Phone :9790266530
Branch :Sri Krishna College of Technology Department :CSE
Batch :2021-25 Degree :BE-CSE

2021_25 Batch_Fundamentals of Java_IRC

IRC_JAVA_COD_OPERATORS

Attempt : 1
Total Mark : 200
Marks Obtained : 200

Section 1 : CODING

1. Ramu and Somu are going on a picnic. Ramu packs m apples, n


oranges. Somu packs m1 more apples than Ramu and n1 more oranges
than Ramu.

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));
}
}

Status : Correct Marks : 10/10

2. Wisconsin State Fair


Wisconsin State Fair is one of the largest midsummer celebrations in the
Midwest Allis, showcasing the agriculture skills and prowess of the state.
The Event organizers hired few part-time employees to work at the fair and
the agreed salary paid to them are as given below:
Weekdays --- 80 / hour
Weekends --- 50 / hour
Justin is a part-time employee working at the fair. Number of hours Justin
has worked in the weekdays is 10 more than the number of hours he had
worked during weekends. If the total salary paid to him in this month is
known, write a program to estimate the number of hours he had worked
during weekdays and the number of hours he had worked during weekends.

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);
}
}

Status : Correct Marks : 10/10

3. WonderWorks Magic Show


The Magic Castle, the home of the Academy of Magical Arts at California
has organized the great ‘WonderWorks Magic Show’. 3 renowned
magicians were invited to mystify and thrill the crowd with their world’s
spectacular magic tricks. At the end of each of the 3 magicians’ shows, the
audience were requested to give their feedback in a scale of 1 to 10.
Number of people who watched each show and the average feedback
rating of each show is known. Write a program to find the average
feedback rating of the WonderWorks Magic show.

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);
}
}

Status : Correct Marks : 10/10

4. Problem Statement:

Area of an equilateral triangle:

Ragu's grandparents have their land in an equilateral triangle shape. They


have to sow apple seeds for cultivation. Calculate the total area of the land
so that they can buy the required amount of seeds. Round off the area to
two decimal places.
.
Answer
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
double s=sc.nextDouble();
double area=Math.sqrt(3)/4*(s*s);
System.out.format("%.2f",area);
}
}

Status : Correct Marks : 10/10

5. MILEAGE REMUNERATION CALCULATOR


Write a program that calculates mileage remuneration for a salesperson at
a rate of Rs.25 per mile. Your program should interact with the user in this
manner:
Sample Output 1: (Explanation)
Enter beginning odometer reading: 13505.2
Enter ending odometer reading: 13810.6
You traveled 305.4 miles. At Rs.25 per mile, your remuneration is Rs.7635

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);
}
}

Status : Correct Marks : 10/10

6. Problem Statement:

Pranav and Change

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);
}
}

Status : Correct Marks : 10/10

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));
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

10. Celsius to Fahrenheit

Write a program to convert Celsius to Fahrenheit. The formula is as


follows, F = 1.8 C + 32.

Question Instructions:

Create a driver class named Main.The solution code should be written


inside the main method() of the Main class

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);
}
}

Status : Correct Marks : 10/10

11. Find the total customers


Jeevan is running a sports club. He already had N number of customers
and due to the offer that he declared yesterday a lot of new members have
joined the club today. Your task here is to write a program to find the total
number of customers that he has.

Question Instructions:

Create a driver class named Main.The solution code should be written


inside the main method() of the Main class

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);
}
}

Status : Correct Marks : 10/10

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);

}
}

Status : Correct Marks : 10/10

13. Write a program to perform bitwise OR operation.

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);
}
}

Status : Correct Marks : 10/10

14. Write a program to left shift(signed) a value by 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=a<<2;
System.out.print(b);
}
}

Status : Correct Marks : 10/10

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);

}
}

Status : Correct Marks : 10/10

16. Trade Fair

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));
}
}

Status : Correct Marks : 10/10

17. Write a program to print the power of m raised to n.

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);
}
}

Status : Correct Marks : 10/10

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)));
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10


Sri Krishna College of Technology

Name :DHIYANESHWAR R Email :[email protected]


Roll no:727821TUCS043 Phone :9790266530
Branch :Sri Krishna College of Technology Department :CSE
Batch :2021-25 Degree :BE-CSE

2021_25 Batch_Fundamentals of Java_IRC

IRC_JAVA_COD_CS_DECISION

Attempt : 1
Total Mark : 150
Marks Obtained : 150

Section 1 : CODING

1. Write a program to display the grade of a student.If the mark is > 85


then display 'A', if mark > 75 then display 'B',mark > 65 then display 'C' for
others display 'D'.
Note: Use if else statement.

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");
}
}

Status : Correct Marks : 10/10

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");
}
}
}
}

Status : Correct Marks : 10/10

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");
}
}

Status : Correct Marks : 10/10

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");
}
}
}

Status : Correct Marks : 10/10

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.

The grades are as follows:


Grade is 10 if all three conditions are met.
Grade is 9 if conditions (i) and (ii) are met.
Grade is 8 if conditions (ii) and (iii) are met.
Grade is 7 if conditions (i) and (iii) are met.
Garde is 6 if only one condition is met.
Grade is 5 if none of three conditions are met.
Write a program display the grade of the rides, given the values of hurl
factor, spin factor and speed factor of the ride under consideration.

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");

}
}

Status : Correct Marks : 10/10

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");
}
}

Status : Correct Marks : 10/10

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");
}
}

Status : Correct Marks : 10/10

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);
}
}
}

Status : Correct Marks : 10/10

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");
}
}

Status : Correct Marks : 10/10

11. Ticket types


The Magic Castle, the home of the Academy of Magical Arts at California
has organized the great 'WonderWorks Magic Show'. Renowned magicians
were invited to mystify and thrill the crowd with their world’s spectacular
magic tricks. The Ticket booking for the show started 2 days prior and
there were different types of tickets offered with different fare. The show
organizers wanted to place a scanning machine at the entrance of the
venue for scrutiny. The machine will take the input of a character denoting
the various ticket types and displays the equivalent ticket type of the given
character.
There are 5 types of tickets, each of which is denoted by a character (both
upper case and lower case). Please find the equivalent strings for the
characters.
E or e - Early Bird Ticket
D or d - Discount Ticket
V or v - VIP Ticket
S or s - Standard Ticket
C or c - Children Ticket
Write a piece of code for the scanning machine that will take the input of a
character and print the equivalent string as given.

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;
}
}
}

Status : Correct Marks : 10/10

12. Write a program to check the greatest of three numbers.

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);
}
}

Status : Correct Marks : 10/10

13. Given an integer as an input, it represents the temperature in


centigrade. Determine the
Weather conditions based on the temperature.
Temperature < 0 then print "Freezing weather".Temperature 0 - 10 then
print "Very cold weather".Temperature 10 - 20 then print "Cold
weather".Temperature 20 - 30 then print "Normal in
temperature".Temperature 30 - 40 then print "Its hot".Temperature >= 40
then print "Its very hot".

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");
}
}

Status : Correct Marks : 10/10

14. Minimum Travel Time

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");
}
}

Status : Correct Marks : 10/10

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 &amp; 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);
}
}

Status : Correct Marks : 10/10


Sri Krishna College of Technology

Name :DHIYANESHWAR R Email :[email protected]


Roll no:727821TUCS043 Phone :9790266530
Branch :Sri Krishna College of Technology Department :CSE
Batch :2021-25 Degree :BE-CSE

2021_25 Batch_Fundamentals of Java_IRC

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;
}
}
}

Status : Correct Marks : 10/10

2. Problem Statement :

Trendy Numbers

Write a program to check whether the given number is a trendy number or


not. A number is said to be a trendy number if and only if it has 3 digits and
the middle digit is divisible by 3.

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)+" ");
}
}
}

Status : Correct Marks : 10/10

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;
}
}
}

Status : Correct Marks : 10/10

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 &amp; 8+1=9. and 297
is a Kaprekar number since 2972 = 88209 &amp; 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");
}
}

Status : Correct Marks : 10/10

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)+" ");
}
}

Status : Correct Marks : 10/10

7. Problem Statement :

Target Practice

Drona normally trains his disciples using a board that 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 a score of 100. Can
you write a program to determine the number of turns a disciple takes to
reach the target score of 'n'?

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");
}
}

Status : Correct Marks : 10/10

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;
}
}
}

Status : Correct Marks : 10/10

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.

For example, 19 is a special number.

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);
}
}
}

Status : Correct Marks : 10/10

10. Problem Statement:-

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

11. Problem Statement :

Print continuous number

Write a program to print all numbers between a and b (a and b inclusive)


using a while loop.

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++;
}
}
}

Status : Correct Marks : 10/10

12. Problem Statement:-

Write a program to generate the following series 0,2,8,14,...,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;
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+" ");
}

}
}
}

Status : Correct Marks : 10/10

13. Problem Statement:-

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);
}
}
}

Status : Correct Marks : 10/10

14. Problem Statement:-

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+" ");}
}
}
}

Status : Correct Marks : 10/10

15. Problem Statement :

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);
}
}

Status : Correct Marks : 10/10


Sri Krishna College of Technology

Name :DHIYANESHWAR R Email :[email protected]


Roll no:727821TUCS043 Phone :9790266530
Branch :Sri Krishna College of Technology Department :CSE
Batch :2021-25 Degree :BE-CSE

2021_25 Batch_Fundamentals of Java_IRC

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");

}
}
}

Status : Correct Marks : 10/10

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();
}
}
}

Status : Correct Marks : 10/10

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");
}
}
}

Status : Correct Marks : 10/10

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++;
}
}
}

Status : Correct Marks : 10/10

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();
}
}
}

Status : Correct Marks : 10/10

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();
}
}
}

Status : Correct Marks : 10/10

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--;
}
}
}

Status : Correct Marks : 10/10

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");
}
}
}

Status : Correct Marks : 10/10


10. Problem statement:
Write a java program to palindrome pyramid pattern 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,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--;
}
}
}

Status : Correct Marks : 10/10


1.A common problem in statistics is that of generating frequency
distribution of the given data.
Assuming that the data consists of n positive integers in the range 1 to
25,
write a program that prints the number of times each integer occurs in
the data.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n;
n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
boolean v[]=new boolean[n];
Arrays.fill(v,false);
for(int i=0;i<n;i++)
{
if(v[i]==true)
{
continue;
}
int count=1;
for(int j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
v[j]=true;
count++;
}
}
if(count!=0)
{
System.out.println(a[i]+" "+count);
}
}
}
}

2.Given an array of elements.


Find two elements in the array such that their sum is equal to the given
element K?

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n;
n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
int k;
k=s.nextInt();
int t=0;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(a[i]+a[j]==k)
{
t=1;
}
}
}
if(t==1)
{
System.out.print("Array has two elements with given
sum"+" "+k);
}
else
{
System.out.print("Array doesn't have two elements
with given sum "+k);
}

}
}

3.Given an array of numbers.


Give an algorithm for finding the first element in the array which is
repeated.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
int c=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
System.out.print("The first repeating element is "+a[i]);
c=1;
i=n;
j=n;
}

}
}
if(c==0)
{
System.out.print("There are no repeating elements");
}
}
}

4.Given an array A of n elements.


Find three elements m, n, and k in the array such that m2 + n2 = k2?

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int m[]=new int[n];
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
m[i]=a[i]*a[i];
}int c=0;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
for(int k=j;k<n;k++)
{
if(m[k]==m[i]+m[j])
{
System.out.println(m[k]+" "+m[i]+" "+m[j]);
System.out.println((float)a[k]+" "+(float)a[i]+"
"+(float)a[j]);
c=1;
}
else
c=c+0;
}
}
}
if(c==0) System.out.print("No such triplet exists");
}
}

5.LucarnosFilm Festival is an annual film festival and is also known for


being a prestigious platform for art house films.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n;
n=s.nextInt();
int l[]=new int[n];
int res[]=new int[n];
for(int i=0;i<n;i++)
{
l[i]=s.nextInt();
}
int r[]=new int[n];
for(int i=0;i<n;i++)
{
r[i]=s.nextInt();
}
for(int i=0;i<n;i++)
{
res[i]=l[i]*r[i];
}
int prod=0,ak=0;
for(int i=0;i<n;i++)
{
if(res[i]>prod)
{
prod=l[i]*r[i];
ak=i;
}
if(res[i]==prod)
{
if(r[i]>r[ak])
{
ak=i;
}
if(r[i]==ak)
{
if(ak>i)
{
ak=i;
}
}
}
}
System.out.print(ak+1);
}
}

6.Youngest and Oldest

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.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n;
n=s.nextInt();
if(n<0) System.out.print("Invalid Input");
int a[]=new int[n];
for(int k=0;k<n;k++)
{
a[k]=s.nextInt();
if(a[k]<0) System.out.print("Invalid Input");
}

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.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[50];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
int k=0,j=0,c=0;
for(int i=0;i<n;i++)
{
if(a[i]==1) k++;
else if(a[i]==2) j++;
else c++;
}
if(k>j)
{
if(k>c)
{
System.out.print(n-k);
}
else
{
System.out.print(n-c);
}
}
else
{
if(j>c)
{
System.out.print(n-j);
}
else
{
System.out.print(n-c);
}
}
}
}

8.Version Management System

A version Managementsystem (VMS) is a repository of files,


often the files for the source code of computer programs, with monitored
access.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int m,n,k;
n=s.nextInt();
m=s.nextInt();
k=s.nextInt();
int a[]=new int[m];
for(int i=0;i<m;i++)
{
a[i]=s.nextInt();
}
int b[]=new int[k];
for(int i=0;i<k;i++)
{
b[i]=s.nextInt();
}
int c=0,cc=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<k;j++)
{
if(a[i]==b[j]) {c++;}
}
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
{
if(a[j]==i) {break;}
if(j==m-1)
{
for(int l=0;l<k;l++)
{
if(b[l]==i) break;
if(l==k-1) cc++;
}
}
}
}
System.out.print(c+" "+cc);
}
}

9.Find largest and smallest number in an array.

// You are using Java


import java.util.Scanner;
class Large_Small{
public static void main (String args[])
{
Scanner s=new Scanner(System.in);
int min,max;
//get input from user for array length
//declaring an array of n elements
//for loop takes input from user
//Logic to find minimum and maximum
int n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
min=a[0];
max=a[0];
for(int i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
if(min>a[i])
{
min=a[i];
}
}
System.out.print("smallest value: "+min);
System.out.print("\nlargest value: "+max);
}
}

10.Given an unsorted array of unique integers in the range from 1 to N+1.


Find the missing element in the array without sorting the array.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
int k;
for(k=1;k<=n+1;k++)
{
int c=0;
for(int i=0;i<n;i++)
{
if(k!=a[i])
{
c++;
}
}
if(c==n) System.out.print(k);
}
}
}

11. Weighing machines in Sunrise Logistics is not working.


Raju, the manager of the division wants to calculate the total weight of
received goods.
Weight is printed in the goods label.
Write a suitable code to help Raju.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n;
n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
System.out.print(sum);
}
}

12.Write a program to insert an element at a specified position in the


array and
find the duplicate values of the array of float values.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n,pos,in;
n=s.nextInt();
int a[]=new int[n+1];
for(int i=1;i<=n;i++)
{
a[i]=s.nextInt();
}
pos=s.nextInt();
in=s.nextInt();
for(int j=1;j<pos;j++)
{
System.out.print((float)a[j]+" ");
}
System.out.print((float)in+" ");
for(int j=pos;j<=n;j++)
{
System.out.print((float)a[j]+" ");
}
System.out.println();
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(a[i]==a[j])
{
System.out.println((float)a[i]);
}
}
}
}
}

13.Write a program to find the first and last occurrence of an element in


a sorted array.

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;
}
}
}
}

14.Write a program to find all pairs of elements in an array whose sum is


equal to the given value.
Help Guru to write a program to complete this task.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int k,n;
k=s.nextInt();
n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(a[i]+a[j]==k)
{
System.out.println(a[i]+" + "+a[j]+" = "+k);
}
}
}
}
}

15.Given an array A consists of N number of elements.


If the sum of the element is "even" print the sum of the element.
If the sum of the element is "odd" print the product of the element.

// You are using Java


import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n,res=0;
n=s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
if(sum%2==1)
{
res=1;
for(int i=0;i<n;i++)
{
res=res*a[i];
}
}
else
{
res=0;
for(int i=0;i<n;i++)
{
res=sum;
}
}
System.out.print(res);
}
}
Sri Krishna College of Technology

Name :DYANESH S Email :[email protected]


Roll no:727821TUEE024 Phone :9003488474
Branch :Sri Krishna College of Technology Department :EEE
Batch :2021-25 Degree :BE-EEE

2021_25 Batch_Fundamentals of Java_IRC

IRC_JAVA_COD_ARRAYS2D

Attempt : 1
Total Mark : 150
Marks Obtained : 150

Section 1 : Coding

1. Write a program to find the normal of a matrix.

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);

}
}

Status : Correct Marks : 10/10

2. Write a program to implement matrix multiplication.

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();
}
}
}

Status : Correct Marks : 10/10

3. Write a program to subtract two matrices.

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();
}
}
}

Status : Correct Marks : 10/10

4. Write a program to add two matrices.

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();
}
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

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();
}
}
}

Status : Correct Marks : 10/10

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]+" ");
}
}
}
}
}

Status : Correct Marks : 10/10

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);
}
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

11. Collisions of Events


Lucarnos Film Festival is an annual film festival and is also known for
being a prestigious platform for art house films. This year at the Lucarnos
Film festival there are many movies to be screened, each of different genre
ranging from drama movies to comedy ones and teen movies to horror
ones. The festival is a long-running event this time as the organizers are
planning to screen only one movie per day. The organizers have populated
their schedule in the form of a matrix where 'i' is the movie number and 'j' is
the day number. Eij is the movie preference dates.
You are given a matrix E of N rows and M columns where Eij is 1 if the i-th
movie is to be screened on j-th day, otherwise it will be 0. Note that it is not
necessary that if a movie x will be screened on day y, then day y should
screen only movie x.
You know that if there are two different movies x and y, which are to be
screened on the same day z, and then there will be a collision. Can you
calculate the number of different collisions at this movie festival? Note
that order of movies in the collision doesn't matter.

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);

}
}

Status : Correct Marks : 10/10

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();
}
}
}

Status : Correct Marks : 10/10

13. Valid Initial Configuration


Nurikabe logical game (sometimes called Islands in the Stream) is a binary
determination puzzle. The puzzle is played on a typically rectangular grid
of cells, some of which contain numbers. You must decide for each cell if it
is white or black (by clicking on them) according to the following rules:
· All of the black cells must be connected.
· Each numbered cell must be part of a white island of connected white
cells.
· Each island must have the same number of white cells as the number it
contains (including the numbered cell).
· Two islands may not be connected.
· There cannot be any 2x2 blocks of black cells.
Unnumbered cells start out grey and cycle through white and black when
clicked. Initially numbered cells are white in color.
Problem Statement:
Write a program to check whether the given board configuration is a valid
initial configuration. Below figure is the sample valid initial configuration.

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");
}
}

Status : Correct Marks : 10/10

14. Mid Aged


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. The senior flight purser Neerja Banhot withered her
fear and helped evacuating the passengers on board.

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);
}
}

Status : Correct Marks : 10/10

15. Seetha, a maths teacher explained about Matrix addition, subtraction


and multiplication in her class. She assigned different task to her students.

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();
}
}
}

Status : Correct Marks : 10/10


Sri Krishna College of Technology

Name :DYANESH S Email :[email protected]


Roll no:727821TUEE024 Phone :9003488474
Branch :Sri Krishna College of Technology Department :EEE
Batch :2021-25 Degree :BE-EEE

2021_25 Batch_Fundamentals of Java_IRC

IRC_JAVA_COD_Strings

Attempt : 1
Total Mark : 100
Marks Obtained : 100

Section 1 : Coding

1. Write a program to count the vowels in the string.

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));
}

}
}

Status : Correct Marks : 10/10

3. Write a program to check whether the given string is a palindrome.

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");
}
}
}

Status : Correct Marks : 10/10

4. Write a program to remove all the spaces in the given sentence.

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);
}
}

Status : Correct Marks : 10/10

5. Write a program to check whether the string is lexicographically equal


to another string

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));
}
}

Status : Correct Marks : 10/10

6. Write a program to remove consecutive vowels from a string.

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));

}
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

9. Using contains() and trim() method


Having finished most of our application for the fair, it's time to focus on
minor details that went wrong during a test run of our application in this
module. Accidentally some gibberish text with leading and trailing got
copied to the clipboard and got pasted in the some of your text
documents. Don't worry, still, we have the gibberish text with us, you can
manually load each document, and find the text and delete it. Think it will
take ages, no we can think of a time saver. Using your programming skills,
load each document in a program and find in which files the text got
copied. Assume text of the document is given as the input to the program.
write a program to find whether the gibberish text is present in the string.

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();
}
}

Status : Correct Marks : 10/10

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;
}
}
}
}

Status : Correct Marks : 10/10


Sri Krishna College of Technology

Name :DYANESH S Email :[email protected]


Roll no:727821TUEE024 Phone :9003488474
Branch :Sri Krishna College of Technology Department :EEE
Batch :2021-25 Degree :BE-EEE

2021_25 Batch_Fundamentals of Java_IRC

IRC_JAVA_COD_Strings_Set2

Attempt : 1
Total Mark : 100
Marks Obtained : 100

Section 1 : Coding

1. Using contains() and trim() method


Having finished most of our application for the fair, it's time to focus on
minor details that went wrong during a test run of our application in this
module. Accidentally some gibberish text with leading and trailing got
copied to the clipboard and got pasted in the some of your text
documents. Don't worry, still, we have the gibberish text with us, you can
manually load each document, and find the text and delete it. Think it will
take ages, no we can think of a time saver. Using your programming skills,
load each document in a program and find in which files the text got
copied. Assume text of the document is given as the input to the program.
write a program to find whether the gibberish text is present in the string.

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");

}
}
}

Status : Correct Marks : 10/10

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");
}

Status : Correct Marks : 10/10


3. Mobile number validation
Let's implement the logic for mobile number validation using StringBuilder
and embed it in our program. Mobile number should precede with "+91",
followed by 10 digits. The indexOf() method returns index of given
character value or substring. If it is not found, it returns -1. Write a program
to validate the mobile number given as input. Use indexOf() to check
whether "+91" is present or not.
Create a driver class called Main. In the Main method, obtain the inputs
from the console, validate the mobile number and prompt the user as given
in sample I/O.

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");
}
}
}

Status : Correct Marks : 10/10

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));
}
}
}

Status : Correct Marks : 10/10

5. Balls for Challenge


The Circoloco Children Carnival is the City’s largest and successful event
dedicated to children and families. The main focus at the carnival is the
workshop arena where kids can participate in engaging and educational
activities.
Charlie, a little boy accompanied by his Mom visited the fair, where he
participated at the "Balls for Challenge" activity. He was given many balls
of white and black colors. During the play, he arranged the balls into two
rows both consisting of N number of balls. These two rows of balls are
given to you in the form of strings X, Y. Both these string consist of 'W' and
'B', where 'W' denotes a white colored ball and 'B' a black colored.
Other than these two rows of balls, Charlie has an infinite supply of extra
balls of each color. He wants to create another row of N balls, Z in such a
way that the sum of hamming distance between X and Z, and hamming
distance between Y and Z is maximized.
Hamming Distance between two strings X and Y is defined as the number
of positions where the color of balls in row X differs from the row Y ball at
that position. e.g. hamming distance between "WBB", "BWB" is 2, as at
position 1 and 2, corresponding colors in the two strings differ. As there
can be multiple such arrangements of row Z, Charlie wants you to find the
lexicographically smallest arrangement which will maximize the above
value.

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));
}
}
}
}

Status : Correct Marks : 10/10

6. Casper at the Carnival


The Circoloco Children Carnival is the City’s largest and successful event
dedicated to children and families. Casper is a smart little boy who loves
eating cookies and drinking fresh juices. He visits the carnival with his
parents and is going to spend N minutes at the event ground. Each minute
he either eats a cookie or drinks fresh juice. Cookies are very sweet and
thus Casper’s parents have instructed him to drink fresh juice in the next
minute, after eating a cookie.
You are given whether he ate a cookie or drank fresh juice in each of the N
minutes. Your task is to check if Casper followed his parents' instructions.
That is, you need to verify whether after each eaten cookie he drinks fresh
juice in the next minute.

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");
}
}

Status : Correct Marks : 10/10

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));
}
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

9. Consider a fleet of soldiers of a country are being assembled for a


rehearsal session, the enemy country secretly surrounded them and has a
special strategy to kill the soldiers in a particular pattern.
Assume that the soldiers are standing in a single straight line. The
enemies will repeatedly scan through this line and kill soldiers who are all
matching the given pattern.

Find the list of soldiers who are surviving at last or find if all of them are
killed.

Here soldiers are represented as alpha-numeric letters, irrespective of


cases.

Implement "FindRemainingSoldiers" class with "defeatSoldiers(String


soldiers, String pattern)" method to find the left out soldiers if any, else
print "Defeat" as result.

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 ;
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);

String soldiers = in.next();


String pattern = in.next();

in.close();

String result = defeatSoldiers(soldiers, pattern);

if (result.length() == 0) {
System.out.println("Defeat");
} else {
System.out.println(result);
}

Status : Correct Marks : 10/10

10. Write a program to convert a String to an int

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);
}
}

Status : Correct Marks : 10/10


Sri Krishna College of Technology

Name :Vinothini R Email :[email protected]


Roll no:727821TUAD059 Phone :7708211201
Branch :Sri Krishna College of Technology Department :AI&DS
Batch :2021-25 Degree :B.E-AI&DS

2021_25 Batch_Fundamentals of Java_IRC

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");
}

}
}

Status : Correct Marks : 10/10

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);
}
}
}

Status : Correct Marks : 10/10

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");
}
}

Status : Correct Marks : 10/10


4. Problem statement:
Write a Java program to check for a number at the end of a given string.

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!");
}
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

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");
}
}
}
}

Status : Correct Marks : 10/10

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");
}
}
}

Status : Correct Marks : 10/10

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");
}
}
}

Status : Correct Marks : 10/10

10. Problem statement:


Write a Java Program to Extract a Single Quote Enclosed String From a
Larger String using Regex.

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));
}
}
}

Status : Correct Marks : 10/10


Sri Krishna College of Technology

Name :Vinothini R Email :[email protected]


Roll no:727821TUAD059 Phone :7708211201
Branch :Sri Krishna College of Technology Department :AI&DS
Batch :2021-25 Degree :B.E-AI&DS

2021_25 Batch_Fundamentals of Java_IRC

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));
}
}

Status : Correct Marks : 10/10

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());
}
}

Status : Correct Marks : 10/10

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);
}
}

Status : Correct Marks : 10/10

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();

String arr[] = {"MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATUR


DAY","SUNDAY"};
System.out.println(arr[a-1]);
if(a+b > 7){
int ans=1;
switch(a+b){
case 8:
ans=1;
break;
case 9:
ans=2;
break;
case 10:
ans=3;
break;
case 11:
ans=4;
break;
case 12:
ans=5;
break;
case 13:
ans=6;
}
System.out.println(ans);
System.out.print(arr[ans-1]);
}
else{
System.out.println(a+b);
System.out.print(arr[a+b-1]);
}

}}

Status : Correct Marks : 10/10

You might also like