Tuesday, January 17, 2017

Program to generate otp with char and numeric.

import java.util.*;
class Otp3
{
public static void main(String...args)
{
String str="";
int Snum,num,count=0;
Random rn=new Random();

      for(;;)
{
Snum=rn.nextInt(100);
if(Snum>=65 && Snum<=90)
{
str=str+(char)Snum;
count++;
}
if(count==4)
{
break;
}
}
for(;;)
{
num=rn.nextInt(10000);
if(num>1000)
{
break;
}
}
str=str+num;
System.out.println("Your Otp is : "+str);
}
}

                                                                                                                                       

Output:


Monday, January 16, 2017

Program to display time in 12 and 24 hours formats.

import java.util.*;
class Watch
{
int second,minute,hour;
void store()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the time in 24 format: ");
System.out.print("Hours : ");
do
{
hour=sc.nextInt();
if(hour > 23 || hour < 0)
{
System.out.println("Oop! Hours must be between 0 to 23");
System.out.print("Hours : ");
}
}while(hour > 23 || hour < 0);
System.out.print("Minutes : ");
do
{
minute=sc.nextInt();
if(minute > 59 || minute < 0)
{
System.out.println("Oop! Minutes must be between 0 to 59");
System.out.print("Minutes : ");
}
}while(minute > 59 || minute < 0);
System.out.print("Seconds : ");
do
{
second=sc.nextInt();
if(second >59 || second < 0)
{
System.out.println("Oop!Seconds must be between 0 to 59");
System.out.print("Seconds : ");
}
}while(second > 59 || second < 0);
}
void show_time12F()
{
if(hour > 12)
{
System.out.println(" 12 Format Time -> "+(hour-12)+":"+minute+":"+second+" PM");
}
else if (hour == 12)
{
System.out.println(" 12 Format Time -> "+hour+":"+minute+":"+second+" PM");
}
else
{
System.out.println(" 12 Format Time -> "+hour+":"+minute+":"+second+" AM");
}
}
void show_time24F()
{
System.out.println(" 24 Format Time -> "+hour+":"+minute+":"+second);
if(hour==0)
{
System.out.println("It's Mid Night!!!!!!\n");
}
else if(hour>1 && hour<11)
{
System.out.println("It's Morning!!!!!!\n");
}
else if(hour>11 && hour<12)
{
System.out.println("It's Morning!!!!!!\n");
}
else
{
System.out.println("It's Evening!!!!!!\n");
}
}
public static void main(String...args)
{
Watch w1=new Watch();
w1.store();
w1.show_time12F();
w1.show_time24F();
}
}

                                                                                                                          

Output:


Program to display student report card.

import java.util.*;
class Student
{
int rollno,physics,chem,math;
String name,email;
public static void main(String...args)
{
Student s1=new Student();
Scanner sc=new Scanner(System.in);
System.out.print("Roll no. : ");
s1.rollno=sc.nextInt();
System.out.print("Name : ");
s1.name=sc.next();
System.out.print("Email : ");
s1.email=sc.next();
System.out.println("Enter the Marks following...");
System.out.print("Physics : ");
s1.physics=sc.nextInt();
System.out.print("Chemistry : ");
s1.chem=sc.nextInt();
System.out.print("Math : ");
s1.math=sc.nextInt();

int total=s1.physics+s1.chem+s1.math;

int avg=(total*100)/300;

String grade;
if(avg<33)
{
grade="Fail";
}
else if ( avg >=34 && avg <= 44 )
{
grade="III";
}
else if ( avg >= 45 && avg <= 59 )
{
grade="II";
}
else if ( avg >=60 && avg <= 74 )
{
grade="I";
}
else
{
grade="Distinction";
}

//Report card..
System.out.println("\n***Report Card***");
System.out.println("Roll no. : "+s1.rollno);
System.out.println("Name : "+s1.name);
System.out.println("Email : "+s1.email);
System.out.println("Physics : "+s1.physics);
System.out.println("Chemistry: "+s1.chem);
System.out.println("Math : "+s1.math);
System.out.println("Total : "+total);
System.out.println("Average : "+avg);
System.out.println("Grade : "+grade);


}
}

                                                                                                                               

Output:


Friday, January 13, 2017

Program to display clock in console window

import java.util.*;
class SystemTime
{

public static void main(String...args) throws Exception
{
System.out.println("type ctrl and c to quit");
for(;;)
{
//Create the object.
Date d=new Date();
//Display the System time and date.
System.out.print(d);
// hold the next time.
Thread.sleep(500);
//To overide the previous time and move cursor to starting point.
System.out.print("\r");
Thread.sleep(500);
}
}
}

                                                                                                                                  

Output:


Program to generate floating point random number.

import java.util.*;

public class RandomDemo2 {
   public static void main( String...args )
   {
// create random object
Random randomno = new Random();

//Float value
System.out.println("Float value between 0.0 and 1.0: "+randomno.nextFloat());

//Double value
System.out.println("Double value between 0.0 and 1.0: "+randomno.nextDouble());

// check next Gaussian value
System.out.println("Next Gaussian value: " + randomno.nextGaussian());
   }    
}

                                                                                                                                          

Output:


Thursday, January 12, 2017

Program to generate 6 digits Random Number(Positive Only).

import java.util.*;
class RandomNum
{
public static void main(String...args)
{
//Random object created.
Random rn=new Random();

//Random method called for integer values with fixed value.
int num=rn.nextInt(1000000);

//show Random with 6 digits number.
if(num>100000)
{
System.out.println("Your Random No. is : "+num);
}
else
{
System.out.println("Retry!");
}

}
}

                                                                                                                                   

Output:


Program to generate random integer number(Positive or Negative).

import java.util.*;
class RandomDemo1
{
public static void main(String...args)
{
//Random object created.
Random rn=new Random();

//Random method called for integer values.
int num=rn.nextInt();

//show the Random number.
System.out.println("Random number is : "+num);
}
}
                                                                                                                                                 
Output: