Inheritance

INHERITANCE

Its just simply say childs can use or access their parent property. It means a child class object can access the super class methods.
There is a important key word used in this concept is extends.
Ex: class child extends father {}

It represents IS-A relationship
ex:Dhoni IS-A Cricketer,
R15 IS-A Bike.

Uses are code reusability, method overriding.

Types of Inheritance
• single inheritance
• mutilevel inheritance
• hierarchical inheritance
• multiple inheritance
• hybrid iheritance

single inheitance
it has single parent class and single child class.

class bird{
void sound(){
System.out.println();
}
void color(){
System.out.println();
}
void eatinghabit(){
System.out.println();
}
public static void main (String [] args){
bird b=new bird();
b.color();
b.sound();
b.eatinghabit();
}

multilevel inheritance
it has chain of inheritance like one below one.

class bird{
void sound(){
System.out.println();
}
}
class bigbird extend bird{
void height(){
System.out.println();
}
}
class peacock extends bigbird{
void color(){
System.out.println();
}
}
class ExamineInheritance{
public static void main(String [] args){
ExamineInheritance EI=new Examine Inheritance();
EI.color();
EI.height();
}
}

Hierarchical inheritance

It shows single parent and multiple child concept.

class bird{
void sound(){
System.out.println();
}
}
class chicken extends bird{
void walk(){
System.out.println(“only walk”);
}
}
class parrot extends bird{
void fly(){
System.out.println(“can walk and fly ”);
}
}
class ExamineInheritance{
public static void main(String [] args){
ExamineInheritance EI=new Examine Inheritance();
EI.walk();
EI.fly();
}
}

Mutiple inheritance
java dosen’t support multiple inheritance through classes. Its only achieved by interfaces.

Multiple inheritance shows a single child using more than one parents and the disadvantage is
suppose the child want to execute function which is common to both parents the child decide which
parent cause dynamic problem.

We can see multiple inheritance in detail in future blogs.
Hybrid Inheritance

This is nothing but the combination of previously shown inheritances of single and hierarchical.
It combine both concepts just shown below.

Single child and multiple child

                                                     grandfather
                                                         |
                                    (single inheritance) |

_______________________son_____________
| |
| (multiple inheritance) |
grandson granddaughter

this example show single and multiple child concept with real life relations.

public class Grandfather{
public void print (){
System.out.println(“kumar”);

    }
    }

public class Son extends Grandfather{
public void print (){
System.out.println(“ram”);
}
}
public class Grandson extends Son{
public void print (){
System.out.println(“raj”);

public class Granddaughter extends Son{
public void print (){
System.out.println(“diya”);
}
public static void main(String [] args){

Granddaughter gd=new Granddaughter();
gd.print();
}

}
RESULT :
diya }
}
now you all know about inheritanc after this we gonna see polymorphism.

System.in,System.out and Scanner

System.in is an InputStream which is typically connected to keyboard input of console programs. System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, or configuration files. In applications with GUI the input to the application is given via the GUI.

At times you might need to read console input provided by the user from the keyboard. The System.in field permits you to read input from the keyboard. The input can be converted into a stream of characters and then buffered so that all characters up to but not including the Enter key can be presented to the program.

System.out.println is a Java statement that prints the argument passed, into the System.out which is generally stdout. System – is a final class in java.lang package. out – is a static member field of System class and is of type PrintStream. println – is a method of PrintStream class.

Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.
• To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. We may pass an object of class File if we want to read input from a file.
• To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example, to read a value of type short, we can use nextShort()
• To read strings, we use nextLine().
• To read a single character, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string.

References:
Stack Overflow
http://www.webucator.com
javapapers
geeksforgeeks

CONSTRUCTOR

Constructor
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes:

constructor program example:
package constructorexcercise;

public class Payticketprice {
int paise;
public Payticketprice() {
paise=100;

}
public static void main(String[] args) {
    Payticketprice payticket=new Payticketprice();// object created here  and constructor called here
    System.out.println("Payticket price "+payticket.paise);
}

}

Result:
Payticket price 100

Note:

  • constructor name match the class name
    *it dosen’t contain return type(like void)
    *All classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object attributes.

Parameters used in constructor example

package constructorexcercise;

public class Buychocolate {
String chocolatename;
int chocolateprice;
public Buychocolate(String name,int price) {
chocolatename=name;
chocolateprice=price;
}
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println(“what chocolate you want to buy”);
Buychocolate buy=new Buychocolate(“Dairymilk”,140);
System.out.println(buy.chocolatename+” price is “+ buy.chocolateprice);

}

}

Result:
what chocolate you want to buy
Dairymilk price is 140

OOPS

Today we gonna create small project to calculate hsc marks.

package Oops;

import java.util.Scanner;

public class Hscmarkcalculator {

int noofsubject; int marks; int total;

public static void main(String[] args) {

// TODO Auto-generated method stub

Hscmarkcalculator hsc=new Hscmarkcalculator();

hsc.getmark();//method calling statement

hsc.Calculategrade();// method calling statement

}

private void Calculategrade() {//method Body

// TODO Auto-generated method stub

if(total>1000)

{

System.out.println(“A Grade”);

}

else if(total > 800)

{

System.out.println(“B Grade “);

}

else

{

System.out.println(“C Grade”);

}

}

private int getmark() {//method body

// TODO Auto-generated method stub

System.out.println(“Please enter number of subjects”);

Scanner scannerObj = new Scanner(System.in);

int len = scannerObj.nextInt();

int[] marks = new int[len];

total = 0;

for (int sub = 0; sub < len; sub++) {

System.out.println(“Please enter marks”);

marks[sub] = scannerObj.nextInt();

System.out.println(“You entered ” + marks[sub]);

total = total + marks[sub];

}

System.out.println(“Your total mark is ” + total);

return total;

}

}

result

Please enter number of subjects

6

Please enter marks

180

You entered 180

Please enter marks

190

You entered 190

Please enter marks

150

You entered 150

Please enter marks

160

You entered 160

Please enter marks

170

You entered 170

Please enter marks

178

You entered 178

Your total mark is 1028

A Grade

In the above program we create method calling and method body. Here we use the return type, that repeat the value where the method was calling.

Note:after return keyword line, below lines are not execueted.

Method calling

hsc.getmark();//method calling statement

hsc.Calculategrade();// method calling statement

Method body

private int getmark()

{

}

private void Calculategrade()

{

}

Object:

object is nothing but, combination of state and behaviour. And its a representative of class. The example of object

ex:man

state:tall,short, black skin,white skin, etc,.

behaviour:walk,run,jump etc,.

Class:

it gives an template or blueprint.and the class of no.of objects and methods.

It is a non primitive data type.

Method:

it is a Behaviour

Set of instructions with a name

Code Reusability

Member function

Smallest logical unit

void:

if we use void in method signature like

public static void main(String[] args)

it means nothing return.

Return:

if we use return we send the value where it was called.

Ex:

hsc.getmark()//method calling

private int getmark()// return type mention datatype

return // return the value

Pattern programs

Today we gonna see some pattern programs.

In this program we print three row and three column.

package Pattern;

public class Pattern {

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int no=1;no<=3;no++) {

for (int col=1;col<=3;col++) {

System.out.print(no);

}

System.out.println();

}

}

}

Result:

111

222

333

In this program we print letter A.

package Pattern;

public class PatternA {

public static void main(String[] args) {

// TODO Auto-generated method stub

for(int r=1;r<=5;r++) {

for(int c=1;c<=5;c++) {

if((r==1)&&((c==1)||(c==2)||(c==4)||(c==5)))

{

System.out.print(” “);

}

else if((r==2)&&((c==1)||(c==3)||(c==5)))

{

System.out.print(” “);

}

else if(((r==4)||(r==5))&&((c==2)||(c==3)||(c==4)))

{

System.out.print(” “);

}

else {

System.out.print(“*”);

}

}

System.out.println();

}

}

}

In this program we print D

package Pattern;

public class PatternD {

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int row=1;row<=5;row++) {

for(int col=1;col<=3;col++) {

if((col==2 )&&((row==2)||(row==3)||(row==4))) {

System.out.print(” “);

}

else if((col==3)&&((row==1)||(row==5))){

System.out.print(” “);

}

else {

System.out.print(“*”);

}

}

System.out.println();

}

}

}

Result:

In this program we print E

package Pattern;

public class PatternE {

public static void main(String[] args) {

// TODO Auto-generated method stub

for(int r=1;r<=5;r++) {

for(int c=1;c<=3;c++) {

if(((r==2)||(r==4))&&((c==2)||(c==3))) {

System.out.print(” “);

}

else {

System.out.print(“*”);

}

}

System.out.println();

}

}

}

In this program we print Q

package Pattern;

public class PatternQ {

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int r=1;r<=6;r++)

{

for(int c=1;c<=5;c++) {

if(((r==1)||(r==5))&&((c==1)||(c==5))) {

System.out.print(” “);

}

else if(((r==2)||(r==3))&&((c==2)||(c==3)||(c==4))) {

System.out.print(” “);

}

else if((r==4)&&((c==2)||(c==4))) {

System.out.print(” “);

}

else if((r==6)&&((c==1)||(c==2)||(c==3)||(c==4))){

System.out.print(” “);

}

else {

System.out.print(“*”);

}

}

System.out.println();

}

}

}

ARRAY PROGRAMS


Today we gonna see some array programs.
In below program we gonna see how many times a number present in the array.
package Array;

public class FindnoOfTimeaNumberPresent {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] a= {10,20,30,40,10};
    int no=10;
    int count=0;
    for(int i=0;i<a.length;i++) {
        if(no==a[i]) {
            count++;
        }

    }
    System.out.println(count);  
}

}

Result:
2

In this program we using linear search to find given number.
package Array;

public class LinearSearch {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int a[]= {1,2,3,4,5};
for(int i=0;i<a.length;i++) {
if(4==a[i]) {
System.out.println(” I GOT IT”);

}

}
}

}

Result:
I GOT IT

In this program we remove a specific number in array list.
package Array;

public class RemovingGivenNumberinArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] a= {10,20,30,40,50};
int no=30;
for(int i=0;i<a.length;i++) {
if(no!=a[i]) {
System.out.println(a[i]);
}
}

}

}

Result:
10
20
40
50

In this program we assign array length, while program running,with using Scanner class.

package selva.w4;

import java.util.Scanner;

public class Arrayusingforloop {

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter length");
    int len=sc.nextInt();
    int[] marks1= new int[len];

for(int i=0; i<len; i++)
{
System.out.println(“enter marks”);

marks1[i]=sc.nextInt();

}
}

}

Result:
enter length

3
enter marks
10
enter marks
15
enter marks
20

In this program we count how many times both numbers present in this array.
package selva.w4;

public class CountBothNoinArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] array= {10,20,10,30,40,10,25};
    int search1=10,count1=0,i=0,search2=20,count2=0;
    while(i<array.length)
    {
        if(array[i]==search1) {
    count1++;
        }
        else if(array[i]==search2)
        {
            count2++;
        }
        i++;

        }
        System.out.println("ANS:no of time present is "  + count1);
        System.out.println("ANS:no of time present is "  + count2);
    }
}

Result:
ANS:no of time present is 3
ANS:no of time present is 1

In this program we gonna find how many times number 5 present in array.
package selva.w4;

public class CountfivepresentinNumbers {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] a= {12,45,30,85,95,105};
int i=0;
int count=0;
int no2;
int no1;
while(i<a.length) {
no1=a[i]%10;no2=a[i]/10;
if(no1==5||no2==5) {
count++;
}
i++;
}

System.out.println(count);
}

}

Result:
4

In this program find number index of an number.
package selva.w4;

public class Findarray {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] array= {5,10,15,6,3};

        for(int i=0; i<array.length-1;i++)
            if(array[i]==15)
    {
        System.out.println("15 is present at "+i);
        break;
    }

}

}

Result:
15 is present at 2

In this program we gonna find first and second largest number in array.

package selva.w4;

public class FindFirstandSecondLargestValueInArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] no= {3,7,2,8,5};
    int max=Integer.MIN_VALUE;
            int max2=Integer.MIN_VALUE;
            int i=0;
    while(i<no.length) {
        if(no[i]>max) {
            max2=max;
            max=no[i];

        }
        else if(no[i]>max2) {
            max2=no[i];

    }
    i++;
}
    System.out.println("first largest "+max);

System.out.println(“second larges “+max2);
}
}

Result:
first largest 8
second larges 7

In this program we gonna find smallest number present in array.
package selva.w4;

public class FindMinValueinArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] no= {3,7,2,8,5};
    int min=Integer.MAX_VALUE,i=0;
    while(i<no.length) {
        if(min>no[i]) {
            min=no[i];

        }
        i++;
    }
    System.out.println("the smallest number is "+min);
}

}

Result:
the smallest number is 2

In this program we merge two array.
package selva.w4;

public class MergeTwoArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] a= {10,20,30};
int[] b= {5,15};
int[] result=new int[a.length+b.length];
int i=0;
while(i<a.length)
{
result[i]=a[i];
System.out.println(result[i]);
i++;

}
int j=i,k=0;
while(j<result.length)
{
result[j]=b[k];
System.out.println(result[j]);
k++;
j++;
}
}

}

Result:
10
20
30
5
15

In this program we shift towards array towards left.

package selva.w4;

public class ShiftTowardsLeftArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] a= {15,20,25,20,25};
int i=0;
int temp1=a[0];
int temp2=a[1];
while(i<3)
{

a[i]=a[i+2];
System.out.println(a[i]);
i++;

}
System.out.println(temp1);
System.out.println(temp2);
}
}

Result:
25
20
25
15
20

In this program we store and even index number.
package selva.w4;

public class StoreEvenIndexElementsInArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] arr= {10,20,30,40,50};
int[] result= new int[arr.length/2];
int i=0,j=0;
while(i<result.length) {
result[i]=arr[j];
System.out.println(result[i]);
i++;
j+=2;
}
}

}

Result:
10
30

package selva.w4;

public class SwapingArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

int[] arr1= {3,8,2,5,4};
int len=arr1.length;
int[] arr2=new int[len];//take the length of largest array
int i=0,j=arr1.length-1;
while(i<arr1.length)
{
arr2[i]=arr1[j];
System.out.println(arr2[i]);
i++;
j–;
}

}

}

Result:
4
5
2
8
3

ARRAY

Today we gonna see array.

What is array?

Collection of similar (datatype) elements stored continuously.

  • It is index based.
  • Index starts from 0(zero).
  • Index ends at length-1.
  • In java array is an object

Array declaration.

datatype[] referencename= new datatype[int] variable

array length is integer

ex:

array

int[] arr= new int[6]

A0A1A2A3A4A5

Ex:

char[] name=new char[10]











Boolean[] pass=new boolean[5]






String[] names=new string[20]





















In this area we see some compilation error and run time error

in compile time error not shown below

package selva.w4;

public class Array {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] i=new int[10];

System.out.println(i[14]);

<a href=”https://www.javatpoint.com/method-overriding-in-java”>Method Overriding</a>

}

}

shown run time error

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 14 out of bounds for length 10

at selva.w4.Array.main(Array.java:9)

Try this one

float[] marks=new float[-5]

package selva.w4;

public class Array {

public static void main(String[] args) {

// TODO Auto-generated method stub

float[] marks=new float[10];

System.out.println(marks[0]);

byte[] b=new byte[5];

System.out.println(b[0]);

short[] s=new short[10];

System.out.println(s[0]);

int[] i=new int[10];

System.out.println(i[-5]);

}

}

result

0.0

0

0

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index -5 out of bounds for length 10

at selva.w4.Array.main(Array.java:15)

example

array declaration type

package selva.w4;

public class marks {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] i=new int[10];

i[0]=90;

i[1]=89;

i[2]=97;

System.out.println(i[0]);

System.out.println(i[1]);

System.out.println(i[2]);

}

}

result

90

89

97

value insulation type

package selva.w4;

public class marks {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] i=new int[10];

int[] i1 = {90,89,97};

System.out.println(i1[0]);

System.out.println(i1[1]);

System.out.println(i1[2]);

}

}

result

90

89

97

assign size directly

package selva.w4;

public class Valueinsulationtype {

public static void main(String[] args) {

// TODO Auto-generated method stub

//int[] i=new int[10];

//int[] i1 = {90,89,97};

//System.out.println(i);//print the hash code for security purpose

//System.out.println(i1[0]);

//System.out.println(i1[1]);

//System.out.println(i1[2]);

int i=0;

int[] marks= {90,87,97};

while(i<marks.length) {

System.out.println(marks[i]);

i++;

}

}

}

90

87

97

note

In java array is a object(memory reference)

object has state and behaviour(action)

state – attributes

ex:

shirt.size

price

indian

action- methods

shirt.wear()

note

java has a some predefined packages

ex:

java.lang

provide string and systems

java.util

utility classes

java.net

it provide network related protocol

java.io

file reading

java.awt

web tool kit

java.javax

used for extension

java.lang there is no need to import. But other classes are need to import.

ARRAY USING FOR LOOP AND SCANNER CLASS

package selva.w4;

import java.util.Scanner;

public class Arrayusingforloop {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] marks1= new int[5];

for(int i=0; i<5; i++)

{

System.out.println(“enter marks”);

Scanner sc=new Scanner(System.in);

marks1[i]=sc.nextInt();

}

}

}

RESULT

enter marks

1

enter marks

2

enter marks

32

enter marks

4

enter marks

54

in this type we given length and input also using scanner class

note

scanner class imports given before the loop not inside the loop, while length and input given to program inside and outside the loop

package selva.w4;

import java.util.Scanner;

public class Arrayusingforloop {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println(“enter length”);

int len=sc.nextInt();

int[] marks1= new int[len];

for(int i=0; i<len; i++)

{

System.out.println(“enter marks”);

marks1[i]=sc.nextInt();

}

}

}

result

enter length

2

enter marks

1

enter marks

2

NUMBER PROBLEMS3

In this time, we see the programs like armstrong number,binary to decimal,decimal to binary,fibonacci series,greatest common divisor, least common divisor,neon number,perfect number,spy number, sum of digits.

ARMSTRONG NUMBEER

It is the number with sum of each digits multiplied itself three times and its equal to the given number.

Ex:

153

3–> 3*3*3=27

5–>5*5*5=125

1–>1*1*1=1

total=27+125+1=153

package mars1;

public class Armstrong {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=153,rem=0,sum=0,armstrong=0,no2=no;

while(no>0)

{

rem=no%10;

armstrong=armstrong+(rem*rem*rem);

no=no/10;

}

if(no2==armstrong)

{

System.out.println(“ARM”);

}

}

}

RESULT

ARM

BINARY TO DECIMAL

It converts given binary number to decimal.

Ex:1001

1–>1*2^0=1

0–>1*2^1=0

0–>1*2^2=0

1–>1*2^3=8

total=1+0+0+8=9

package mars1;

public class BinaryToDecimal {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1001,power=0,dec=0, rem=0;

while(no>0) {

rem=no%10;

dec=(int)(dec+(rem*Math.pow(2,power)));

no=no/10;

power++;

}

System.out.println(dec);

}

}

RESULT

9

DECIMAL TO BINARY

It converts decimal number into binary.

Ex:no=4

rem=no%2,no=no/2;

rem=4%2=0,no=4/2=2;

rem=2%2=0,no=2/2=1;

rem=1%2=1

package mars1;

public class DecimalToBinary {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=4;

String rem=””;

while(no>0)

{

rem=no%2+rem;

no=no/2;

}

System.out.println(rem+””);

}

}

RESULT

100

FIBONACCI SERIES

Fibonacci series moving their series from left to right by adding the first and second digit to form the third digits and neglect the first digit. The new first digit is the second one and second digit is the third one.

Ex:011

Here below F is first,S is second,T is third

F—S—T

0—1—1

1—2—3

2—3—5…,

package mars1;

public class FibonacciProblems {

public static void main(String[] args) {

// TODO Auto-generated method stub

int first=0,second=1,count=0,third=1;

while(true)

{

first=second;

second=third;

third=first+second;

count++;

if(third==233) {

System.out.println(“I got”);

break;

}

else if(third>223){

System.out.println(“not get”);

}

}

}

}

RESULT

I got

FIBONACCI SERIES USING FOR LOOP

Same concept used in above forloop used instead of while.

package mars1;

public class FibonacciSeries {

public static void main(String[] args) {

// TODO Auto-generated method stub

int first=0,second=1;

for(int i=2;i<=10;i++)

{

int third=first+second;

first=second;

second=third;

System.out.println(third);

}

}

}

RESULT

1

2

3

5

8

13

21

34

55

FIBONACCI SERIES WITHOUT USING THIRD VARIABLE

In this program we eliminate the third variable and use

first=second

second=first+second

package mars1;

public class FibonacciSerieswithoutThirdVariable {

public static void main(String[] args) {

// TODO Auto-generated method stub

int first=0,second=1;

for(int i=2;i<=10;i++)

{

first=second;

second=first+second;

System.out.println(second);

}

}

}

RESULT

2

4

8

16

32

64

128

256

512

GREATEST COMMON DIVISOR

In greatest common divisor

30–>15,10,6 ,5,3,2

18–>18,9,6 ,3,2

both have greatest common is 6.

package mars1;

public class GreatesCommonDivisor {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no1 = 30, no2 = 18;

int small = no1 < no2 ? no1 : no2;

int big = no1 > no2 ? no1 : no2;

while (small >= 2) {

if ((no1 % small == 0) && (no2 % small == 0)) {

System.out.println(“GCD IS ” + small);

break;

}

small–;

}

}

}

RESULT

GCD IS 6

LEAST COMMON MULTIPLE

example:

multiples of 2 and 3.

2–>2,4,6,8,12,14,16,18,20

3–>3,6,9,12,15,18,21

both have least common is 6.

package mars1;

public class LeastCommonMultiple {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no1 = 8, no2 = 3;

int small = no1 < no2 ? no1 : no2;

int big = no1 > no2 ? no1 : no2;

int bigcount=big;

while (true) {

if (big % small == 0) {

System.out.println(“LCM IS ” + big);

break;

}

big=big+bigcount;

}

}

}

RESULT

LCM IS 24

NEON NUMBER

The example of neon number given below.

9*9=81

8+1=9

the product of given number twice produce a number have its sum of digits equal to given number.

package mars1;

public class NeonNumber {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=9,neon=0,rem=0;

int no2=no*no;

while(no2>0) {

rem=no2%10;

neon=neon+rem;

no2=no2/10;

}

if(neon==no) {

System.out.println(“neon”);

}

else {

System.out.println(“not neon”);

}

}

}

RESULT

neon

PERFECT NUMBER

It is number of perfect divisor equal to the given number.

Ex:6

1,2,3,4,5,6 only 1,2,3 exactly divide number 6 so add the number of exact divisors 1+2+3=6.

package mars1;

public class PerfectNumber {

public static void main(String[] args) {

// TODO Auto-generated method stub

int sum=0;

int no=8;

for(int i=1;i<no;i++)

{

if (no%i==0) {

sum=sum+i;

}

}

if(sum==no) {

System.out.println(“PERFECT NUMBER”);

}

else {

System.out.println(“NOT PERFECT NUMBER”);

}

}

}

RESULT

NOT PERFECT NUMBER

SPY NUMBER

spy number means the sum of digits and product of digits of a give number to be equal.

package mars1;

public class SpyNo {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1234,sum=0,prod=1;

while(no>0)

{

int rem=no%10;

sum=sum+rem;

prod=prod*rem;

no=no/10;

}

System.out.println(“SUM ”+sum);

System.out.println(“PROD “+prod);

if(sum==prod) {

System.out.println(“spy number”);

}

else {

System.out.println(“not spy number”);

}

}

}

RESULT

SUM 10

PROD 24

not spy number

SUM OF DIGITS

In this program we need sum of digits in a single digit number so we use do while condition and the range is below 9 will give single digit. so, we choose it.

package mars1;

public class SumOfDigits {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=123,sum=0;

do {

while(no>0)

{

int rem=no%10;

sum=sum+rem;

no=no/10;

}

no=sum;

}while(sum>9);

System.out.println(sum);

}

}

RESULT

6

NUMBER PROBLEMS 2

Here we gonna see some simple interesting programs are modulus,multiple increment, narrow casting,number reverse, or operator, palindrome,qube,reverse order, smallest divisor,square root,sum of digits, swaping out,table 3 and their results.

MODULUS

package selva.day1;

public class Modulus {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1;

while(no<=50)

{

if(no%3==0) {

if(no%5==0)

{

System.out.println(no);

}

}

no++;

}

}

}

RESULT:

15

30

45

MULTIPLE INCREMENT

package selva.day1;

public class MultipleIncrement {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1;

while(no<=5) {

System.out.println(no*(no+1)*(no+2));

no++;

}

}

}

RESULT:

6

24

60

120

210

NARROW CASTING

package selva.day1;

public class NarrowCasting {

public static void main(String[] args) {

// TODO Auto-generated method stub

double myDouble=9.78;

int myint= (int) myDouble;//manual casting; double to int

System.out.println(myDouble);

System.out.println(myint);

}

}

RESULT

9.78

9

NUMBER REVERSE

package selva.day1;

public class NumberReverse {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no = 5;

while (true) {

System.out.println(no);// print the value infinitely if we didnt use no–

no–;

if (no == 0)

break;// we need to stop the count use break

}

}

}

RESULT

5

4

3

2

1

OR OPERATOR

package selva.day1;

public class OrOperator {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no;

for (no=1;no<=50;no++)

{

if(no%3==0||no%5==0)

{

System.out.println(no);

}

}

}

}

RESULT

3

5

6

9

10

12

15

18

20

21

24

25

27

30

33

35

36

39

40

42

45

48

50

PALLINDROME

package selva.day1;

public class Pallindrome {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no = 121;

int no1 = no;

int rev = 0;

int rem;

while (no > 0) {

rem = no % 10;

rev = (rev * 10) + rem;

no = no / 10;

}

System.out.println(rev);

if (no1 == rev) {

System.out.println(“PALINDROME”);

} else {

System.out.println(“NOT PALINDROME”);

}

}

}

RESULT

121

PALINDROME

QUBE

package selva.day1;

public class Qube {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no = 1, count;

while (no <= 4) {

count = no * no * no;

System.out.println(count);

no = no + 1;

}

}

}

RESULT

1

8

27

64

REVERSE ORDER

package selva.day1;

public class ReverseOrder {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1234;

int rev=0;

int rem;

while(no>0) {

rem=no%10;

rev=(rev*10)+rem;

no=no/10;

}

System.out.println(rev);

}

}

RESULT

4321

SMALLEST DIVISOR

package selva.day1;

public class SmallestDivisor {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=105;

int div=2;

while(div<=no)

{

if(no%div==0)

{

System.out.println(div);

break;

}

div++;

}

}

}

RESULT

3

SQUARE ROOT

package selva.day1;

public class Squareroot {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=81;

int div=2;

while(div<=no)

{

if(no/div==div) {

System.out.println(div+” is root of ” + no);

break;

}

div++;

}

}

}

RESULT

9 is root of 81

SUM OF DIGITS

package selva.day1;

public class SumOfDigits {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1234;

int rev=0,sumofdigits=0;

int rem;

while(no>0) {

rem=no%10;

sumofdigits=sumofdigits+rem;

no=no/10;

}

System.out.println(sumofdigits);

}

}

RESULT

10

SWAPINGOUT

package selva.day1;

public class SwapingOut {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a, b, c;

a = 5;

b = 10;

c = 15;

a = (a + b + c);

b = (a – (b + c));

a = (a – (b + c));

System.out.println(a);

System.out.println(b);

System.out.println(c);

}

}

RESULT

10

TABLE 3

package selva.day1;

public class Table3 {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no = 1,no1;

while (no <= 21) {

no1=no*3;

System.out.println(no + “*3=” + no1);

no++;

}

}

}

RESULT

1*3=3

2*3=6

3*3=9

4*3=12

5*3=15

6*3=18

7*3=21

8*3=24

9*3=27

10*3=30

11*3=33

12*3=36

13*3=39

14*3=42

15*3=45

16*3=48

17*3=51

18*3=54

19*3=57

20*3=60

21*3=63

TYPE CASTING

package selva.day1;

public class TypeCasting {

public static void main(String[] args) {

// TODO Auto-generated method stub

int myInt=8;

double myDouble=myInt;// auto convert double to int

System.out.println(myInt);//8

System.out.println(myDouble);//8.0

}

}

RESULT

8

8.0

Number problems

Today we gonna see some interesting number problems program like increament,number reverse, modulus etc.,

ForCondition

package excercise;

public class ForCondition {

public static void main(String[] args) {

// TODO Auto-generated method stub

for(;;) {

System.out.println(“hi”);

break;

}

}

}

RESULT:

hi

ForLoop

package excercise;

public class ForLoop {

public static void main(String[] args) {

int no=5;

for (no=5;no>=1; no–)

{

System.out.println(no);

//break;

}

}

}

RESULT:

5

4

3

2

1

LoopReductionMethod

package excercise;

public class LoopReductionMethod {

public static void main(String[] args) {

int no=1;

while (no<19)

{

System.out.println(no);

no+=2;

if(no==11)

{

no=2;

}

}

}

RESULT:

1

3

5

7

9

2

4

6

8

10

12

14

16

18

Modulus

package excercise;

public class Modulus {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1;

while(no<=50)

{

if(no%3==0) {

if(no%5==0)

{

System.out.println(no);

}

}

no++;

}

}

}

RESULT:

15

30

45

MultipleIncrement

package excercise;

public class MultipleIncrement {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1;

while(no<=5) {

System.out.println(no*(no+1)*(no+2));

no++;

}

}

}

RESULT:

6

24

60

120

210

NumberCondition

package excercise;

public class NumberCondition {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no1=1,no2=10;

while(no1<no2)

{

System.out.println(no1*no2);

no1++;

no2–;

}

}

}

RESULT:

10

18

24

28

30

NumberReverse

package excercise;

public class NumberReverse {

public static void main(String[] args) {

int no = 5;

while (true) {

System.out.println(no);// print the value infinitely if we didnt use no–

no–;// we need to stop the count use break

if (no == 0)

break;

}

}

}

RESULT:

5

4

3

2

1

OrOperator

package excercise;

public class OrOperator {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no;

for (no=1;no<=50;no++)

{

if(no%3==0||no%5==0)

{

System.out.println(no);

}

}

}

}

RESULT:

3

5

6

9

10

12

15

18

20

21

24

25

27

30

33

35

36

39

40

42

45

48

50

Qube

package selva.day1;

public class Qube {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no = 1, count;

while (no <= 4) {

count = no * no * no;

System.out.println(count);

no = no + 1;

}

}

}

RESULT:

1

8

27

64

SquareRoot

package selva.day1;

public class Squareroot {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=81;

int div=2;

while(div<=no)

{

if(no/div==div) {

System.out.println(div+” is root of ” + no);

break;

}

div++;

}

}

}

RESULT:

9 is root of 81

SwapingOut

package excercise;

public class SwapingOut {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a, b, c;

a = 5;

b = 10;

c = 15;

a = (a + b + c);

b = (a – (b + c));

a = (a – (b + c));

System.out.println(a);

System.out.println(b);

System.out.println(c);

}

}

RESULT:

10

5

15

Design a site like this with WordPress.com
Get started