Professional Documents
Culture Documents
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;
float height,weight;
height = sc.nextFloat();
weight = sc.nextFloat();
}
2) Write a program to find the simple interest
package class1_q2;
import java.util.Scanner;
float P,t,r;
P = sc.nextFloat();
r = sc.nextFloat();
t = sc.nextFloat();
float SI = P*r*t/100;
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;
if(num%2==0)
System.out.println("It's an even number");
else
System.out.println("It's an odd number");
}
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;
b) * *
** **
*****
** **
* *
package class2_q2;
import java.util.Scanner;
int n;
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;
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(units>0){
amount+=(units*2.4);
}
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(" ");
}
}
}
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];
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]+" ");
}
}
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();
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();
}
4) To find the occurrence of “VIT”;
package class3_q4;
import java.util.Scanner;
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;
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();
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;
}
}
}