Search This Blog

UI/UX Design | Web Development | Mobile Application Development

Whether you need UI/ UX design, web development services, or mobile app development services, Qaabil Digitals will be more than happy to help you. We work with top software developers, designers, and project managers. We tailor our services to suit your exact needs. We closely look at your company, listen to your story, and bring about the best ideas and solutions that can help you drive solid results. We don’t just produce work that works. We produce work that matters. Get a quote now Or Talk to us

Monday 14 November 2011

Java program that calculates table of any given number and displays in ascending or descending order (by Sara)

import java.util.*;

class table
{
public static void main (String argsp[])
{
Scanner in = new Scanner (System.in);

int a,b,c,d=0;
System.out.print ("\n\t Enter the number to print table = ");
a=in.nextInt();
System.out.print ("\n\t Enter 1 for ascending and 2 for descending order = ");
c=in.nextInt();

if (c==1)
{
System.out.println ("\n\t Table of "+a+" in ascending order ");
b=1;
while (b<=10)
{
d=b*a;
System.out.println ("\n\t "+a+" * "+b+" = "+d);
++b;
}
}
else if (c==2)
{
System.out.println ("\n\t Table of "+a+" in ascending order ");
b=10;
while (b>0)
{
d=b*a;
System.out.println ("\n\t "+a+" * "+b+" = "+d);
--b;
}
}
else
System.out.println ("\n\t Invalid number ");

}
}

Saturday 5 November 2011

Develop a java program to generate the Electricity bill. The program have three member functions, Getdata(): to accept the block number, flat name, name and number of units, Calc(): calculate the total cost, Display(): display the bill in proper format ( name, units , total charges). If the total is greater than RM 150 then additional surcharge 15% is added (by Fikri Fieda)


import java.util.*;
class ebill
{
public static void main (String args[])
{
customerdata ob = new customerdata();
ob.Getdata();
ob.Calc();
ob.Display();

}
}

class customerdata
{
Scanner in = new Scanner (System.in);
Scanner ins = new Scanner (System.in);
String cname,fname;
int bn;
double units,tbill;

void Getdata()
{
System.out.print ("\n\t Enter block number = ");
bn = in.nextInt();
System.out.print ("\n\t Enter flate name = ");
fname = ins.nextLine();
System.out.print ("\n\t Enter customer name = ");
cname = ins.nextLine();
System.out.print ("\n\t Enter total consumed units = ");
units = in.nextDouble();
}

void Calc()
{
if (units<100)
tbill=0.40*units;
else if (units>100 && units<300)
tbill=0.50*units;
else
tbill=0.60*units;
if (tbill>150)
tbill+=tbill*0.15;
}

void Display()
{
System.out.println ("\n\t Customer name = "+cname);
System.out.println ("\n\t Total units = "+units);
System.out.println ("\n\t Total bill = RM "+tbill);
}
}

Write a class name employee. This class should hold the employee's details such as employee id,name,gender,salary and position. Write a member function to input all these details and displaying the information on the screen. Supply another member function to calculate the net salary for an employee (by Syuhadah razak)


import java.util.*;
class employee
{
public static void main (String args[])
{
employeeinfo ob = new employeeinfo();
ob.get();
ob.totalsalary();
ob.display();

}
}

class employeeinfo
{
Scanner in = new Scanner (System.in);
Scanner ins = new Scanner (System.in);
String name,gender,position;
int id,salary,ma,hr,bn;

void get()
{
System.out.print ("\n\t Enter employee ID = ");
id = in.nextInt();
System.out.print ("\n\t Enter emplyee name = ");
name = ins.nextLine();
System.out.print ("\n\t Enter employee gender = ");
gender = ins.nextLine();
System.out.print ("\n\t Enter employee salary = ");
salary = in.nextInt();
System.out.print ("\n\t Enter employee position = ");
position = ins.nextLine();
}

void totalsalary()
{
System.out.print ("\n\t Enter medical allowance = ");
ma = in.nextInt();
System.out.print ("\n\t Enter house rent = ");
hr = in.nextInt();
System.out.print ("\n\t Enter bounus = ");
bn = in.nextInt();
}

void display()
{
System.out.println ("\n\t Employee ID = "+id);
System.out.println ("\n\t Employee name = "+name);
System.out.println ("\n\t Employee gender = "+gender);
System.out.println ("\n\t Employee basic salary = "+salary);
System.out.println ("\n\t Employee position/post = "+position);
System.out.println ("\n\t Total salary = "+(ma+hr+bn+salary));
}

}



Write a program that store (a, e, i, o and u) in string data type. Then ask the user to enter a string of characters. then the program should count the number of vowels in that string (by Tieqa)


import java.util.*;
class charvowels
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
char vowels[] = new char[10];
String str;
char c1,c2;
int t=0;

vowels[0] = 'A';
vowels[1] = 'a';
vowels[2] = 'E';
vowels[3] = 'e';
vowels[4] = 'I';
vowels[5] = 'i';
vowels[6] = 'O';
vowels[7] = 'o';
vowels[8] = 'U';
vowels[9] = 'u';

System.out.print ("\n\t Enter the string = ");
str=in.nextLine();

System.out.print ("\n\n\t Vowels in the string = ");
for (int i=0;i<str.length();++i)
{
c1=str.charAt(i);
for (int j=0;j<10;++j)
{
if (c1==vowels[j])
{
System.out.print (" "+c1);
++t;
break;
}
}
}

System.out.println ("\n\t Total number of vowels = "+t);
}
}

Java program to create an ARRAY of 5 STRINGS. The program finds and displays the total number of vowels (both uppercase and lowercase) in all five strings by (Adler Lawn Service, LLC)


import java.util.*;
class vowels
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
String n="",n1,n2,n3,n4,n5;
char c;
int r=1;

System.out.print ("\n\t Enter first name of first person = ");
n1 = in.nextLine();
System.out.print ("\n\t Enter first name of second person = ");
n2 = in.nextLine();
System.out.print ("\n\t Enter first name of third person = ");
n3 = in.nextLine();
System.out.print ("\n\t Enter first name of fourth person = ");
n4 = in.nextLine();
System.out.print ("\n\t Enter first name of fifth person = ");
n5 = in.nextLine();

while (r<6)
{
if (r==1)
{
n=n1;
System.out.print ("\n\n\t Vowels in first person's name = ");
}
else if (r==2)
{
n=n2;
System.out.print ("\n\n\t Vowels in second person's name = ");
}
else if (r==3)
{
n=n3;
System.out.print ("\n\n\t Vowels in third person's name = ");
}
else if (r==4)
{
n=n4;
System.out.print ("\n\n\t Vowels in fourth person's name = ");
}
else if (r==5)
{
n=n5;
System.out.print ("\n\n\t Vowels in fifth person's name = ");
}
else
System.out.println ();

for (int i=0;i<n.length();++i)
{
c=n.charAt(i);
switch (c)
{
case 'A':
case 'a':
System.out.print (" "+c);
break;
case 'E':
case 'e':
System.out.print (" "+c);
break;
case 'I':
case 'i':
System.out.print (" "+c);
break;
case 'O':
case 'o':
System.out.print (" "+c);
break;
case 'U':
case 'u':
System.out.print (" "+c);
break;
default:
System.out.print ("");
}
}

++r;
}

}
}

Wednesday 26 October 2011

A video rental shop maintains the inventory of videotapes that are being sold in the shop. Whenever a customer wants to rent a videotape, the sales person can check through the list to find out the availability of that particular videotape. If it is not available, then certain message will be displayed. If it is available, then the system displays the details. If the customer confirms his/her choice, the system displays the price and the due date for returning the videotape by (noor diana fauzi)

import java.util.*;
class moviesforrent
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
String mname;
System.out.print ("\n\t Enter the movie name = ");
mname=in.nextLine();
movies ob = new movies();
ob.pricendetail(mname);
}
}

class movies
{
String ma[] = new String [4];
String mc[] = new String [4];
//price per week=RM10.
movies()
{
ma[0]="fast&furious";
ma[1]="pearl harbor";
ma[2]="death note";
ma[3]="ratatouille";

mc[0]="Josh Hartnett";
mc[1]="Ben Affleck";
mc[2]="Michael Bays";
mc[3]="Jerry Bruckheimer";
}

void pricendetail(String mname)
{
int i=0,j=0;
while (i<4)
{
if (mname.equals(ma[i]))
{
System.out.println ("\n\t Movie: "+mname+" \n\t Main character: "+mc[i]+"\n\t Price per week = RM 10.00");
j=1;
break;
}
++i;
}
if (j==0)
System.out.println ("\n\t "+mname+" is not available.");
}

}

Tuesday 25 October 2011

Java program that joins 3 strings to show the quote, the person who said that and dates he/she lived by(Adler Lawn Service, LLC)

import java.util.*;

class joinstring
{
public static void main (String args[])
{
Scanner in= new Scanner (System.in);
String quote,name,date;

System.out.print ("\n\t Enter the quote = ");
quote=in.nextLine();
System.out.print ("\n\t Enter name of the person who said this = ");
name=in.nextLine();
System.out.print ("\n\t Enter the dates that person lived \n\t (i.e. 14 September 1925 - 25 January 1995 ) = ");
date=in.nextLine();

System.out.println ("\n\n\n\t ''"+quote+"''\n\t "+name+"\n\t ("+date+")");

}
}

Wednesday 19 October 2011

Java program to find sum, average and modulus by 3, of three numbers by(aol.com)

import java.util.*;
class average
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
double a,b,c,sum=0;
double mod,avg;
System.out.print ("\n\t Enter first number = ");
a=in.nextDouble();
System.out.print ("\n\t Enter second number = ");
b=in.nextDouble();
System.out.print ("\n\t Enter third number = ");
c=in.nextDouble();
sum=a+b+c;
avg=sum/3;
mod=sum%3;




System.out.println ("\n\t Sum of the numbers = "+sum);
System.out.println ("\n\t Average of the numbers = "+avg);
System.out.println ("\n\t Modulus of the sum of numbers by 3 = "+mod);
}
}

Java program which calculates next number by adding all previous numbers using exception handling (by Adler Lawn Service, LLC)

import java.util.*;

class EverySum
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
int a,b=0;
System.out.println ("\n\t The number must be 50 or less");
System.out.print ("\n\t Enter the nth number = ");
a=in.nextInt();
try
{
if (a>50)
check.test();
else
{
for (int i=1;i<a;++i)
{
b+=i;
System.out.print ("\t "+b);
}
}

}
catch (IndexOutOfBoundsException e)
{
System.out.println ("\n\t Invalid number: "+e);
}


}
}

class check
{
static void test() throws IndexOutOfBoundsException
{
throw new IndexOutOfBoundsException (">50");
}
}

Java program to display ifno of different cars using abstract class (by noor diana fauzi)

import java.util.*;
class CarPrice
{
public static void main (String args[])
{
Ford fr = new Ford();
fr.getcarmaker();
fr.setPrice();
Chevy ch = new Chevy();
ch.getcarmaker();
ch.setPrice();
fr.displyinfo();
ch.displyinfo();
}
}

abstract class Auto
{
abstract void getcarmaker();
abstract void setPrice();
}

class Ford extends Auto
{
Scanner in = new Scanner (System.in);
double price;
String maker;
void getcarmaker()
{
System.out.print ("\n\t Enter the name of car maker = ");
maker=in.nextLine();
}

void setPrice()
{
System.out.print ("\n\t Enter price of "+maker+" car = $");
price=in.nextDouble();
}

void displayinfo()
{
System.out.println ("\n\t Car maker = "+maker);
System.out.println ("\n\t Car Price = $"+price);
}
}

class Chevy extends Auto
{
Scanner in = new Scanner (System.in);
double price;
String maker;
void getcarmaker()
{
System.out.print ("\n\t Enter the name of car maker = ");
maker=in.nextLine();
}

void setPrice()
{
System.out.print ("\n\t Enter price of "+maker+" car = $");
price=in.nextDouble();
}

void displayinfo()
{
System.out.println ("\n\t Car maker = "+maker);
System.out.println ("\n\t Car Price = $"+price);
}
}

Java program to find area of a square and rectangle using abstract class (by noor diana fauzi)

import java.util.*;
class area
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
double base,height;
System.out.print ("\n\t Enter base of the Rectangle = ");
base=in.nextDouble();
System.out.print ("\n\t Enter height of the Rectangle = ");
height=in.nextDouble();

double side1,side2;
System.out.print ("\n\t Enter length of side1 of the Square = ");
side1=in.nextDouble();
System.out.print ("\n\t Enter length of side2 of the Square = ");
side2=in.nextDouble();

Rectangle ob1 = new Rectangle (base,height);
Square ob2 = new Square (side1,side2);
ob1.area();
ob2.area();
}
}


abstract class Shape
{
abstract void area();
}

class Rectangle extends Shape
{
double base,height,a;
Rectangle(double b, double l)
{
base=b;
height=l;
}
void area()
{
a=base*height;
System.out.println ("\n\t Area of Rectangle = "+a);
}
}

class Square extends Shape
{
double side1,side2,a;
Square (double s1, double s2)
{
side1=s1;
side2=s2;
}
void area()
{
a=side1*side2;
System.out.println ("\n\t Area of Square = "+a);
}
}

Sunday 2 October 2011

Java program to calculate commission on sales using methods (by Adler Lawn Service,LLC)

 import java.util.*;
class commission2
{
public static void main (String args[])
{
Scanner in =new Scanner (System.in);
double sales_figure,commission_rate_d;
int commission_rate_i;
System.out.print ("\n\t Enter sales figure = ");
sales_figure=in.nextDouble();
System.out.print ("\n\t Enter first commission rate = ");
commission_rate_d=in.nextDouble();
System.out.print ("\n\t Enter second commission rate = ");
commission_rate_i=in.nextInt();
comm2 ob2 = new comm2();
totalcomm ob = new totalcomm();
ob.computeCommission(sales_figure,commission_rate_d);
ob.computecommission(sales_figure,commission_rate_i);
ob2.computeCommission(sales_figure);
}
}

class comm2 extends totalcomm
{
void computeCommission (double sales)
{
double cr;
cr=(7.5/100)*sales;
System.out.println ("\n\t Commission at 7.5% = "+cr);
}
}

class totalcomm
{
double sales_fig,comm_rate;
int com_rate;
void computeCommission(double p1, double p2)
{
sales_fig=p1;
comm_rate=p2;
System.out.println ("\n\t Sales commission = "+(sales_fig*comm_rate));
}
void computecommission(double p1, int p2)
{
sales_fig=p1;
com_rate=p2/100;
System.out.println ("\n\t Commission = "+(sales_fig*com_rate));
}
}

Monday 26 September 2011

Java program to find even and odd numbers from 20 numbers (by noor diana)

import java.util.*;
class evenodd
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
int a[]= new int[20];
System.out.println ("\n\t You must enter 20 numbers");
for (int i=0;i<20;++i)
{
System.out.print ("\n\t Enter the number = ");
a[i]=in.nextInt();
}

System.out.println ("\n\t Even numbers : ");
for (int i=0;i<20;++i)
{
if (a[i]%2==0)
System.out.println ("\t"+a[i]);
}

System.out.println ("\n\t Odd numbers : ");
for (int i=0;i<20;++i)
{
if (a[i]%2!=0)
System.out.println ("\t"+a[i]);
}

}
}

Thursday 22 September 2011

Java program to calculate total cost of an item using methods (OOP)

import java.util.*;
class invoice
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
String name;
itemdata ob = new itemdata();
ob.get();
System.out.print ("\n\t Enter item name = ");
name=in.nextLine();
ob.cost();
ob.displyLine(name);

}
}


class itemdata
{
Scanner in = new Scanner (System.in);
int number;
double quantity,price,tcost=0;
void get()
{
System.out.print ("\n\t Enter item number = ");
number=in.nextInt();
}

void cost()
{
System.out.print ("\n\t Enter item quantity = ");
quantity=in.nextDouble();
System.out.print ("\n\t Enter item price per unit = ");
price=in.nextDouble();
tcost=quantity*price;
}

void displyLine(String name)
{
System.out.println ("\n\t Item number = "+number);
System.out.println ("\n\t Item nume = "+name);
System.out.println ("\n\t Item quantity = "+quantity);
System.out.println ("\n\t Item price per unit = "+price);
System.out.println ("\n\t Total cost = "+tcost);
}

}

Wednesday 21 September 2011

Java program to calculate salary of an employe using methods

import java.util.*;
class salary
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
double hpr,rh,oh;
System.out.print ("\n\t Enter hourly pay rate = ");
hpr=in.nextDouble();
System.out.print ("\n\t Enter regular hours = ");
rh=in.nextDouble();
System.out.print ("\n\t Enter overtime hours = ");
oh=in.nextDouble();

double rhp=0,otp,tp=0;
rhp = hpr*rh;                            //calculating regular hours pay: rhp

calculate ob = new calculate();            //creating object of class calculate
otp=ob.overtimepay(hpr,oh);                   //calling the method overtimepay
tp=rhp+otp;                                //calculating total pay

System.out.println ("\n\t Regular hours pay = "+rhp);
System.out.println ("\n\t Overtime pay = "+otp);
System.out.println ("\n\t Total pay = "+tp);
}
}


class calculate
{
double otp;
double overtimepay (double hpr, double oh)        //recieving hourly pay rate and overtime hours hpr, oh respectively
{
otp=(hpr*0.5)*oh;                                //calculating overtime pay
return otp;
}
}

Thursday 1 September 2011

Java program to find average and add two numbers taken from user

Finding addition and average of two numbers is also very simple in java. As you will see it is little bit different from another program which finds sum and product of two numbers. There is its code:

import java.util.*;
class sumavg
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
int a,b,s,avg;
System.out.print ("\n\t Enter first number = ");
a=in.nextInt();
System.out.print ("\n\t Enter second number = ");
b=in.nextInt();
s=a+b;
avg=s/2;
System.out.print ("\n\t sum = "+s);
System.out.print ("\n\t avg = "+avg);
}
}

Note:
          Look at this "avg=s/2" statement. We have sum of the numbers in s and that's why we are dividing s by 2 to calculate average of these numbers. You can also do that in a different way like:
avg=(a+b)/2;

Java program to find sum and product of two numbers

It is very easy to find sum and product of two numbers in java. First of all we must take two numbers from user and then we will add and multiply these numbers. Lets see how, it is

import java.util.*;
class sumproduct
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
int a,b,s,p;
System.out.print ("\n\t Enter first number = ");
a=in.nextInt();
System.out.print ("\n\t Enter second number = ");
b=in.nextInt();
s=a+b;
p=a*b;
System.out.print ("\n\t sum = "+s);
System.out.print ("\n\t product = "+p);
}
}

Note:
          After we have the numbers, take two variables for sum and product. As we have s and p in this program.

Tuesday 9 August 2011

The for loop

For loop:
                For loop is a powerful and versatile construct. Its general form is:
for (initialization;condition;iteration)
{
//body
}
If you have only one statement to repeat then you can do this without curly braces. The for loop operates as follows:
  • When the loop starts first time, the initialization portion of the loop is executed. Generally, this is an expression that sets the value of the loop control variable, which acts as a counter that controls the loop. It is important to understand that the initialization expression is only executed once.
  • Next, condition is evaluated. This must be a Boolean expression. If this expression is true then the body of the loop is executed. If it is false the loop terminates.
  • Next, the iteration portion of the loop is executed. This is usually an expression that increments or decrements the loop control variables.
This process will continue till condition is true. See a simple java program explaining all this.

Monday 8 August 2011

A simple java program using for loop

class simplefor
{
public static void main (String arg[])
{
int a;
for (a=5;a>=0;--a)
{
System.out.print ("\n\t a = "+a);
}

}
}

Note:
        In this program, body of the loop will be executed till a is greater than or equals to 0 and value of a will be printed.

Saturday 16 July 2011

The do while loop

Do while:
                As you know if the conditional expression controlling a while loop initially false then the body of the loop will not be executed at all. However, sometimes it is desirable to execute the body of a while loop at least once, even if the conditional expression is false at the beginning. We can say that, there are times while programming with java when you would like to test the conditional expression at the end of the body of loop rather than at the beginning. Fortunately, java supplies a loop that does just that: the do while.

The do while loop always executes its body first (even if the conditional expression is false to begin with) and then checks the conditional expression. It does so because its conditional expression is at the bottom/end of the body of the loop. Its general form is:

do
{
//body of loop
}
while (condition);

Each iteration of this loop first executes body of the loop and then evaluates the conditional expression, controlling the loop. If this condition is true then loop will repeat otherwise, the loop terminates. Remember, a condition must be a Boolean expression.

Friday 15 July 2011

Write a program that read a number and check wheather it is prime or not using do while loop

import java.util.*;
class prime
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b=2,c=1;
System.out.print ("\n\t Enter number = ");
a=in.nextInt();

do
{
if (a%b==0)
{
c=0;
break;
}
++b;
}while (b>a/2);

if (c==1)
System.out.println ("\n\t Prime ");
else
System.out.println ("\n\t Not prime ");

}
}

Note:
          This program is working just like we have seen a program like this using while loop.

Write a program that prints all numbers from 1 to 100 which are divisible by 9 using do while loop

import java.util.*;
class numbers
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a=100;
do
{
if (a%9==0)
System.out.print ("\n\t "+a);
--a;
}while (a>0);

}
}

Note:
        This program is approximately same as that prints even or odd numbers.

Thursday 7 July 2011

Write a program to print factorial of any number using do while loop

import java.util.*;
class factorial
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);

int a,b=1;
System.out.print ("\n\t Enter the number = ");
a=in.nextInt();

do
{
b*=a;
--a;
}while (a>0);

System.out.println ("\n\t Factorial = "+b);

}
}

Note:
        In this program first of all body of the loop will be executed then its condition will be checked if it is true then body of the loop will be executed again otherwise not. This will continue until a=0. When a=0 the loop will be broken and the next statement to it will be executed.

Write a program that print all odd numbers from 0 to a user specific number using do while loop

import java.util.*;
class odd
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);

int a;
System.out.print ("\n\t Enter the ending number = ");
a=in.nextInt();

do
{
if (a%2!=0)
System.out.print ("\n\t "+a);
--a;
}while (a>0);

}
}

Note:
     In this program if we divide a by 2 and it do not returns 0, then it will be printed on
screen otherwise not.

Write a program to calculate sum and product of even numbers using do while loop

import java.util.*;
class sumeven
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,s=0,p=1;
System.out.print ("\n\t Enter the ending number = ");
a=in.nextInt();

do
{
if (a%2==0)
{
s+=a;
p*=a;
}
--a;
}while (a>0)

System.out.println ("\n\t Sum of even numbers = "+s);
System.out.println ("\n\t Product of even numbers = "+p);
}
}

Note:
     In this program we have used two arithmetic operators in the body of if statement. First
is adding, whatever the value of a, in s and second is multiplying all values of a.

Write a program that print all even numbers from 0 to a given range using do while loop

import java.util.*;
class even
{
public static void main (String arg[])
{
int a;
System.out.print ("\n\t Enter the number = ");
a=in.nextInt();

do
{
if (a%2==0)
System.out.print ("\n\t "+a);
--a;
}while (a>0);

}
}

Note:
     In this program, body of the loop will be executed till a is greater than 0. As you have
noticed a conditional statement in the body of the loop, this is the actual code in our program
which is checking that either the number is even or odd.

Tuesday 5 July 2011

Write a program that read a number and check wheather it is prime or not


import java.util.*;
class prime
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);

int a,b=2,c=1;
System.out.print ("\n\t Enter number = ");
a=in.nextInt();

do
{
if (a%b==0)
{
c=0;

}
++b;
}while (b<a/2);

if (c==1)
System.out.println ("\n\t Prime ");
else
System.out.println ("\n\t Not prime ");

}
}


Note:
          In this program, if a%b is equals to 0 it means there is a number which divides a properly, then c will assigned 0. After this process finishes, if condition will be evaluated and the result will be displayed according to the conditional expression.

Write a program that prints all numbers from 1 to 100 which are divisible by 9 using do while loop


class numbers
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a=100;

do
{if (a%9==0)
System.out.print ("\n\t "+a);
--a;
}
while (a>0);

}
}

Note:
         This program is approximately same as that prints even or odd numbers.

Write a program to print factorial of any number


import java.util.*;
class factorial
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);

int a,b=1;
System.out.print ("\n\t Enter the number = ");
a=in.nextInt();

while (a>0)
{
b*=a;
--a;
}

System.out.println ("\n\t Factorial = "+b);

}
}

Note:
     First of all computer will check the condition of while loop. If it is true then it will
 multiply b by a. After that it will decrease the value of a by 1 and repeat the same task. This
 will continue until a=0. When a=0 the loop will be broken and the next statement to it will
be executed.

Write a program that print all odd numbers from 0 to a user specific number using do while loop


import java.util.*;
class odd
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a;
System.out.print ("\n\t Enter the ending number = ");
a=in.nextInt();
 
do
{
if (a%2!=0)
System.out.print ("\n\t "+a);
--a;
}while (a>0);

}
}

Note:
        In this program if we divide a by 2 and it do not returns 0, then it will be printed on
screen otherwise not.

Write a program to calculate sum and product of even numbers


import java.util.*;
class sumeven
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,s=0,p=1;
System.out.print ("\n\t Enter the ending number = ");
a=in.nextInt();

while (a>0)
{
if (a%2==0)
{
s+=a;
p*=a;
}
--a;
}

System.out.println ("\n\t Sum of even numbers = "+s);
System.out.println ("\n\t Product of even numbers = "+p);
}
}

Note:
         In this program we have used two arithmetic operators in the body of if statement. First
is adding, whatever the value of a, in s and second is multiplying all values of a.

Write a program that print all even numbers from 0 to a given range


import java.util.*;
class even
{
public static void main (String arg[])
{
int a;
System.out.print ("\n\t Enter the number = ");
a=in.nextInt();

while (a>0)
{
if (a%2==0)
System.out.print ("\n\t "+a);
--a;
}

}
}

Note:
         In this program, body of the loop will be executed till a is greater than 0. As you have
noticed a conditional statement in the body of the loop, this is the actual code in our program
which is checking that either the number is even or odd.

Monday 27 June 2011

Java program using while loop


import java.util.*;
class loop
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a=10;
while (a<=20)
{
System.out.print ("\n\t a = "+a);
++a;
}

}
}

Remember, while a is less than or equal to 20 body of the loop will be executed. Once a equals to 21, loop will be broken and control goes to the next statement after that loop.

While loop in java


While loop:
                    The while loop is java's most fundamental looping statement. It is very easy and powerful loop of java.It repeats a statement or block while its Boolean expression is true. This Boolean expression controls the loop.

Syntax:
                 while (condition)
                  {
                    //body of the loop
                   }

The body of the loop will be executed till the conditional expression is true. When conditional expression comes false the control passes to the next statement right after the body of the loop. Always remember, in while loop first of all conditional expression is evaluated if it is true then body of the loop will be executed. If it becomes false at the beginning then body of the loop will not be executed, even once.

Curly braces are not important if you have only one statement to repeat but using curly braces is a good habit. Another point you should know that the body of while loop can be empty.

Sunday 26 June 2011

Nested switch statement in java


Nested switch:
                         We can use a switch as part of the statement sequence of an outer switch. This is called nested switch. We know that each switch defines its own block of code but case constants of outer switch have no conflicts with the inner switch's case constants.

General syntax:
                           switch (value)
                           {
                            case 1:
                               switch (value)
                               {
                                case 1:
                                statement(s)
                                break;
                                .
                                .
                                case n:
                                statement (n)
                                break;
                                default:
                                statement
                                }
                          case 2:
                          statement(s)
                          break;
                          .
                          .
                          case n:
                          statement (n)
                          break;
                          default:
                          statement
                         }    
Here, value can be a variable or any constant value. Remember case 1 of outer switch have no conflicts with case 1 of inner switch. If the value matches with case 1 (outer switch's) then inner switch will be executed otherwise it will be bypassed.


Java program using nested switch


import java.util.*;
class nswitch
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b;
a=3;
b=2;
switch (a)
{
case 1:
switch (b)
{
case 1:
System.out.println ("\n\t Inner One ");
break;
case 2:
System.out.println ("\n\t Inner Two ");
default:
System.out.println ("\n\t Inner :: Invalid ");
}
case 2:
System.out.println ("\n\t Outer Two ");
break;
case 3:
System.out.println ("\n\t Outer Three ");
break;
default:
System.out.println ("\n\t Outer :: Invalid ");
}

}
}

Saturday 25 June 2011

Nested if statement of java and its uses


Nested if:
                 It can be defined as an if statement containing another if statement with in its body/block. When we have to make one decision from multiple decisions we use nested if. A nested if is an if statement that is the target of another if or else. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. 

General syntax:
                           if (condition)      
                           {
                            if (condition)     
                            if (condition)         /* this if is associated with */
                            statement / block
                            else                      /* this else*/
                            statement / block
                            }
                            else                     /*and this else is associated with the most first if*/
                           statement / block
Here, in the block of if the second if in it is associated with the first if within the block. Remember an else always refers to the nearest if without an else and within the same block.

Java program using nested if


import java.util.*;
class nestedif
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b,c,d;
System.out.print ("\n\t Enter first number = ");
a=in.nextInt();


System.out.print ("\n\t Enter second number = ");
b=in.nextInt();

System.out.print ("\n\t Enter third number = ");
c=in.nextInt();

 if (a<b)
{
if (a<c)
System.out.println ("\n\t First number is smallest ");
else
System.out.println ("\n\t First number is not smallest");
}
else if (b<a)
{
if (b<c)
System.out.println ("\n\t Second number is smallest ");
else
System.out.println ("\n\t Second number is not smallest ");
}
else
System.out.println ("\n\t Third number is smallest ");

}
}

Note: 
          In the above program, after entering the values, computer will check that either a is less than b or not. If it is than it will check the other if condition in its block. If it is true then it will show the message 
"First number is smallest" otherwise the statement associated with else will be executed and rest of the code will be bypassed.

What is type casting?


Type casting:
            If we want to store an integer value in a byte or short type variable. What we will do? When we need to store a large value in a small data type variable we use type casting. In type casting we explicitly make a value narrow so that it will fit into the targeted variable. 

General syntax:
                  Two incompatible/different data types can be converted into same. Its general syntax is
  • (target-type) value;
Here, target-type is the casting type, that data type in which your given value will be converted and value can be any variable or constant value.

Click here to see a program of type casting

Java program to cast integer into short and byte data type


import java.util.*;
class casting
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a;
short b;
byte c;

System.out.print ("\n\t Enter the value = ");
a=in.nextInt();

b=(short)a;
c=(byte)a;

System.out.println ("\n\t value of a in short form = "+b);
System.out.println ("\n\t Value of a in byte form= "+c);

}
}

Note:
         In the above program you might have noticed these two statements
  • b=(short)a;
  • c=(byte)a;
these are the actual statements that are casting the data type of a into short and byte.

Friday 24 June 2011

Java operator precedence chart


Highest
Operator
()  []  .
++  --  ~  !
*/  %
+  -
>>  >>>  <<
>  >=  <  <=
==  !=
&
^
|
&&
||
?:
Lowest
=  *=  /=  %=  +=  -=



Wednesday 22 June 2011

Logical operators and their uses in java programming


Operator
Name
||
OR
&&
AND
==
Equal to
!=
Not equal to
?:
Ternary operator
  • Click here to see a program using EQUAL TO operator
  • Click here to see a program using NOT EQUAL TO operator
  • Click here to see a program using TERNARY operator
 

Use of TERNARY operator in java program


import java.util.*;
class ternary
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b,c;

System.out.print ("\n\t Enter first number = ");
a=in.nextInt();

System.out.print ("\n\t Enter second number = ");
b=in.nextInt();

a==b ? c=1:c=5;
System.out.println ("\n\t c = "+c);

}
}

Note:
          After entering values, in the above program, computer will check that whether 'a' is equal to 'b' or not. If 'a' is equal to 'b' than
  • c = 1 will be displayed
Otherwise " c = 5 " will be displayed

Using NOT EQUAL TO operator in java program


import java.util.*;
class notequal
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b;

System.out.print ("\n\t Enter first number = ");
a=in.nextInt();

System.out.print ("\n\t Enter second number = ");
b=in.nextInt();

if (a!=b)
System.out.println ("\n\t First number is not equal to second ");

else
System.out.println ("\n\t Both numbers are equal ");

}
}

Note:
          After you have entered values of both variables, computer will evaluate the condition
  • If value of 'a' isn't equal to value of 'b' then
" First number is not equal to second " will be displayed otherwise
" Both numbers are equal " will be displayed.

Java program using EQUAL TO operator


import java.util.*;
class usingequal
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b;

System.out.print ("\n\t Enter first number = ");
a=in.nextInt();

System.out.print ("\n\t Enter second number = ");
b=in.nextInt();

if (a==b)
System.out.println ("\n\t Both numbers are equal ");

else
System.out.println ("\n\t First number is not equal to second ");

}
}

Note: 
         After entering values, computer will check the condition 
  • If 'a' is equal to 'b' then
" Both numbers are equal " will be displayed. Otherwise 
" First number is not equal to second " this message will be displayed on screen.
  

Java program using AND operator


import java.util.*;
class usingand
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);

int a,b,c;

System.out.print ("\n\t Enter value of a = ");
a=in.nextInt();

System.out.print ("\n\t Enter value of b = ");
b=in.nextInt();

System.out.print ("\n\t Enter value of c = ");
c=in.nextInt();

if (a>b && a>c)
System.out.println ("\n\t a is greatest ");

else if (b>a && b>c)
System.out.println ("\n\t b is greatest ");

else
System.out.println ("\n\t c is greatest ");

}
}

Note:
          In this program, once you have entered values, first statement will be executed
  • If 'a' is greater than both 'b' and 'c'
if our first condition comes false than computer will execute second statement
  • If 'b' is greater than both 'a' and 'c'
if both conditions come false than third statement will be executed automatically i.e.
"c is greatest"

Java program using OR opeator


import java.util.*;
class usingor
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);

int a,b;

System.out.print ("\n\t Enter value of a = ");
a=in.nextInt();

System.out.print ("\n\t Enter value of b = ");
b=in.nextInt();

if (a<=10 || a<b)
System.out.println ("\n\t a is less then b or 10 ");
if (b>=25 || b==a)
System.out.println ("\n\t b is greater than 25 or equal to a ");

}
}

Note:
          In the above program, after you entered values of variables, first statement will be executed 
  • If 'a' is less than or equal to 10 
          OR
  • 'a' is less than 'b'
          and second statement will be executed in this case
  • If 'b' is greater than or equal to 25
          OR
  • 'b' is equal to 'a'

    Tuesday 14 June 2011

    How can you compile and execute your java program


    After you have installed java open your command prompt or simply click at start button and then click run.
    A small window will be opened. Write cmd in the text/search box and press run button.

    Once you have opened command prompt open the drive/partition in which you have saved your program.
    For example i have save my program in "local disk d/my progs". Write "d:" in command prompt and press enter.

    After this write the folder name (as in above paragraph d/my progs) and press enter. I will write it as "my progs" and then enter. Now, write your program name and press enter. Just like this:

    javac salary.java

    there "javac" is the command to compile your program and "salary.java" shows that the programs name is salary and it is a java file.

    Finally your program is compiled and ready to execute. When your program will be compiled successfully a class file of the program will be created in the same folder as in your program. To execute your program just write the following command

    java salary

    and your program will be executed.

    Note: 
            "salary" is the program name. You should write the name of your program.

    Arithmatic operators and their uses


    Operators
    Use
    +
    Addition
    -
    Subtraction
    *
    Multiplication
    /
    Division
    %
    Modulus
    ++
    Increment
    --
    Decrement
    =
    Assignment
    +=
    Addition assignment
    -=
    Subtraction assignment
    *=
    Multiplication assignment
    /=
    Division assignment
    %=
    Modulus assignment




      Monday 13 June 2011

      Using different arithmatic assignment operators in java program


      class assignops
      {
      public static void main (String arg[])
      {
      int a=10,b=25,c=5,d=18,e=49;
      a+=10;
      b-=10;
      c*=10;
      d/=2;
      e%=7;
      System.out.println ("\n\t a = "+a);

      System.out.println ("\n\t b = "+b);
      System.out.println ("\n\t c = "+c);

      System.out.println ("\n\t d = "+d);
      System.out.println ("\n\t e = "+e);
      }
      }

      Note:
                After execution you will observe following five changes
      1. Value of 'a' is increased by 10
      2. Value of 'b' is decreased by 10
      3. Value of 'c' is 50 (due to multiplying by 10)
      4. Value of 'd' is 9 (because we are dividing 'c' by 2)
      5. Value of 'e' is 0 (because 7 divides 49 properly)
       
        Share