Showing posts with label java program. Show all posts
Showing posts with label java program. Show all posts

Thursday, 17 August 2017

SPY NUMBER


SPYNUMBER:
A spy number is a number where the sum of its digits equals the product of its digits. For example, 1124 is a spy number, the sum of its digits is 1+1+2+4=8 and the product of its digits is 1*1*2*4=8.

/* java program to find given number is spynumber or not*/

import java.util.*;
public class SpyNumber
{
    public static void main(String args[])
    {
        int no, pro, sum, digit;
        pro = 1;
        sum = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number to check spy number :");
        no = sc.nextInt();
        while(no != 0)
        {
            digit = no % 10;
            pro = pro * digit;
            sum = sum + digit;
            no = no / 10;
        }
        if(sum == pro)
        {
            System.out.println("Spy Number");
        }
        else
        {
            System.out.println("Not Spy Number");
        }
    }
}





OUTPUT:
Enter a number to check spy number:
1124
Spy Number
Enter a number to check spy number:
1254
Not Spy Number

FASCINATING NUMBER


 FASCINATING NUMBER :
When a number( 3 digit or more) is multiplied by 2 and 3 ,and when both these products are concatenated with the original number all digits from 1 to 9 are present exactly once, regardless of the number of zeroes.


Example:Joining 192 with its products by 2 and 3, the resultant number is 192384576. This number contains all digits from 1 to 9 and these digits appear exactly once in the result. Hence 192 is a fascinating number. Note : The number should be of at least 3 digits to qualify for fascinating number test

/* program to find whether it is fascinating number or not*/

import java.util.*;
class FascinatingNumber
{
    boolean isUnique(String q)
    {
        int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit from '0' to '9'
        int i, flag = 0;
        char ch;
        for(i=0; i<q.length(); i++)
        {
            ch = q.charAt(i);
            A[ch-48]++;
            /*  increasing A[5] if ch='5' as '5'-48 = 53-48=5
             *  (ASCII values of '0' to '9' are 48 to 57) */
        }

        for(i=1; i<10; i++)
        {
            //checking if every digit from '1' to '9' are present exactly once or not
            if(A[i]!=1)
            {
                flag = 1; //flag is set to 1 if frequency is not 1
                break;
            }
        }

        if(flag == 1)
            return false;
        else
            return true;
    }
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        FascinatingNumber ob = new FascinatingNumber();

        System.out.print("Enter a number : ");
        int n = sc.nextInt();
        String p = Integer.toString(n); //converting the number to String

        if(p.length()<3)
            System.out.println("Number should be of atleast 3 digits.");

        else
        {
            String s = Integer.toString(n*1) + Integer.toString(n*2) + Integer.toString(n*3);
            /*  Joining the first, second and third multiple of the number
             *  by converting them to Strings and concatenating them*/
            if(ob.isUnique(s))
                System.out.println(n+" is a Fascinating Number.");
            else
                System.out.println(n+" is not a Fascinating Number.");
        }    
    }
}






OUTPUT:

Enter a number : 192

192 is a Fascinating Number.

Enter a number : 152
152 is not a Fascinating Number.

Tuesday, 8 August 2017

Creating OTP

import java.util.*;

public class OTP
{
    static char[] OTP(int len)
    {
        System.out.println("Generating OTP : ");
        System.out.print("You OTP is : ");

        // Using numeric values
        String numbers = "0123456789";

        // Using random method
        Random rndm_method = new Random();

        char[] otp = new char[len];

        for (int i = 0; i < len; i++)
        {
           
            otp[i] = numbers.charAt(rndm_method.nextInt(numbers.length()));
        }
        return otp;
    }
    public static void main(String[] args)
    {
        int length = 4;
        System.out.println(OTP(length));
    }
}





OUTPUT:


Generating OTP :

You OTP is : 2438

Generate Random Number using java program



import java.util.Random;

public class RandomNumber{

 public static void main(String args[])
 {
     // create instance of Random class
     Random rand = new Random();

     // Generate random integers in range 0 to 99999999
     int rand_int1 = rand.nextInt(99999999);
   
     // Print random integers
     System.out.println("Random Number: "+rand_int1);
   

 }
}




OUTPUT:

Random Number: 67779032

program for printing N odd numbers and printing sum of N odd numbers

import java.util.*;
public class SumOfOdd Numbers {

public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter number");
int n=s.nextInt();
System.out.println("The Odd Numbers are:");
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
System.out.println(i + " ");
}
}


int sum = 0;
for (int i = 1; i <= n; i++)
{
if (i % 2 != 0)
{
sum = sum + i;
}
}
System.out.println("sum of   " + n +"  odd numbers:" +sum);



}

}


OUTPUT:

enter number
10
The Odd Numbers are:
1
3
5
7
9
sum of   10  odd numbers:25

Program for sum of n natural numbers

import java.util.*;
public class SumOfNaturalNumbers{

public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter number to find sum of n natural numbers");
int sum=0;
int n=s.nextInt();
for(int i=0;i<=n;i++)
{
sum=sum+i;
}

System.out.println("here sum of  "+n+"  numbers");
System.out.println(sum);

}

}


OUTPUT:

Enter number to find sum of n natural numbers
10
here sum of  10  numbers
55

Thursday, 3 August 2017

Number reverse program using swings

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class StringReverse1 extends JFrame implements ActionListener
{
    JButton b1,b2;
    JTextField tx,tx1;
    JLabel l1,l2,l3;
    Container c;
    StringReverse1()
    {
        c=getContentPane();
        c.setBackground(Color.black);
        c.setLayout(null);

        l1=new JLabel("Enter a number");
        l1.setBounds(50,50,100,50);
        l1.setForeground(Color.white);
        c.add(l1);

        l3=new JLabel("after Reverse");
        l3.setBounds(125,0,225,50);
        l3.setForeground(Color.white);
        c.add(l3);

        tx=new JTextField();
        tx.setBounds(200,50,150,30);
        c.add(tx);

        l2=new JLabel("Answer");
        l2.setBounds(50,150,50,50);
        l2.setForeground(Color.white);
        c.add(l2);

        tx1=new JTextField();
        tx1.setBounds(200,150,150,30);
        c.add(tx1);

        b1=new JButton("Check");
        b1.addActionListener(this);
        b1.setBounds(100,200,90,50);
        c.add(b1);

        b2=new JButton("Cancel");
        b2.addActionListener(this);
        b2.setBounds(250,200,90,50);
        c.add(b2);

    }
    public void actionPerformed(ActionEvent ae)
    {
        String str=ae.getActionCommand();
        int no,rev=0,rem;
        if(str=="Check")
        {
            no=Integer.parseInt(tx.getText());
            while(no!=0)
            {
                rem=no%10;
                rev=rev*10+rem;
                no=no/10;
            }
            tx1.setText(String.valueOf(rev));
        }
        if(str=="Cancel")
        {
            System.exit(0);
        }
    }

}
class StringReverse
{
    public static void main(String args[])
    {
        StringReverse1 f=new StringReverse1();

        f.setTitle("Reverse");
        f.setSize(400,400);
        f.setVisible(true);
    }
}




OUTPUT:





Wednesday, 2 August 2017

Factorial program using swings

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Factorial extends JFrame {
JTextField t1, t2;

Factorial() {
JLabel l1 = new JLabel("Enter Number: ");
JLabel l2 = new JLabel("Factorial of Input Number: ");
t1 = new JTextField(20);
t2 = new JTextField(20);
JPanel p = new JPanel(new GridLayout(3, 2));
JButton b = new JButton("Find");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number = t1.getText();
int num = Integer.parseInt(number);
long fac = num;
for (int i = num; i > 1; i--) {
fac = fac * (i - 1);
}
t2.setText(Long.toString(fac));

}
});
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(b);
add(p);
setVisible(true);
pack();
}

public static void main(String[] args) {
Factorial f = new Factorial();
}
}


OUTPUT:


Tuesday, 25 July 2017

to print Current DATE and TIME


import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Time {
   public static void main(String[] args) {
     
       DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
       Date dateobj = new Date();
       System.out.println(df.format(dateobj));

     
       Calendar calobj = Calendar.getInstance();
       System.out.println(df.format(calobj.getTime()));
    }
}

OUTPUT:
25/07/17 12:01:29

25/07/17 12:01:29

Monday, 24 July 2017

multification Table program using java

import java.util.Scanner;
public class Table {
public static void main(String[] args) {

Scanner scan=new Scanner(System.in);
System.out.println("enter the number");
int n=scan.nextInt();
System.out.println("here your "+n+" table");
    int i;
    int k=0;
for ( i=1;i<=10;i++)
{
    k=n*i;
    System.out.println(n+"*"+i+"="+k);
}
}
}

OUTPUT:


enter the number
12
here your 12 table
12*1=12
12*2=24
12*3=36
12*4=48
12*5=60
12*6=72
12*7=84
12*8=96
12*9=108

12*10=120

Break Statement

public class Break{
public static void main(String[] args)
 {
            for(int i=0;i<=5;i++)
{  
                    for(int j=1;j<=6;j++)
{  
                        if(i==2&&j==3)
{  
                            break;  
                        }  
                        System.out.println(i+"   "+j);  
                    }  
            }  
}
}

OUTPUT:
0 1
0 2
0 3
0 4
0 5
0 6
1 1
1 2
1 3
1 4
1 5
1 6
2 1
2 2
3 1
3 2
3 3
3 4
3 5
3 6
4 1
4 2
4 3
4 4
4 5
4 6
5 1
5 2
5 3
5 4
5 5
5 6

while example in java

public class Whileexample {
public static void main(String[] args) {
    int i=0;
    while(i<=5){
        System.out.println(i);
    i++;
    }
}
}




OUTPUT:
0
1
2
3
4
5

Thursday, 20 July 2017

star pattern program for daimond shape

class Daimond
{
public static void main(String[] args) 
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}
}

To print numbers in plus(+) pattern

public class Pattern {
public static void main(String[] args) {
    for(int i=1;i<=5;i++){
        System.out.println("        "+i);
    }
    for(int i=1;i<=10;i++){
        System.out.print(i+" ");
    }System.out.println();
    for(int i=6;i<=10;i++){
        System.out.println("        "+i);
    }
}
}





OUTPUT:

           1
           2
           3
           4
1 2 3 4 5 6 7 8 9 10
           6
           7
           8
           9
          10

Wednesday, 19 July 2017

To print infinite stars using for loop


public class InfiniteStars {
public static void main(String[] args) {
    for(;;)
{
   
        System.out.println("*");
     }
}
}  

To print infinite numbers using for loop


public class Infinite {
public static void main(String[] args) {
    for(;;){
    for(int i=0;;i++)
    {
        System.out.println(i);
    }  }
}
}  

If-else Satatement

import java.util.*;
public class Ifelse
{
public static void main(String[] args)
 {
Scanner scan=new Scanner(System.in);
System.out.println("enter number to find even or odd");
    int number=scan.nextInt();
    if(number%2==0)
{
        System.out.println("even number");
    }
else
{
        System.out.println("odd number");
    }
}
}  

Factorial program

import java.util.*;
class Factorial{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
 int i,fact=1;
 System.out.println("enter number to find factorial");
 int number=scan.nextInt();
for(i=1;i<=number;i++){  
     fact=fact*i;  
 }  
 System.out.println("Factorial of "+number+" is: "+fact);  
}
}  

Polindrome number or not

import java.util.*;
class Palindrome
{
 public static void main(String args[])
{
Scanner s=new Scanner(System.in);
  int r,sum=0,temp;  
  System.out.println("Enter number to check polindrome or not");
  int n=s.nextInt();
 
  temp=n;  
  while(n>0)
{  
   r=n%10;  //getting remainder
   sum=(sum*10)+r;  
   n=n/10;  
  }  
  if(temp==sum)  
   System.out.println("palindrome number ");  
  else  
   System.out.println("not palindrome");  
}
}  

PRIME NUMBER

import java.util.*;
class Prime
{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
 int i,m=0,flag=0;  
 System.out.println("enter number");
 int n=s.nextInt();
 m=n/2;  
 for(i=2;i<=m;i++){  
  if(n%i==0){  
  System.out.println("Number is not prime");  
  flag=1;  
  break;  
  }  
 }  
 if(flag==0)  
 System.out.println("Number is prime");  
}
 }