0% found this document useful (0 votes)
95 views

1.armstrong Number: Public Class Public Static Void Int Int Int While

The document contains 11 code examples demonstrating various Java programming concepts: 1. The first example checks if a number is an Armstrong number by summing the cubes of its digits. 2. The second example calculates the sum of numbers from 1 to 10 using a for loop. 3. Additional examples demonstrate printing even numbers between 1-10, a number series, reversing numbers from 100 to 1, storing employee details in variables, multilevel inheritance, hybrid inheritance, method overloading, and overloading and overriding methods. 4. The last two examples cover calculating average marks of a student using arrays and counting characters in a string.

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

1.armstrong Number: Public Class Public Static Void Int Int Int While

The document contains 11 code examples demonstrating various Java programming concepts: 1. The first example checks if a number is an Armstrong number by summing the cubes of its digits. 2. The second example calculates the sum of numbers from 1 to 10 using a for loop. 3. Additional examples demonstrate printing even numbers between 1-10, a number series, reversing numbers from 100 to 1, storing employee details in variables, multilevel inheritance, hybrid inheritance, method overloading, and overloading and overriding methods. 4. The last two examples cover calculating average marks of a student using arrays and counting characters in a string.

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

1.

Armstrong Number
public class Armstrong
{
public static void main(String[] args)
{
int n=153;
int temp=n;
int r,sum=0;

while(n>0)
{
r = n%10;
n = n/10;
sum = sum+r*r*r;
}

if(temp==sum)
System.out.println("Its an armstrong number");
else
System.out.println("Not an armstrong number");

o/p
Its an armstrong number

2.Add 1 to 10 numbers
public class SumofTen
{
public static void main(String[] args)
{
int sum=0;

for(int i=1;i<=10;i++)
{
sum=sum+i;
}
System.out.println("Sum of 1 to 10 is:"+sum);

o/p
Sum of 1 to 10 is:55
3.Even numbers b/w 1 to 10
public class Even
{
public static void main(String[] args)
{
int i;
for(i=1;i<=10;i++)
{
if(i%2==0)
System.out.println("Even Number:"+i);
}

o/p
Even Number:2
Even Number:4
Even Number:6
Even Number:8
Even Number:10

4.Print series- 1,5,10,15,20,25


class series {

public static void main(String[] args) {


for(int i=0;i<=25;i=i+5)
{
if(i==0)
System.out.println(1);
else
System.out.println(i);

o/p

1
5
10
15
20
25
5.Print reverse numbers from 100 to 1

public class Reverse


{
public static void main(String[] args)
{
int i=100;
do
{
System.out.println("Print:"+i);
i--;
}
while(i>=1);

o/p

Print:100
Print:99
Print:98
Print:97
Print:96
Print:95
Print:94
Print:93
Print:92
Print:91
Print:90
Print:89
Print:88
Print:87
Print:86
Print:85
Print:84
Print:83
Print:82
Print:81
Print:80
Print:79
Print:78
Print:77
Print:76
Print:75
Print:74
Print:73
Print:72
Print:71
Print:70
Print:69
Print:68
Print:67
Print:66
Print:65
Print:64
Print:63
Print:62
Print:61
Print:60
Print:59
Print:58
Print:57
Print:56
Print:55
Print:54
Print:53
Print:52
Print:51
Print:50
Print:49
Print:48
Print:47
Print:46
Print:45
Print:44
Print:43
Print:42
Print:41
Print:40
Print:39
Print:38
Print:37
Print:36
Print:35
Print:34
Print:33
Print:32
Print:31
Print:30
Print:29
Print:28
Print:27
Print:26
Print:25
Print:24
Print:23
Print:22
Print:21
Print:20
Print:19
Print:18
Print:17
Print:16
Print:15
Print:14
Print:13
Print:12
Print:11
Print:10
Print:9
Print:8
Print:7
Print:6
Print:5
Print:4
Print:3
Print:2
Print:1

6.Employee Details
public class EmpDetails
{
static int eno, eage;
static String ename;

static void employee()


{
System.out.println("The emp details
are"+'\n'+"eno="+eno+'\n'+"ename="+ename+'\n'+"eage="+eage);
}
public static void main(String[] args)
{
eno=1;
ename="emp1";
eage=26;

employee();

eno=2;
ename="emp2";
eage=28;

employee();

eno=3;
ename="emp3";
eage=30;

employee();

}
o/p

The emp details are


eno=1
ename=emp1
eage=26
The emp details are
eno=2
ename=emp2
eage=28
The emp details are
eno=3
ename=emp3
eage=30

7.Multilevel

class MainClass
{
void main()
{
System.out.println("This is mainclass");
}
}
class SubClass extends MainClass
{
void sub()
{
System.out.println("This is subclass");
}
}
class ChildClass extends SubClass
{
void child()
{
System.out.println("This is childclass");
}
}
public class MultiLevel
{

public static void main(String[] args)


{
MainClass c1=new MainClass();
c1.main();
SubClass c2=new SubClass();
c2.sub();
c2.main();
ChildClass c3=new ChildClass();
c3.child();
c3.sub();
c3.main();

o/p

This is mainclass
This is subclass
This is mainclass
This is childclass
This is subclass
This is mainclass

8.Hybrid
class Apple
{
void apple()
{
System.out.println("This is my 1st favourite fruit");
}
}
class Banana extends Apple
{
void banana()
{
System.out.println("This is my 2nd favourite fruit");
}
}
class Cherry extends Banana
{
void cherry()
{
System.out.println("This is my 3rd favourite fruit");
}
}
class Dates extends Banana
{
void dates()
{
System.out.println("This is my 4th favourite fruit");
}
}
public class Hybrid {

public static void main(String[] args)


{
Apple a=new Apple();
a.apple();
Banana b=new Banana();
b.banana();
b.apple();
Cherry c=new Cherry();
c.cherry();
c.banana();
c.apple();
Dates d=new Dates();
d.dates();
d.banana();
d.apple();

o/p

This is my 1st favourite fruit


This is my 2nd favourite fruit
This is my 1st favourite fruit
This is my 3rd favourite fruit
This is my 2nd favourite fruit
This is my 1st favourite fruit
This is my 4th favourite fruit
This is my 2nd favourite fruit
This is my 1st favourite fruit

9.Overloading

public class Overloading


{
public void student(int sno,String sname)
{
System.out.println("Student Number:"+sno);
System.out.println("Student Name:"+sname);
}
public void student(String sname,String sadd)
{
System.out.println("Student Name:"+sname);
System.out.println("Student Address:"+sadd);
}
public void student(String sname,int sage,String sadd)
{
System.out.println("Student Name:"+sname);
System.out.println("Stundent Age:"+sage);
System.out.println("Student Address:"+sadd);
}
public static void main(String[] args)
{
Overloading ol=new Overloading();
ol.student(001,"siri");
ol.student("siri","RTP");
ol.student("siri",30,"RTP");

o/p

Student Number:1
Student Name:siri
Student Name:siri
Student Address:RTP
Student Name:siri
Stundent Age:30
Student Address:RTP

10.Overloading and Overriding


class Poly1
{
void display()
{
System.out.println("Logic 1");
}
}
class Poly2 extends Poly1
{
void display()
{
System.out.println("Logic 2");
}
void display(int x)
{
System.out.println("Logic " +x);
}
}
public class Polymorphism
{
public static void main(String[] args)
{

Poly1 p1=new Poly1();


Poly2 p2=new Poly2();

p1.display();
p2.display();
p2.display(3);
}

}
o/p

Logic 1
Logic 2
Logic 3

11.ATM
abstract class Rose
{
abstract public void display();
abstract public void deposit();
abstract public void withdraw();
abstract public void balance();
abstract public void atmexit();
}

public class Atm extends Rose


{
int bal=10000;
int dep=3000;
int with=2000;

public void display()


{
System.out.println("Display the amount: "+bal);
}
public void deposit()
{
System.out.println("Deposit amount: "+dep);
}
public void withdraw()
{
System.out.println("Withdraw amt: "+with);
}
public void balance()
{
System.out.println("Balance: "+bal);
}
public void atmexit()
{
System.exit(0);
}
public static void main(String[] args)
{

Atm a=new Atm();


a.display();
a.deposit();
a.balance();
a.atmexit();
a.withdraw();

o/p

Display the amount: 10000


Deposit amount: 3000
Balance: 10000

[email protected]

===================Part 2 from Arrays==========================

1.Avg marks of a student

import java.util.Scanner;

public class AvgMarks


{
public static void main(String[] args)
{
float average,sum=0;

Scanner s=new Scanner(System.in);


int a[]=new int[5];
System.out.println("Enter stundent marks in 5 subjects: ");

for(int i=0;i<5;i++)
{
a[i]=s.nextInt();
sum=sum+a[i];
}
average=sum/5;
System.out.println("Average of the marks :"+average);
s.close();

}
}

o/p:
Enter stundent marks in 5 subjects:
75
82
77
90
85
Average of the marks :81.8

2.Counting characters of a given string


import java.util.*;

public class CharCounter


{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String str=sc.nextLine();

HashMap<Character,Integer> map=new HashMap<>();


for(char ch:str.toCharArray())
{
if(map.containsKey(ch))
{
int val=map.get(ch);
map.put(ch, val+1);
}
else
{
map.put(ch, 1);
}
}
System.out.println(map);
}

o/p:
Enter the String
siri
{r=1, s=1, i=2}

3.States and Capitals through hashmap


import java.util.*;
//import java.util.HashMap;
//import java.util.Map.Entry;

public class HashMapExp


{
public static void main(String[] args)
{
HashMap<String, String> h=new HashMap<String, String>();
h.put("AP","Amaravathi");
h.put("Telangana","Hyd");
h.put("Tamilnadu","Chennai");
h.put("Karnataka","Banglore");
h.put("MP","Bhopla");

System.out.println("States and Capitals are: ");


Set s=h.entrySet();
Iterator itr=s.iterator();
while(itr.hasNext())
{
Map.Entry me=(Map.Entry) itr.next();
Object skey=me.getKey();
Object val=me.getValue();
System.out.println(skey+" "+val);
}

o/p:
States and Capitals are:
MP Bhopla
Telangana Hyd
Karnataka Banglore
Tamilnadu Chennai
AP Amaravathi
4.List of jobs through Arraylist
import java.util.*;

public class JobList


{
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();
al.add("Teacher");
al.add("IT Professional");
al.add("Govt Employee");
al.add("Private Employee");
al.add("Doctor");

System.out.println("List of jobs are: ");


Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
System.out.println("Select the job from the list: ");
String s=al.get(1);
System.out.println(s);

o/p:
List of jobs are:
Teacher
IT Professional
Govt Employee
Private Employee
Doctor
Select the job from the list:
IT Professional

5.Printing Tomatto through HashSet


import java.util.*;

public class Tomato


{
public static void main(String[] args)
{
String s="Tomatto";
HashSet hs=new HashSet();

for(char res:s.toCharArray())
{
hs.add(res);
}

Iterator itr=hs.iterator();
while(itr.hasNext())
{
Object ob=itr.next();
ob.toString();
System.out.println(ob);
}

o/p:
a
T
t
m
o

6.ExceptionHandling
public class ExceptionHandling
{
private static String s="abc";
public static void main(String[] args)
{
int a=30, b=0,c;
s=null;
int d[]= {10,20,30};
try
{
c=a/b;
System.out.println("Result: "+c);

if(s.equals("abc"))
System.out.println("Same");
else
System.out.println("Not same");

a=Integer.parseInt(s);
d[4]=40;
}
catch(ArithmeticException ae)
{
System.out.println("You shouldn't divide anumber by zero");
}
catch(NullPointerException npe)
{
System.out.println("NullPointerException Error");
}
catch(NumberFormatException nfe)
{
nfe.printStackTrace();
}
catch(ArrayIndexOutOfBoundsException abe)
{
System.out.println("Array is out of Bounds");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
System.out.println("finally block would be executed for
sure");
}

}
}

o/p:
You shouldn't divide anumber by zero
finally block would be executed for sure

7.Packages
1.Creating a class as Teacher and packagename as College
package College;

public class Teacher


{
public String first;
public String last;
public String email;
public int id;
public String phone;

public Teacher(String first, String last, String email, int id,


String phone)
{
this.first=first;
this.last=last;
this.email=email;
this.id=id;
this.phone=phone;
}
public void display()
{
System.out.println("-------------------------");
System.out.println("----Teacher Details------");
System.out.println("-------------------------");
System.out.println("Name:"+first+" "+last);
System.out.println("Id: "+id);
System.out.println("Email: "+email);
System.out.println("Phone no : "+phone);
}
}

2.Creating another class as Student and packagename as College


package College;

public class Student


{
public String first;
public String last;
public String email;
public int rno;
public String phone;

public Student(String first, String last, String email, int rno,


String phone)
{
this.first=first;
this.last=last;
this.email=email;
this.rno=rno;
this.phone=phone;
}
public void display()
{
System.out.println("-------------------------");
System.out.println("----Student Details------");
System.out.println("-------------------------");
System.out.println("Name:"+first+" "+last);
System.out.println("Roll No: "+rno);
System.out.println("Email: "+email);
System.out.println("Phone no : "+phone);
}

3.Importing College package in default package

Import College.*;

class StudentInfo
{
public static void main(String[] args)
{
Teacher t=new Teacher("SriLaxmi", "Varadi",
"[email protected]", 1011,"9959944406");
Student s=new
Student("Siri","Varadi","[email protected]",11,"6301013072");
t.display();
s.display();
}

}
o/p:
-------------------------
----Teacher Details------
-------------------------
Name:SriLaxmi Varadi
Id: 1011
Email: [email protected]
Phone no : 9959944406
-------------------------
----Student Details------
-------------------------
Name:Siri Varadi
Roll No: 11
Email: [email protected]
Phone no : 6301013072

==============Subpackages another example=============


ProjectName : Employee_Information

===============General.java (GeneralDetails
package)============

package GeneralDetails;

public class General


{
public void EmployeeGeneralDetails()
{
System.out.println("The student no is 101");
System.out.println("The student name is John");
System.out.println("The student age is 30");

}
}
=============Personal.java (GeneralDetails.PersonalDetails is the
packagename)===

package GeneralDetails.PersonalDetails;

public class Personal


{
public void salary()
{
System.out.println("Salary is 50000 per month");
}

public void bonous()


{
System.out.println("Hike percent is 40% of the package");
}

==Status.java (GeneralDetails.PersonalDetails.MaritalStatus is the


packagename)===

package GeneralDetails.PersonalDetails.MaritalStatus;

public class Status


{
public void status()
{
System.out.println("The candidate got married");
}
public void children()
{
System.out.println("The candidate have 2 children");
}
}
==============Accessing.java==================
import GeneralDetails.General;

import GeneralDetails.PersonalDetails.Personal;

import GeneralDetails.PersonalDetails.MaritalStatus.Status;

public class Accessing


{
public static void main(String asa[])
{
General g=new General();
g.EmployeeGeneralDetails();

Personal p=new Personal();


p.salary();
p.bonous();
Status s=new Status();
s.status();
s.children();

=====================C Logics=========================

1.Armstrong
import java.util.Scanner;

public class Armstrong


{

public static void main(String[] args)


{
int n,r,sum=0;

Scanner sc=new Scanner(System.in);


System.out.print("Enter any number to check whether it is an
armstrong number or not: \n");
n=sc.nextInt();

int temp=n;

while(temp>0)
{
r=temp%10;
temp=temp/10;
sum=sum+r*r*r;
}
if(n==sum)
{
System.out.println(n+" is an armstrong number");
}
else
{
System.out.println(n+" is not an armstrong number");
}
}

o/p 1:
Enter any number to check whether it is an armstrong number or not:
153
153 is an armstrong number

o/p 2:

Enter any number to check whether it is an armstrong number or not:


185
185 is not an armstrong number

2.Factorial
import java.util.Scanner;

public class Factorial


{

public static void main(String[] args)


{
int fact=1;

Scanner in=new Scanner(System.in);


System.out.print("Enter a number to find Factorial: ");
int n=in.nextInt();

for(int i=1;i<=n;i++)
fact=fact*i;
System.out.print("Factorial of "+n+ " is " +fact);

o/p :

Enter a number to find Factorial: 5


Factorial of 5 is 120

3.Fibonacci Series
import java.util.Scanner;

public class FibonacciSeries


{

public static void main(String[] args)


{
int k, a=0, b=1;
Scanner sc =new Scanner(System.in);
k=0;
System.out.println("Enter any number to find fibonacci
series:");
int n = sc.nextInt();
System.out.print("0 1 ");
while(k<=n)
{
k=a+b;
if(k>=n)
break;
System.out.print(k + " ");
a=b;
b=k;
}

}
o/p:
Enter any number to find fibonacci series:
50
0 1 1 2 3 5 8 13 21 34

4.Perfect Number
import java.util.Scanner;

public class PerfectNumber


{

public static void main(String[] args)


{
int n, sum=0;

Scanner sc = new Scanner(System.in);


System.out.print("Enter a number to check perfect or not:
");
n=sc.nextInt();

if(n>0)
{
for(int i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
System.out.println(n+" is a perfect number");
else
System.out.println(n+" is not a perfect number");
}
else
System.out.println("Please enter a positive number");

o/p 1:
Enter a number to check perfect or not: 6
6 is a perfect number

o/p 2:
Enter a number to check perfect or not: 12
12 is not a perfect number

5.Prime Number
import java.util.Scanner;

public class PrimeNumbers


{

public static void main(String[] args)


{
boolean a=true;

Scanner in=new Scanner(System.in);


System.out.print("Enter any number to check prime or not: ");
int n=in.nextInt();

for(int i=2;i<n;i++)
{
if(n%i==0)
{
a=false;
break;

}
}
if(a==true)
{
System.out.println(n+" is a prime number");
}
else
{
System.out.println(n+" is not a prime number");
}
}

}
o/p 1:
Enter any number to check prime or not: 13
13 is a prime number

o/p 2:
Enter any number to check prime or not: 25
25 is not a prime number

6.Reverse Number
import java.util.Scanner;

public class ReverseNumber


{

public static void main(String[] args)


{

Scanner in=new Scanner(System.in);


System.out.print("Enter a number to reverse: ");
int num=in.nextInt();

int rem,rev=0,n=num;

while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}

System.out.println("Reverse number of "+num+" is: "+rev);


}

o/p:
Enter a number to reverse: 345
Reverse number of 345 is: 543

7.Sum of Digits
import java.util.Scanner;

public class SumOfDigits


{

public static void main(String[] args)


{
int sum=0,k;

Scanner sc = new Scanner(System.in);


System.out.print("Enter any number to find sum of the
digits: ");
int num=sc.nextInt();

int n=num;

while(n!=0)
{
k=n%10;
n=n/10;
sum=sum+k;
}
System.out.println("sum of the digits of "+num+" is: "+sum);
}

o/p:
Enter any number to find sum of the digits: 4565
sum of the digits of 4565 is: 20

8.Swap Without Temp


import java.util.Scanner;

public class SwapWithoutTemp


{

public static void main(String[] args)


{
Scanner in=new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a=in.nextInt();
int b=in.nextInt();

System.out.print("Swapping of "+a+" and "+b+" are: ");

a=a+b;
b=a-b;
a=a-b;

System.out.print(a+" and "+b);

o/p:
Enter two numbers: 45 67
Swapping of 45 and 67 are: 67 and 45

9.Swap With Temp


import java.util.Scanner;

public class SwapWithTemp


{
public static void main(String[] args)
{

Scanner in = new Scanner(System.in);


System.out.print("Enter two numbers: ");
int a=in.nextInt();
int b=in.nextInt();

System.out.print("Swapping of "+a+" and "+b+" are: ");

int temp=a;
a=b;
b=temp;

System.out.print(a+" and "+b);

o/p:
Enter two numbers: 23 89
Swapping of 23 and 89 are: 89 and 23

You might also like