Today firstly Teacher Discussed the programs that was given yesterday.Then after disscusion we were taught about the Control Statements in java such as if else, ladder if else, nested if else statements.A program consists of a number of statements which are usually executed in sequence. Programs can be much more powerful if we can control the order in which statements are run.We use Control statements to Do So.
we were given following programs to make ourselves:
Check Given number is postive or negative.
Check Given number is even or Odd.
Check Given number's reverse is Equal to Original Or Not.
Check Given number is Armstrong or not.
Check Profit or loss by input of Cost price and Sale price from User.
Check Date is Valid or Invalid.
Program to Check Given number is postive or negative.
import javax.swing.*;
public class positiveornot {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a;
a = Integer.parseInt(JOptionPane.showInputDialog("enter the no."));
if (a>=0)
{ System.out.print("the no is positive");
}
else
{ System.out.print("the no is negative");
}
}}
Program to Check Given number is Even or Odd.
import javax.swing.*;
public class even {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a;
a = Integer.parseInt(JOptionPane.showInputDialog("enter the no."));
if (a%2 ==0)
{ System.out.print("the no. is even");
}
else {System.out.print("the no. is odd");}
}
}
Program to Check reverse of number is equal to original or not.
import javax.swing.*;
public class samerev {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a,b,c,d;
a = Integer.parseInt(JOptionPane.showInputDialog("enter the no."));
c=a%10;
a=a/10;
b=a%10;
a=a/10;
d= (c*100)+(b*10)+(a);
if(a == d)
{
System.out.print("the reverse of the no. is same " );
}
else { System.out.print("the reverse is not same ");}
}
}
Program to Check Given Number is Armstrong Or Not.
import javax.swing.*;
public class amstrong {
public static void main(String[] args) {
int a,b,c,d,temp;
a = Integer.parseInt(JOptionPane.showInputDialog("enter the no."));
temp =a;
c=a%10;
a=a/10;
b=a%10;
a=a/10;
d = (a*a*a)+(b*b*b)+(c*c*c);
if(temp == d )
{
System.out.print("it is a armstrong");
}
else
{
System.out.print("it is not armstrong ");
}
}
}
Program to Check Profit or loss by input of Cost price and Sale price from User.
import javax.swing.*;
public class costprice {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int cp,sp,p;
cp = Integer.parseInt(JOptionPane.showInputDialog("enter the costprice"));
sp = Integer.parseInt(JOptionPane.showInputDialog("enter the selling price"));
if (cp < sp)
{
p = sp - cp;
System.out.print("the profit is "+p);
}
else if (cp > sp)
{
p = cp-sp;
System.out.print("the loss is "+p);
}
else
{
System.out.print("there is neither any profit nor loss");
}
}
}
Program to Check Date is VAlid Or Not.
import javax.swing.*;
public class validdate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int date,month;
month = Integer.parseInt(JOptionPane.showInputDialog("enter the month"));
date = Integer.parseInt(JOptionPane.showInputDialog("enter the month"));
if (month==1 || month == 3 || month== 5 ||month==7 ||month==8|| month==10 ||month==12)
{
if( date>0 && date<32)
{
System.out.print("It is a valid date");
}
else
{System.out.print("it is invalid date");
}
}
/** 30 days **/
else if (month==4 || month == 6 || month== 9 ||month==11)
{
if( date>0 && date <31)
{
System.out.print("It is a valid date");
}
else
{System.out.print("it is invalid date");
}
}
/** 28 days**/
else if (month==2 )
{
if( date>0 && date <29)
{
System.out.print("It is a valid date");
}
else
{System.out.print("it is invalid date");
}
}
else
{
System.out.print("invalid date");
}
}
}