Object Oriented Programming: Unit I: Introduction to OOP and Java

Control Statements Example Java Programs

Control Statements Example Programs : 1.Write a Java program to find factorial of a given number.

Control Statements Example Programs

Ex. 1.11.1: Write a Java program to find factorial of a given number.

Sol.:

Method 1: Factorial program without recursion i.e. using control statement

class FactorialProgram

{

public static void main(String args[])

{

int i,fact=1;

int number=5;//The factorial of 5 is calculated

i=1;

while(i<number)

{

fact=fact*i;

i++;

}

System.out.println("Factorial of "+number+" is: "+fact);

}

}

Output

Factorial of 5 is: 24

Method 2: Factorial program using recursion

class FactorialProgramRec

{

static int factorial(int n)

{

if (n== 0)

return 1;

else

return(n* factorial(n-1)); //recursive call

}

public static void main(String args[])

{

int i,fact=1;

int number=5;//Factorial of this number is calculated.

fact = factorial(number);

System.out.println("Factorial of "+number+" is: "+fact);

}

}

Output

Factorial of 5 is: 120

Ex. 1.11.2: Write a Java program to display following pattern.

    *

   * *

  * * *

 * * * *

* * * * * 

Sol.: 

class TriangleDisplay

{

public static void main(String[] args)

{

int i,j,k;

for(i=1; i<=5; i++)

{

for(j=4; j>=i; j--)

{

System.out.print(" ");

}

for(k=1; k<=(2*i-1); k++)

{

System.out.print("*");

}

System.out.println(""");

}

}

}

Output

*

***

*****

*******

*********

Ex. 1.11.3: Write a Java program to display following number pattern

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Sol.:

class NumberPatternDisplay

{

public static void main(String[] args)

{

int i,j;

for(i=1; i<=5; i++)

{

for(j=1;j<=i; j++)

{

System.out.print(j+" ");

}

System.out.println("");

}

}

}

Output

Ex. 1.11.4 Write a program for following series 1+1/2+1/22+1/23+...+1/2n

Sol.:

Java Program

//Program for computing 1+1/2+1/2^2+1/2 3+...+1/2 n

import java.io.*;

import java.lang.*;

import java.math.*;

public class Series1

{

public static void main(String args[])throws IOException

{

int n;

double val;                                            

double sum=1;

n=5;

System.out.println("\n\t\tProgram for displaying the sum of series");

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

{

val=1/(Math.pow(2,1));

sum sum+val;

}

System.out.println("\nSum of the series is "+sum);

}

}

Output

Program for displaying the sum of series

Sum of the series is 1.9375

Ex. 1.11.5 Write a Java application that computes the value of ex by using the following series. ex = 1+x/1!+x2/2!+x3/3!+...

Sol. :

//Program for computing sum of series

import java.io.*;

import java.lang.*;

import java.math.*;

public class Series2

{

public static int fact(int n)

{

int x,y; if(n<0)

{

System.out.println("The negative parameter in the factorial function");

return -1;

}

if(n==0)

return 1;

x=n-1;

y=fact(x); /*recursive call*/

return(n*y);

}

public static void main(String args[])throws IOException

{

int n;

float val;

float sum=1;

int x=2;

n=3;

System.out.println("\n\t\tProgram for displaying the sum of series");

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

{

val=(float) (Math.pow(x,i))/fact(i);

sum=sum+val;

}

System.out.println("\nSum of the series is "+sum);

}

}

Output

Program for displaying the sum of series

Sum of the series is 6.3333335

Ex. 1.11.6: Write a Java program that reverses the digits of given number.

Sol. :

//Program for reversing digits of given number

import java.io.*;

public class Reversenum

{

public static void main(String[] args)

{

int num=1234;

int rev num=0;

while(num!=0)

{

rev_num=(rev_num*10)+(num%10);

num=num/10;

}

System.out.println(rev_num);

}

}

Output

4321

Ex.1.11.7 Write a Java code using do-while loop that counts down to 1 from 10 printing exactly ten lines of "hello".

Sol. :

class test

{

public static void main(String args[])

{

int count = 10;

do

{

System.out.println("Hello");

count--;

} while(count>=1);

}

}

Output

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Hello

Object Oriented Programming: Unit I: Introduction to OOP and Java : Tag: : - Control Statements Example Java Programs