Java Programming Solved Programs Sheet 2 by MCA Scholar's Group

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

Java Programming Solved Programs Sheet 2 By MCA

Scholar’s Group ✨
@rushipatil71

1.W a p to display first 20 even numbers


import java.util.*;

public class even20

public static void main(String args[])

{int no=1,even=0;

System.out.println("First 20 Even NUmbers");

while(no<=20)

even=even+2;

System.out.println(" "+even);

no++;

}}}
2) W a p to display first 30 odd numbers and display sum of that numbers
import java.util.*;

public class odd30

public static void main(String args[])

int no=1,odd=-1,sum=0;

System.out.println("First 30 odd Numbers");

while(no<=30)

odd=odd+2;

System.out.print(" "+odd);

sum=sum+odd;

no++;

}
System.out.println("\nSum of all od numbers "+sum);

3) W a p to calculate factorial of entered number

import java.util.*;

public class fact

public static void main(String args[])

int num, fact=1,a=1;

System.out.println("Enter a Number");

Scanner sc=new Scanner(System.in);

num=sc.nextInt();

while(a<=num)

fact=fact*a;

a=a+1;
}

System.out.print("Factorial of Given Number Is "+fact);}

4) W a p to calculate sum of digit of numbers ( Ex: 412= 7)


import java.util.*;

public class sumdigit

public static void main(String args[])

int num,a=0,sum=0;

System.out.println("Enter a Digit");

Scanner sc=new Scanner(System.in);

num=sc.nextInt();

while(num!=0)

a=num%10;

sum=sum+a;

num=num/10;
}

System.out.print("Sum of Digit Of number "+sum);}

5) W a p to calculate entered number is palindrome or not


import java.util.*;

public class palindrome

public static void main(String args[])

int num,a=0,b,sum=0;

System.out.println("Enter a Digit");

Scanner sc=new Scanner(System.in);

num=sc.nextInt();

b=num;

while(num!=0){

a=num%10;

sum=(sum*10)+a;

num=num/10;
}

if(b==sum){

System.out.print("Entered Number Is Palindrome");}

else{

System.out.print("Entered Number Is Not Palindrome");}

}}

6) W a p to display revers of entered number


import java.util.*;

public class revers

public static void main(String args[])

int num,a=0,rev=0;

System.out.println("Enter a number to revers");

Scanner sc=new Scanner(System.in);

num=sc.nextInt();

while(num!=0)

a=num%10;

rev=(rev*10)+a;

num=num/10;
}

System.out.print("Revers of Entered Number Is "+rev);

}}

7) W a p to display Fibonacci series up to 20


import java.util.*;

public class fib20

public static void main(String args[])

int fibo=1, num1=0,num2=1;

System.out.print("Fibonacci series up to 20\n"+num1);

while(fibo<=20)

System.out.print(" "+fibo);

fibo=num1+num2;

num1=num2;

num2=fibo;

}
}}

8) W a p to find entered number is Armstrong number or not


import java.util.*;

public class armstrong{

public static void main(String args[]){

int num,orignal, rem, res=0;

System.out.print("Enter Three Digit input");

Scanner sc =new Scanner(System.in);

num=sc.nextInt();

orignal=num;

while(orignal!=0)

rem=orignal%10;

res+=rem*rem*rem;

orignal=orignal/10;

if(res==num){

System.out.print(num+" number Is Armstrong");

}else

{System.out.print(num+" number Is Not Armstrong");

}}}
9) Write a program of class addition of two numbers by using void method and without void method.

class classaddition{

public void add(int a, int b){

int sum =a+b;

System.out.println("addition of two numbers by using void method:"+sum);

public double add(double a, double b){

double sum1=a+b;

return sum1;

public static void main (String[]args)

classaddition obj = new classaddition();

obj.add(10,20);

double return1=obj.add(10,20.0);

System.out.println("addition of two numbers by without void method:"+return1);

}}
10) Write a program of class student, accept student rollno, name, three subject marks, mob no and
calculate total, percentage and remark of student as,

If per >70 = dis

Per 60-70 = first class,

Per 50-60 = second class

Per 40-50 = pass class otherwise fail

import java.util.*;

class studclass{

int stud_rol,s1,s2,s3,stud_mob,total,per;

String stud_name;

void insert()

System.out.println("Enter Student Roll Number:");

Scanner sc= new Scanner(System.in);

stud_rol=sc.nextInt();

System.out.println("Enter Student name:");

stud_name=sc.next();

System.out.println("Enter Student Math marks:");

s1=sc.nextInt();

System.out.println("Enter Student Hindi marks:");


s2=sc.nextInt();

System.out.println("Enter Student English marks:");

s3=sc.nextInt();

System.out.println("Enter Student Mobile Number:");

stud_mob=sc.nextInt();

void calculate()

total=s1+s2+s3;

System.out.println("Total of subject:"+total);

void persentage(){

per=total/3;

System.out.println("Student persentage is :"+per);

if(per>=70){

System.out.println("Student pass with Distinction ");

}else{

if(per<=70&&per>=60){

System.out.println("Student Pass with First Class");

}else{

if(per<60&&per>=50){

System.out.println("Student Pass with Second Class");

}else{

if(per<50&&per>=40){
System.out.println("Student Pass Class");

}else{

System.out.println("Student Faill");

}}}}}

public static void main(String args[]){

studclass ob=new studclass();

ob.insert();

System.out.println("Enter Student Roll Number: "+ob.stud_rol);

System.out.println("Enter Student name: "+ob.stud_name);

System.out.println("Enter Student Math marks: "+ob.s1);

System.out.println("Enter Student Hindi marks: "+ob.s2);

System.out.println("Enter Student english marks: "+ob.s3);

System.out.println("Enter Student Mobile Number: "+ob.stud_mob);

ob.calculate();

ob.persentage();

}}
11) Write a program to create dmart class with accepting values of product id,name,rate and quantity and
calculate payable amt with discount

If amt 5000-3000 =20 %

3000-2000 =15%

2000-1000=10%

Other wise no discount

import java.util.*;

public class dmart

int pid,qantity,rate,amount,dis,tot;

String pname;

void input(){
Scanner sc=new Scanner(System.in);

System.out.println("product id=");

pid=sc.nextInt();

System.out.println("product name=");

pname=sc.next();

System.out.println("product qantity=");

qantity=sc.nextInt();

System.out.println("product rate=");

rate=sc.nextInt();

void calculate(){

amount=qantity*rate;

void discount()

if(amount>5000){

dis=(amount*20)/100;}

if(amount<5000&&amount>3000){

dis=(amount*15)/100;}

if(amount<3000&&amount>1000){

dis=(amount*7)/100;}

if(amount<1000){

dis=0;

}
}

void total()

tot=amount-dis;

public static void main(String args[])

dmart pd=new dmart();

pd.input();

pd.calculate();

pd.discount();

pd.total();

System.out.println("-------|| welcome In Dmart ||-----");

System.out.println(" product id :"+pd.pid);

System.out.println(" product name :"+pd.pname);

System.out.println(" product qantity :"+pd.qantity);

System.out.println(" product rate :"+pd.rate);

System.out.println(" product amount :"+pd.amount);

System.out.println(" product discount :"+pd.dis);

System.out.println(" product total :"+pd.tot);

System.out.println("-------| Thanks For Coming In Dmart|-----");

You might also like