1.armstrong Number: Public Class Public Static Void Int Int Int While
1.armstrong Number: Public Class Public Static Void Int Int Int While
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
o/p
1
5
10
15
20
25
5.Print reverse numbers from 100 to 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;
employee();
eno=2;
ename="emp2";
eage=28;
employee();
eno=3;
ename="emp3";
eage=30;
employee();
}
o/p
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
{
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 {
o/p
9.Overloading
o/p
Student Number:1
Student Name:siri
Student Name:siri
Student Address:RTP
Student Name:siri
Stundent Age:30
Student Address:RTP
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();
}
o/p
import java.util.Scanner;
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
o/p:
Enter the String
siri
{r=1, s=1, i=2}
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.*;
o/p:
List of jobs are:
Teacher
IT Professional
Govt Employee
Private Employee
Doctor
Select the job from the list:
IT Professional
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;
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
===============General.java (GeneralDetails
package)============
package GeneralDetails;
}
}
=============Personal.java (GeneralDetails.PersonalDetails is the
packagename)===
package GeneralDetails.PersonalDetails;
package GeneralDetails.PersonalDetails.MaritalStatus;
import GeneralDetails.PersonalDetails.Personal;
import GeneralDetails.PersonalDetails.MaritalStatus.Status;
=====================C Logics=========================
1.Armstrong
import java.util.Scanner;
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:
2.Factorial
import java.util.Scanner;
for(int i=1;i<=n;i++)
fact=fact*i;
System.out.print("Factorial of "+n+ " is " +fact);
o/p :
3.Fibonacci Series
import java.util.Scanner;
}
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;
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;
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;
int rem,rev=0,n=num;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
o/p:
Enter a number to reverse: 345
Reverse number of 345 is: 543
7.Sum of Digits
import java.util.Scanner;
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
a=a+b;
b=a-b;
a=a-b;
o/p:
Enter two numbers: 45 67
Swapping of 45 and 67 are: 67 and 45
int temp=a;
a=b;
b=temp;
o/p:
Enter two numbers: 23 89
Swapping of 23 and 89 are: 89 and 23