Assignments Day 1: Public Class
Assignments Day 1: Public Class
Assignments Day 1: Public Class
Code:
System.out.println("Hello World");
Output:
Code:
import java.util.Scanner;
int a,b;
System.out.println("---Addition of Numbers---");
a=sc.nextInt();
b=sc.nextInt();
Output:
Code:
import java.util.Scanner;
int a,b,c;
System.out.println("---------Swap of Numbers---------");
a=sc.nextInt();
b=sc.nextInt();
c=a;
a=b;
b=c;
Output:
Code:
import java.util.Scanner;
double total,per;
System.out.println("----------Student Details----------");
String name=sc.next();
int rollno=sc.nextInt();
String address=sc.next();
double marks1=sc.nextDouble();
double marks2=sc.nextDouble();
double marks3=sc.nextDouble();
total=marks1+marks2+marks3;
per=total/3;
}
Output:
Assignments Day 2
Q 1. Write a program to find factorial of a given number
Code:
import java.util.Scanner;
int a,i,fact=1;
a=sc.nextInt();
for(i=1;i<=a;i++)
{
fact=fact*i;
Ouput:
int m,n;
int p=1;
m=sc.nextInt();
n=sc.nextInt();
for(int i=1;i<=n;i++)
p *= m;
}
}
Output:
int a=sc.nextInt();
for(int i=2;i<a/2;++i)
if(a%i==0)
flag=true;
break;
if(!flag)
else
}
}
Output:
Q 4. Num of series :
1+2+3+….+n
Code:
import java.util.Scanner;
int n,sum=0;
n=sc.nextInt();
for(int i=1;i<=n;i++)
sum=sum+i;
Output:
Q 5. Check whether the number is palindrome or not?
Code:
import java.util.Scanner;
int x,n,temp,sum=0;
n=sc.nextInt();
temp=n;
x=n%10;
sum=(sum*10)+x;
n=n/10;
System.out.println("Number is Palindrome.");
else
{
System.out.println("Number is not Palindrome.");
Output:
Q 6. Write a program to find sum of all even and odd numbers between 1 to n.
Code:
import java.util.Scanner;
int num,oSum=0,eSum=0;
num=sc.nextInt();
for(int i=1;i<=num;i++)
if(i%2==0) {
eSum=eSum+i;
else {
oSum=oSum+i;
}
System.out.println("Sum of all Even Numbers are : "+eSum);
Output:
int rem,n,rev=0;
n=sc.nextInt();
while(n!=0)
rem=n%10;
rev=rev*10+rem;
n=n/10;
Output:
Q 8. Write a program to print all Prime numbers between 1 to n
Code:
import java.util.Scanner;
int i,n,flag;
n=sc.nextInt();
for(int j=2;j<=n;j++)
flag=0;
for(i=2;i<j;i++)
if(j%i==0)
flag=1;
break;
if (flag==0)
System.out.println(+j);
}
Output:
int rem,n,num,result=0;
n=sc.nextInt();
num=n;
while(n>0)
{
rem=n%10;
result=result+(rem*rem*rem);
n=n/10;
if(result==num)
else
Output:
Q 10. Write a program to find greatest of three numbers using nested if-else
Code:
import java.util.Scanner;
double i,j,k;
i=sc.nextDouble();
j=sc.nextDouble();
k=sc.nextDouble();
if(i>=j)
if(i>=k)
else
else
if(j>=k)
else
Output:
Q 11. Create menu driven program for Pizza Shop.And display total amount
Code:
import java.util.Scanner;
System.out.println("-----Pizza Shop-----");
System.out.println("-----Main Menu----");
System.out.println(" 5: Bill");
System.out.println(" 6 :exit");
boolean flag=false,count=false;
int qty,total=0;
while(flag!=true)
{
System.out.println("Please Enter your Choice : ");
int ch=sc.nextInt();
switch(ch)
case 1:
qty=sc.nextInt();
total=total+(qty*200);
break;
case 2:
qty=sc.nextInt();
total=total+(qty*250);
break;
case 3:
qty=sc.nextInt();
total=total+(qty*150);
break;
case 4:
int top=sc.nextInt();
while(count!=true)
switch(top)
{
case 1:
total=total+20;
break;
case 2:
total=total+25;
break;
case 3:
total=total+30;
break;
case 4:
total=total+50;
break;
case 5:
count=true;
break;
default:
System.out.println("Invalid Choice");
break;
break;
case 5:
case 6:flag=true;
break;
default:
System.out.println("Invalid Choice");
break;
Output:
Q 12. Create Menu driven program for array operations
1:Read Array
2:Print Array
3:Search element in array
4:Reverse Array
5:Even number from array
Code:
import java.util.Scanner;
int size,ch,ele,temp;
int []arr=null;
boolean flag=true,flag1=true;
System.out.println("----------------MENU-------------------");
while(flag==true) {
ch=sc.nextInt();
switch(ch) {
case 0:
flag=false;
//System.exit(0);
break;
case 1:
size=sc.nextInt();
arr=new int[size];
for(int i=0;i<arr.length;i++) {
arr[i]=sc.nextInt();
break;
case 2:
System.out.println("ARRAY : ");
for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
//for(int n:arr)
// System.out.println(n);
break;
case 3:
ele=sc.nextInt();
for(int i=0;i<arr.length;i++) {
if(arr[i]==ele) {
flag1=false;
break;
if(flag1==false){
else {
break;
case 4:
for(int i=0,j=arr.length-1;(i<=arr.length/2)&&(j>=arr.length/2);i+
+,j--) {
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
break;
case 5:
for(int i=0;i<arr.length;i++) {
if(arr[i]%2==0)
System.out.print(" "+arr[i]);
Output:
Assignments Day 3
Q 1. Java program to print the following pattern on the console
**
***
****
*****
Code:
import java.util.Scanner;
int row;
row=sc.nextInt();
System.out.print("* ");
System.out.println();
}
Output:
per<59 :C
Code:
import java.util.Scanner;
int rollno;
String name;
double sub1,sub2,sub3,sub4,sub5,total,per;
System.out.println("-----Student Details-----");
name=sc.next();
rollno=sc.nextInt();
sub1=sc.nextDouble();
System.out.println("Entre Subject 2 Marks : ");
sub2=sc.nextDouble();
sub3=sc.nextDouble();
sub4=sc.nextDouble();
sub5=sc.nextDouble();
total=sub1+sub2+sub3+sub4+sub5;
per=total/5;
if(per>75)
else
Output:
Q 3. Write a Java program to find the maximum and minimum value
of an array.
Code:
import java.util.Scanner;
int n=sc.nextInt();
int i,sum=0,min,max;
for(i=0;i<n;i++)
min=arr[0];
max=arr[0];
for(i=0;i<n;i++)
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
Output:
Q 4. Write a menu driven program for stationary shop.Items are
1:Pen 2:Pencil 3:NoteBook 4:Bottle 5:ColorBox.
Code:
import java.util.Scanner;
System.out.println("-----Stationary Shop-----");
System.out.println("-------------------------");
int qty,total=0;
boolean flag=false;
while(flag!=true)
int ch=sc.nextInt();
switch(ch)
{
case 1:
qty=sc.nextInt();
total=total+(qty*10);
break;
case 2:
qty=sc.nextInt();
total=total+(qty*5);
break;
case 3:
qty=sc.nextInt();
total=total+(qty*20);
break;
case 4:
qty=sc.nextInt();
total=total+(qty*30);
break;
case 5:
qty=sc.nextInt();
total=total+(qty*50);
break;
case 6:
break;
case 7:
System.out.println("------------------------------");
break;
default:
System.out.println("Invalid Option");
Output:
Q 5: Write a Java program to accept 2D array elements.Display all
elements
Code:
import java.util.Scanner;
int row,col,i,j;
int[][] arr;
row=sc.nextInt();
col=sc.nextInt();
arr=new int[row][col];
for(i=0;i<row;i++)
for(j=0;j<col;j++)
arr[i][j]=sc.nextInt();
for(i=0;i<row;i++)
for(j=0;j<col;j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
Output:
Code:
import java.util.Scanner;
int row1,col1,row2,col2,i,j;
int[][] arr1,arr2,sum;
row1=sc.nextInt();
col1=sc.nextInt();
System.out.println("Input Number of Rows and Columns of Second Matrix -");
row2=sc.nextInt();
col2=sc.nextInt();
arr1=new int[row1][col1];
arr2=new int[row2][col2];
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
arr1[i][j]=sc.nextInt();
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
arr2[i][j]=sc.nextInt();
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
sum[i][j]= arr1[i][j]+arr2[i][j];
System.out.print(sum[i][j]+" ");
System.out.println();
}
else
Output:
Code:
import java.util.Scanner;
int size;
size=sc.nextInt();
String str="";
for(int i=0;i<ch.length;i++)
ch[i]=sc.next().charAt(0);
for(int i=0;i<ch.length;i++)
str=str+ch[i];
Output:
Q 8. Write a program to accept array of string.Display all elements in
uppercase.
Code:
import java.util.Scanner;
//String n1=sc.next();
int n=sc.nextInt();
sc.next();
for(int i=0;i<name.length;i++)
name[i]=sc.nextLine();
for(int i=0;i<name.length;i++)
System.out.println(name[i].toUpperCase());
Output:
Assignments Day 3
1:Create Math class
Code:
public class MathClass
int c=a+b;
System.out.println("\n");
int w=x+y+z;
System.out.println("\n");
double d3=d1+d2;
System.out.println("\n");
}
public void add4(String s1,String s2)
System.out.println("\n");
System.out.println();
System.out.println("Name : "+name);
for(int i=0;i<skill.length;i++)
System.out.print(" "+skill[i]);
System.out.println();
String s5 = i1+f1+s4;
Code:
import java.util.Scanner;
int n=sc.nextInt();
float f=sc.nextFloat();
String s=sc.next();
o.add5(n, f, s);
String s1=sc.next();
String s2=sc.next();
o.add4(s1, s2);
Output:
The method should find the sum of all the integers passed and
display the result. Write
a client program to call the method with variable number of
integers.
Code:
import java.util.Scanner;
//customer/client/user code
int n1,n2;
n1=sc.nextInt();
n2=sc.nextInt();
mObj.add(n1, n2);
int n3=sc.nextInt();
mObj.add2(n1,n2,n3);
System.out.println("\n");
double d1=sc.nextDouble();
double d2=sc.nextDouble();
mObj.add3(d1, d2);
System.out.println("-----Enter Strings-----");
String s1=sc.next();
String s2=sc.next();
mObj.add4(s1, s2);
Output:
1.3:Write a displayStudInfo() method that accpets student name
and variable number of skiils .The method should display students
name with skills passed.
Code:
public class TestMathClass2 {
obj.numbers("Red", "PHP","HTML","JavaScript");
obj.numbers("Yellow", "Python","React","Node.js");
Output: