You are on page 1of 41

JAVA PROGRAMMING

CSE1007 (L31+L32)

ADVAITHA B
19BBS0049
Class-1
1) Find BMI of a person by getting weight and height in kg and cm respectively from user.
[Formula BMI = kg/m2]

package class1_q1;

import java.util.Scanner;

public class Class1_Q1 {

public static void main(String[] args) {

float height,weight;

Scanner sc = new Scanner(System.in);

System.out.println("Advaitha B (19BBS0049) ");

System.out.println("Enter your height in cm: ");

height = sc.nextFloat();

System.out.println("Enter your weight in Kilogram: ");

weight = sc.nextFloat();

float BMI = weight/((height/100)*(height/100));

System.out.println("BMI: " + BMI);

}
2) Write a program to find the simple interest

package class1_q2;

import java.util.Scanner;

public class Class1_Q2 {

public static void main(String[] args) {

float P,t,r;

Scanner sc = new Scanner(System.in);

System.out.println("Advaitha B (19BBS0049) ");

System.out.println("Enter the Princpal,interest,time duration: ");

P = sc.nextFloat();
r = sc.nextFloat();

t = sc.nextFloat();

float SI = P*r*t/100;

System.out.println("Simple Interest: " + SI);

3) Write a program to display you name , register number and your cgpa
import java.util.Scanner;
public class Class1_Q3 {
public static void main(String[] args) {
String name,reg_no;
float cgpa;

Scanner sc = new Scanner(System.in);


System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter your name, registration number and CGPA: ");
name = sc.nextLine();
reg_no = sc.nextLine();
cgpa = sc.nextFloat();
System.out.println("Name: "+ name);
System.out.println("Registration Number: "+ reg_no);
System.out.println("CGPA: "+ cgpa);
}

4) Write a program to find the area of circle


public class Class1_Q4 {
public static void main(String[] args) {
float radius;

Scanner sc = new Scanner(System.in);


System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter Radius: ");
radius = sc.nextFloat();
double area = 3.14*radius*radius;
System.out.println("Area: " + area);

5) a) Check if a given number is odd or even number [using if statement]


import java.util.Scanner;

public class Class1_Q5a {


public static void main(String[] args) {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter the number: ");
num = sc.nextInt();

if(num%2==0)
System.out.println("It's an even number");
else
System.out.println("It's an odd number");
}

b) Check if a given number is odd or even number [using switch case]

package class1_q5b;
import java.util.Scanner;
public class Class1_Q5b {
public static void main(String[] args) {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter the number: ");
num = sc.nextInt();
switch(num%2){
case 0:{
System.out.println("It's an even number");
break;
}
case 1:{
System.out.println("It's an odd number");
break;
}
}
}

}
Class 2
1) Print the following patterns by finding the table values of stars and spaces.
a. * ** ***
package class2_q1;
import java.util.Scanner;

public class Class2_Q1a {

public static void main(String[] args) {


int n;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("N: ");
n = sc.nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println(" ");
}
}

b) * *

** **

*****

** **

* *

package class2_q2;
import java.util.Scanner;

public class Class2_Q1b {

public static void main(String[] args) {

int n;

Scanner sc = new Scanner(System.in);

System.out.println("Advaitha B (19BBS0049) ");

System.out.println("N: ");

n = sc.nextInt();

for(int i=1;i<=n/2;i++){

for(int k=1;k<=i;k++)

System.out.print("*");

for(int j=1;j<=(n-2*i);j++)

System.out.print(" ");

for(int k=1;k<=i;k++)

System.out.print("*");

System.out.println(" ");

for(int j=1;j<=n;j++)

System.out.print("*");

System.out.println("");

for(int i=n/2;i>0;i--){

for(int k=i;k>0;k--)

System.out.print("*");

for(int j=1;j<=(n-2*i);j++)

System.out.print(" ");

for(int k=i;k>0;k--)

System.out.print("*");
System.out.println(" ");

}
c) 1 12 123 1234 123 12 1

package class2_q1c;

import java.util.Scanner;

public class Class2_Q1c {

public static void main(String[] args) {


int n;

Scanner scanner = new Scanner(System.in);

System.out.println("Advaitha B (19BBS0049) ");

System.out.print("N: ");

n = scanner.nextInt();

for(int i=1;i<=n;i++){

for(int j=1;j<=i;j++){

System.out.print(j);

System.out.println("");

for(int i=n;i>0;i--){

for(int j=1;j<=i;j++){

System.out.print(j);

System.out.println("");

}
2) Find the HCF and LCM of given numbers.
package class2_q2ans;
import java.util.Scanner;
public class Class2_Q2ans {
public static void main(String[] args) {
int t1, t2, num1, num2, temp, hcf, lcm;
Scanner scanner = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.print("Number1: ");
num1 = scanner.nextInt();
System.out.print("Number2: ");
num2 = scanner.nextInt();
scanner.close();

t1 = num1;
t2 = num2;

while(t2 != 0){
temp = t2;
t2 = t1%t2;
t1 = temp;
}

hcf = t1;
lcm = (num1*num2)/hcf;

System.out.println("HCF: "+hcf);
System.out.println("LCM: "+lcm);
}
}

3) Ask the user to enter the marks of a student in the below order. Maths, M Physics,
P Chemistry, C English, E Computer Science, CS And calculate the metrics according
to the below table. Metric Formula Overall Average (OA) Sum of all the marks /
total number of subjects Engineering Average (EA) Sum of (M * 2), P and C / 4
Computer Science Average (CSA) CS Print the output according to the below table
Case Output If OA > 75 and EA > CSA Probable Mech, Civil, EEE, ECE candidate If OA
> 75 and CSA > EA Probable CSE, IT, IS candidate If OA < 75 and CSA > EA Probable
BCA candidate If OA < 75 and CSA > EA Probable BSc candidate
package class2_q3;
import java.util.Scanner;
public class Class2_Q3 {
public static void main(String[] args) {
int M,P,C,E,CS;
float OA,EA,CSA;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter the scores of
Math,Physics,Chemistry,English,Computer: ");
M = sc.nextInt();
P = sc.nextInt();
C = sc.nextInt();
E = sc.nextInt();
CS = sc.nextInt();

OA = (M+P+C+E+CS)/5;
EA = (M*2)+P+C/4;
CSA = CS;

if(OA >75 && EA>CSA){


System.out.println("Probable Mech,Civil,EEE,ECE Candidate");
}
else if(OA >75 && CSA>EA)
System.out.println("Probable CSE,IT,IS Candidate");
else if(OA<75&& CSA>EA)
System.out.println("Probable BCA Candidate");
else if(OA<75 && CSA<EA)
System.out.println("Probable BSc Candidate");
}
}
4) Write a program to calculate the monthly Electricity bills as per the following tariff:
Minimum Rs. 200 for up to 100 units Plus Rs. 0.60 per next 50 units consumed Plus
Rs. 1.20 per next 50 units consumed Plus Rs. 2.40 per beyond 200 units consumed.
package class2_q4;
import java.util.Scanner;
public class Class2_Q4 {
public static void main(String[] args) {
int units;
float amount;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Unit Consumption: ");
units = sc.nextInt();
amount = 100;
units = units-100;

if(units > 0){


if(units>=50){
amount+= 50*0.6;
units = units-50;
}
else{
amount+= (units*0.6);
units = 0;
}
}
if(units > 0){
if(units>=50){
amount+= 50*1.20;
units = units - 50;
}
else{
amount+= (units*1.2);
units = 0;
}
}

if(units>0){
amount+=(units*2.4);
}

System.out.println("Cost: "+ amount);


}
}
5) Develop a Java to that calculates the Addition, Subtraction, Multiplication and
Division using menu driven approach. (Do...while loop)
package class2_q5;
import java.util.Scanner;
public class Class2_Q5 {
public static void main(String[] args) {
int n1,n2,choice;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("N1: ");
n1 = sc.nextInt();
System.out.println("N2: ");
n2 = sc.nextInt();
System.out.println("Enter 1 for multiplication, 2 for Division, 3 for Addition, 4
for Subraction: ");
choice = sc.nextInt();

switch(choice){
case 1:{
System.out.println(n1*n2);
break;
}
case 2:{
System.out.println(n1/n2);
break;
}
case 3:{
System.out.println(n1+n2);
break;
}
case 4:{
System.out.println(n1-n2);
break;
}
default:
System.out.println("Enter valid choice");
}
}
}

6) Write a Java program to get the marks of ‘n students. Write a method to display
the elements of array and display them in the reverse order
package class2_q6;
import java.util.Scanner;
public class Class2_Q6 {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter number of students: ");
n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the marks: ");
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
for(int i=n-1;i>=0;i--){
System.out.print(arr[i]);
System.out.print(" ");
}
}
}

7) Sort an array of element using bubble sort

package class2_q7;
import java.util.Scanner;
public class Class2_Q7 {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("N: ");
n = sc.nextInt();
int [] arr = new int[n];

System.out.println("Enter the numbers for sorting: ");


for(int i=0;i<n;i++)
arr[i] = sc.nextInt();

for(int i=0;i<n-1;i++){
for(int j=0;j<n-1;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int i=0;i<n;i++)
System.out.print(arr[i] + " ");
}
}
8) Remove duplicate elements from a sorted array [only one array should be used]
package class2_q8;
import java.util.Scanner;
public class Class2_Q8 {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("N: ");
n = sc.nextInt();
int [] arr = new int[n];
System.out.println("Enter sorted array: ");
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
int j=1;
for(int i=1;i<n-1;i++){
if(arr[i]!=arr[i+1]){
arr[j]=arr[i+1];
j++;
}
}
if( arr[j-1]!=arr[n-1]){
arr[j] = arr[n-1];
j++;
}
for(int i=0;i<j;i++)
System.out.print(arr[i]+" ");
}
}

9) Check if a given input matrix from a user is an identity matrix


package class2_q9;
import java.util.Scanner;
public class Class2_Q9 {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("N: ");
n = sc.nextInt();
int [][] arr = new int[n][n];
System.out.println("Enter the data: ");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
arr[i][j] = sc.nextInt();
}
boolean ans = true;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(j==i && arr[i][j]!=1 )
ans = false;
else if(i!=j && arr[i][j]!=0)
ans=false;
}
if(ans==false)
break;
}

if(ans==true)
System.out.println("It is an Identity Matrix ");
else
System.out.println("It is not an Identity Matrix ");
}

}
10) Display the addition result of two matrices
package class2_q10;
import java.util.Scanner;
public class Class2_Q10 {
public static void main(String[] args) {
int n1,n2;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter n1 and n2: ");
n1 = sc.nextInt();
n2 = sc.nextInt();
int [][] arr1 = new int[n1][n2];
int [][] arr2 = new int[n1][n2];
System.out.println("Enter the data of first matrix: ");
for(int i=0;i<n1;i++){
for(int j=0;j<n1;j++)
arr1[i][j] = sc.nextInt();
}
System.out.println("Enter the data of second matrix: ");
for(int i=0;i<n1;i++){
for(int j=0;j<n1;j++)
arr2[i][j] = sc.nextInt();
}

for(int i=0;i<n1;i++){
for(int j=0;j<n1;j++)
arr1[i][j] = arr1[i][j] + arr2[i][j];
}
System.out.println("Sum of matrix: ");
for(int i=0;i<n1;i++){
for(int j=0;j<n1;j++)
System.out.print(arr1[i][j]+" ");
System.out.println(" ");
}
}
}
Class-3
1) Write a method that receives a jagged array containing marks scored by a student in
various semesters. Write a code to display the number of subjects the student has passed.
package class3_q1;
import java.util.Scanner;
public class Class3_Q1 {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter the number of rows: ");
n = sc.nextInt();

int arr[][] = new int[n][];

for(int i=0;i<n;i++){
int temp;
System.out.println("Enter number of columns of row " + (i+1));
temp = sc.nextInt();
arr[i] = new int[temp];
for(int j=0;j<temp;j++){
System.out.println("Enter the data of row " + (i+1) + " ans the data"
+ " of coulumn "+(j+1));
arr[i][j] = sc.nextInt();
}
}
System.out.println("Array: ");
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
if(arr[i][j] >=40)
System.out.print(arr[i][j] + " Passed ");
else
System.out.print(arr[i][j] + " Failed ");
}
System.out.println(" ");
}
}

}
2) Registration and Mobile number validity
package class3_q2;
import java.util.Scanner;
public class Class3_Q2 {
public static void main(String[] args) {
String mobile,reg_no;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter your mobile number: ");
mobile = sc.nextLine();
System.out.println("Enter your Registration number: ");
reg_no = sc.nextLine();
boolean ans1=true,ans2 = false;
for(int i=0;i<mobile.length();i++){
if(mobile.length()!=10 || !Character.isDigit(mobile.charAt(i))){
ans1=false;
break;
}
}
for(int i=0;i<reg_no.length();i++){
if(reg_no.length()!=9){
ans2=false;
break;
}
else if((Character.isDigit(reg_no.charAt(i))) ||
(Character.isAlphabetic(reg_no.charAt(i))))
ans2 = true;
else{
ans2=false;
break;
}
}

if(ans1)
System.out.println("Valid mobile number");
else
System.out.println("Invalid mobile number");
if(ans2)
System.out.println("Valid registration number");
else
System.out.println("Invalid registration number");

}
3) To find if a name is present
package class3_q3;
import java.util.Scanner;
public class Class3_Q3 {
public static void main(String[] args) {
String[] names = new String[5];
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter the names: ");
for(int i=0;i<5;i++){
names[i] = sc.nextLine();
}
System.out.println("Enter the name you want to find: ");
String temp = sc.nextLine();

boolean ans = false;


for(int i=0;i<5;i++){
int t = names[i].indexOf(temp);
if(t!=-1){
ans = true;
break;
}
}
if(ans)
System.out.println("Name found");
else
System.out.println("Name not found");
}

}
4) To find the occurrence of “VIT”;
package class3_q4;

import java.util.Scanner;

public class Class3_Q4 {


public static void main(String[] args) {
String s;
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter the sentence: ");
s = sc.nextLine();
int count=0;

while(s.contains("VIT")){
s=s.substring(s.indexOf("VIT")+3, s.length()-1);
count++;
}
if(count>0){
System.out.println("VIT occures "+count +" times.");
}else{
System.out.println("VIT does not exists.");
}

}
5) Number of scope and sense students
package class3_q5;

import java.util.Scanner;

public class Class3_Q5 {


public static void main(String[] args) {
String[] arr = new String[5];
Scanner sc = new Scanner(System.in);
System.out.println("Advaitha B (19BBS0049) ");
System.out.println("Enter the registration numbers: ");
for(int i=0;i<5;i++)
arr[i] = sc.next();

int scope=0,sense=0;
for(int i=0;i<5;i++){
if(arr[i].contains("BCE"))
scope++;
else if(arr[i].contains("BEC"))
sense++;
}
if(sense==0 && scope==0)
System.out.println("There are no students from Scope and Sense");
else{
System.out.println("Number of scope students "+ scope);
System.out.println("Number of sense students "+ sense);
}
}

}
6) String to Hexadecimal to Binary
package class3_q6;
import java.util.Scanner;
public class Class3_Q6 {
public static void main(String[] args) {
System.out.println("Advaitha B (19BBS0049) ");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = sc.next();
StringBuffer sb = new StringBuffer();

char ch[] = str.toCharArray();


for(int i = 0; i < ch.length; i++) {
String hexString = Integer.toHexString(ch[i]);
sb.append(hexString);
}
String result = sb.toString();
result = result.toUpperCase();
String binary = "";

for(int i=0;i<result.length();i++){
switch(result.charAt(i)){
case 'A':{
binary+="1010";
break;
}
case 'B':{
binary+="1011";
break;
}
case 'C':{
binary+="1100";
break;
}
case 'D':{
binary+="1101";
break;
}
case 'E':{
binary+="1110";
break;
}
case 'F':{
binary+="1111";
break;
}
case '0':{
binary+="0000";
break;
}
case '1':{
binary+="0001";
break;
}
case '2':{
binary+="0010";
break;
}
case '3':{
binary+="0011";
break;
}
case '4':{
binary+="0100";
break;
}
case '5':{
binary+="0101";
break;
}
case '6':{
binary+="0110";
break;
}
case '7':{
binary+="0111";
break;
}
case '8':{
binary+="1000";
break;
}
case '9':{
binary+="1001";
break;
}

}
}

System.out.println("Binary Code: "+ binary);


}

You might also like