0% found this document useful (0 votes)
64 views18 pages

Java Programs

The document contains 13 code examples demonstrating various Java programming concepts like checking if a number is prime, counting even numbers, temperature conversion, checking if a number is Armstrong, bitwise operators, finding numbers divisible by 7, checking if a number is even/odd, calculating factorials, using Math functions, checking if a number is palindrome, calculating sum of digits, reversing a number, calculating percentage and total marks. The code examples are grouped under two chapters - basic syntactical constructs and derived syntactical constructs, with the latter including examples of constructors and command line arguments.

Uploaded by

Raj Debadwar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
64 views18 pages

Java Programs

The document contains 13 code examples demonstrating various Java programming concepts like checking if a number is prime, counting even numbers, temperature conversion, checking if a number is Armstrong, bitwise operators, finding numbers divisible by 7, checking if a number is even/odd, calculating factorials, using Math functions, checking if a number is palindrome, calculating sum of digits, reversing a number, calculating percentage and total marks. The code examples are grouped under two chapters - basic syntactical constructs and derived syntactical constructs, with the latter including examples of constructors and command line arguments.

Uploaded by

Raj Debadwar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 18

JAVA PROGRAMS

Chap-1 Basic Systactical Construct in Java


1)WAP to check Whether No. is Prime Or Not.
Code:
import java.util.*;
class prime{
public static void main(String arg[]) {
int no,m,flag=0;
Scanner s=new Scanner(System.in);
System.out.print("\nEnter Any No.=");
no=s.nextInt();
m=no/2;
for(int i=2;i<=m;i++) {
if(no%i==0) {
System.out.println(+no+" is Not Prime");
flag=1;
break;
}
}
if(flag==0) {
System.out.println(+no+" is Prime"); }
}
}
Output:

2)WAP to To Claculate Even No. from Larg Integer No.


Code:
import java.util.*;
class CountEveno {
public static void main(String arg[]) {
int no,rem,count=0;
Scanner s=new Scanner(System.in);
System.out.print("\nEnter Any No.=");
no=s.nextInt();
while(no!=0) {
rem=no%10;
if(rem%2==0) {
++count;
}
no=no/10;

}
System.out.println("The Total Even No. Present in "+no+" is="+count);
}
}
Output:

3)WAP to Convert FaCelcius Temp into Faherheit using Formula(F= (C * 1.8) +32).
Code:
import java.util.*;
class ConvertTemp{
public static void main(String arg[]) {
int cel;
float fah;
Scanner s=new Scanner(System.in);
System.out.print("\nEnter Celsius Tempreture=");
cel=s.nextInt();
fah=(float)(cel*1.8)+32;
System.out.println("After Converting Fahreheit="+fah+"'F");
}
}
Output:
4)WAP To Check Whether No. is Armstrom Or Not.
Code:
import java.util.*;
class aram1 {
public static void main(String arg[]) {
int n,sum=0,rem,temp;
System.out.println("Enter no:");
Scanner s=new Scanner(System.in);
n=s.nextInt();
temp=n;
while(n!=0) {
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(temp==sum) {
System.out.println("no is Armastrong:");
}
else {
System.out.println("no is not Armastrong:");
}
}
}
Output:

5)WAP to Perform Bitwise Oprator.


Code:
import java.lang.*;
class bitwise1 {
public static void main(String arg[]) {
int a=60;
int b=13;
int c=0;
c=a&b;
System.out.print("\n a&b="+c);
c=a^b;
System.out.print("\n a^b="+c);
c=~a;
System.out.print("\n ~a="+c);
c=a<<2;
System.out.print("\n a<<2="+c);
c=a>>>2;
System.out.print("\n a>>>2="+c);
}
}
Output:

6)WAP to Print No. Divisible By 7 100 To 200.


Code: import java.util.*;
class NoDivBy7
{
public static void main(String abc[])
{
int i=7;
for(i=100;i<=200;i++)
{
if(i%7==0)
{
System.out.println(+i);
}
}
}
}
Output:

7)WAP To Check Whether No. is Even Or Odd.


Code:
//import java.util.*;
class EvenOdd {
public static void main(String abc[])
{
int a;
Scanner s=new Scanner(System.in);
System.out.println("Enter any Number :");
a=s.nextInt();
if(a%2==0)
{
System.out.println("Number is even!!");
}
else
{
System.out.println("Number is odd!!");
}

}
}
Output:
8)WAP to Calculate Factorial of No.
import java.util.*;
class fact
{
public static void main(String abc[])
{
int no,i,fact=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number :");
no=s.nextInt();
for(i=1;i<=no;i++)
{
fact=fact*i;
}
System.out.println("Factorial :"+fact);
}
}
Output:

9)WAP Implement Math Function.


Code:class MathFunction{
public static void main(String arr[]) {
double a=10,b=16;
double z=1.8,x=-90;
double l=Math.min(a,b);
double q=Math.max(a,b);
double p=Math.sqrt(b);
double r=Math.round(z);
double o=Math.abs(x);
double m=Math.exp(a);
double v=Math.asin(a);
System.out.println("MIN()="+l);
System.out.println("MAX()="+q);
System.out.println("SQRT()="+p);
System.out.println("ROUND()="+r);
System.out.println("ABS()="+o);
System.out.println("EXP()="+m);
System.out.println("ASIN()="+v);
}
}
Output:

10)WAP To Check Whether No. is Palindrom Or Not.


Code: import java.util.*;
class palindrome {
public static void main(String arg[]) {
int n,rev=0,rem,temp;
System.out.println("Enter no:");
Scanner s=new Scanner(System.in);
n=s.nextInt();
temp=n;
while(n!=0) {
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(temp==rev) {
System.out.println("no is palindrome!!"+rev);
}
Else {
System.out.println("no is not palindrome!!"+rev);
}
}
}
Output:

11)WAP to Calculate Sum of Digit.


Code:
import java.util.*;
class Sum_of_Digit
{
public static void main(String abc[])
{
int no,rem,sum=0;
System.out.println("Enter any Number :");
Scanner s=new Scanner(System.in);
no=nextInt();
while(no!=0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
System.out.println("Sum="+sum);
}
}
Output:

12)WAP To Reverse the entred No.


Code:
import java.util.*;
class Reverse1
{
public static void main(String abc[])
{
int no,rem,sum=0;
System.out.println("Enter any Number :");
Scanner s=new Scanner(System.in);
no=s.nextInt();
while(no!=0)
{
rem=no%10;
sum=sum*10+rem;
no=no/10;
}
System.out.println("Reverse="+sum);
}
}
Output:

13)WAP to Calculate Percentage & Total Marks.


Code:
import java.util.*;
class student {
public static void main(String args[]) {
int s1,s2,s3,s4,s5,total;
float avg;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the english marks:");
s1=sc.nextInt();
System.out.println("Enter the maths marks :");
s2=sc.nextInt();
System.out.println("Enter the scince marks :");
s3=sc.nextInt();
System.out.println("Enetre the physcice marks:");
s4=sc.nextInt();
System.out.println("Enetr the Java marks:");
s5=sc.nextInt();
total=(s1+s2+s3+s4+s5);
avg=(float)total/5;
System.out.println("*****************************");
System.out.println("********STUDENT INFO*********");
System.out.println("******************************");
System.out.println("English :"+s1);
System.out.println("Maths :"+s2);
System.out.println("science :"+s3);
System.out.println("Physcice :"+s4);
System.out.println("Java :"+s5);
System.out.println("=================================");
System.out.println("show totale="+total);
System.out.println("show Average="+avg+"%");

}
}
Output:

Chap-2 Derived Syntatical Constructor in Java.


1)WAP to Implement Constructor in Java.
Code:
import java.util.*;
class demo1 {
int a,b,c;
demo1() {
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter 2 No.=");
a=sc.nextInt();
b=sc.nextInt();

}
void display() {
c=a+b;
System.out.println("\nAddition="+c);
}
}
class Constuctor {
public static void main(String arg[]) {
demo1 d=new demo1();
d.display(); }
}
Output:

2)WAP to Show Overloading of Constructor.


Code:
class demo1 {
int a,b,c;
demo1() {
a=100;
b=200;
c=a+b;
}
demo1(int x) {

a=x;
b=100;
c=a+b;

}
demo1(int x,int y) {

a=x;
b=y;
c=a+b;
}

void display() {
c=a+b;
System.out.println("\nAddition="+c);
}

public static void main(String arg[]) {


demo1 d=new demo1();
demo1 d1=new demo1(100);
demo1 d2=new demo1(500,700);

d.display();
d1.display();
d2.display();

}
}
Output:

3)WAP to get Command line argument & perform addition


Code:
class command_line

{
public static void main(String arg[])
{
String s1=arg[0];
String s2=arg[1];
int a=Integer.parseInt(s1); //int a=Integer.parseInt(arg[0]);
int b=Integer.parseInt(s2); // int b=Integer.parseInt(arg[1]);
int c;
c=a+b;
System.out.println("\nAddition ="+c);
}
}
Output:

4)WAP to accept No. from Command line & check No. is Prime Or Not.
Code:
class Prime
{
public static void main(String arg[]) {
int a=Integer.parseInt(arg[0]);
int m,flag=0;
m=a/2;
for(int i=2;i<=m;i++) {
if(a%2==0)
{
System.out.println("\n"+a+" No. is Not Prime");
flag=1;
break;
}
}
if(flag==0)
System.out.println("\n"+a+" No. is Prime");

}
}
Output:
5)) “this” Keyword
1)WAP to Show Use of “this” keyword as hiding instance variable .
Code: class This_demo1 {
int a,b,c;
This_demo1(int a,int b) {
this.a=a;
this.b=b;
c=a+b;
}
void display() {
c=a+b;
System.out.println("\nAddition="+c);
}
public static void main(String arg[]) {

This_demo1 d2=new This_demo1(500,700);


d2.display();
}
}

2) WAP to Show Use of “this” keyword as Calling Constructor using another Constructor.
Code: class This_demo2 {
int a,b,c;
This_demo2 () {
a=100;
b=200;
c=a+b;
}
This_demo2(int z) {
a=z;
b=100;
c=a+b;
}
This_demo2(int x,int y,int z) {
this(z);
a=x;
b=y;
c=a+b;
}
void display() {
c=a+b;
System.out.println("\nAddition="+c);
}
public static void main(String arg[]) {
demo1 d=new demo1();
demo1 d1=new demo1(1000);
demo1 d2=new demo1(500,700);
d.display();
d1.display();
d2.display();
}
}
Output:

3) WAP to demonstreat Outer & Inner Class.


Code:
class outerinnerclass{
class inner{
void show(){
System.out.println("Inner Class");
}

}
void display(){
System.out.println("Outer Class");
inner i=new inner();
i.show();
}
public static void main(String arg[]){
outerinnerclass o=new outerinnerclass();
o.display();
}
}
output:

4)WAP to
demonstreat Outer & Inner Class(Outside the class).
code:
class outerinnerclass1{
static class inner{
void show(){
System.out.println("Inner Class");
}

}
void display(){
System.out.println("Outer Class");
}
}
class demo{
public static void main(String arg[])
{
outerinnerclass1 o=new outerinnerclass1();
o.display();
outerinnerclass1.inner i=new outerinnerclass1.inner();
i.show();
}
}
output:

5)

You might also like