-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
1/14
QUESTIONS BANK (CP-II) Compiled by : Prof. Deepak Gaikar
CHAPTER 2 : Control Structure Introducing Classes
Program Questions
1 Write a program to determine the sum of the series for a given value of n.
1+1/2+1/3+ 1/n
Sol:
class P21
{
public static void main(String args[])
{
int n;
float temp,sum=0.0F;
// creating object of class DataInputStream.
DataInputStream in = new DataInputStream(System.in);
System.out.println("Harmonic series : 1 + 1/2 + 1/3 + ......+
1/n \n ");
try
{
System.out.print(" Enter a number : ");
n = Integer.parseInt(in.readLine( ));
for(int i=0;i
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
2/14
QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. Deepak Gaikar
{
int i , n , r , P , C ;
int factorial , temp , result ;
n = 6; r=3;
for (i=1,factorial=1 ; i
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
3/14
QUESTIONS BANK (CP-II) Compiled by : Prof. Deepak Gaikar
{
public static void main(String args[])
{
int ch,i,j,in=1;
for(i=1;i
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
4/14
QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. Deepak Gaikar
{
for(int j=1; j
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
5/14
QUESTIONS BANK (CP-II) Compiled by : Prof. Deepak Gaikar
while(n>1)
{
fact=fact*n;
n-=1;
}
System.out.println("Factorial ="+fact);
}
else
{
System.out.println("n must be zero or greater");
}
}
}
/*
Enter any Number : 5
Factorial =120
*/
6 write a program that reads a 4 digit integer and breaks it in a sequence of individual
digits e.g. 1691 should be displayed as 1691
Solution:
import java.io.*;
class digit
{
public static void main(String args[])throws IOException
{
int quotient,divident,divisor;divisor=1000;
DataInputStream br = new DataInputStream(System.in);
System.out.println("Enter a 4 digit no.: ");
divident=Integer.parseInt(br.readLine());
while(divident>0)
{
quotient=divident/divisor;
System.out.print(quotient+"\t");
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
6/14
QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. Deepak Gaikar
divident=divident%divisor;
divisor= divisor/10;
}
}
}
o/p:Enter a 4 digit no.:1691
1 6 9 1
7 write a program to display the following pattern
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Solution:
class pattern
{
public static void main(String arg[])
{int i,j;
for( i=1; i=1; j--)
{
System.out.print(j%2+" ");
}
System.out.println();
}
}
}
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
7/14
QUESTIONS BANK (CP-II) Compiled by : Prof. Deepak Gaikar
8 WAP and check with it is Armstrong number?
import java.io.*;
class AMSTRONG
{
public static void main(String args[])throws Exception
{
int n,r,t,s=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter value of n");
n=Integer.parseInt(br.readLine());
t=n;
for(;n>0;)
{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(s==t)
System.out.println("amstrong number");
else
System.out.println("NOT amstrong number");
}
}
o/p
enter value of n 153
amstrong number
9 Write a program to generate Fibonacci series.
Solution:
import java.io.*;
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
8/14
QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. Deepak Gaikar
class fibo
{
public static void main(String args[])throws IOException
{
int a=0, b=1, c;
DataInputStream br = new DataInputStream(System.in);
System.out.println("Enter the no of Fibonacci term to be printed.: ");
int n=Integer.parseInt(br.readLine());
for(int i=1; i
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
9/14
QUESTIONS BANK (CP-II) Compiled by : Prof. Deepak Gaikar
i=2;
while(i
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
10/14
QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. Deepak Gaikar
System.out.println("ODD");
}
catch(Exception e)
{
System.out.println("Error in Output/Input Stream");
}
}
}
12 write a program to determine the sum of the series:
1-1/2+1/3-1/4+..+1/n
Take value of n from the user.
Solution :
import java.io.*;
class series3
{
public static void main(String arg[])throws IOException
{
DataInputStream br= new DataInputStream(System.in);
int a=1,z=1; // m= 1 (say)
float s=0f;
System.out.println("Enter the value of n.: ");
int n=Integer.parseInt(br.readLine());
for(int i=1;i
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
11/14
QUESTIONS BANK (CP-II) Compiled by : Prof. Deepak Gaikar
}
}
o/p
Enter the value of n.:3
Sum of the Series =0.83333
-------------********---------
or simillarly
/*
import java.io.*;
class series3
{
public static void main(String arg[])
{
try
{
DataInputStream br = new DataInputStream(System.in);
int m=1;
double sum=0;
System.out.println("Enter the value of n.: ");
int n=Integer.parseInt(br.readLine());
for(double i=1; i
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
12/14
QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. Deepak Gaikar
System.out.println("Error in Output/Input Stream");
}
}
}
*/13 s=1/1-1/3+1/5-1/7+1/9............+n times find sum of series
Solution:
import java.io.*;
class series1
{
public static void main(String arg[])throws IOException
{
DataInputStream br= new DataInputStream(System.in);
int a=1,z=1;
float s=0f;
System.out.println("Enter the value of n.: ");
int n=Integer.parseInt(br.readLine());
for(int i=1;i
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
13/14
QUESTIONS BANK (CP-II) Compiled by : Prof. Deepak Gaikar
14 s=1/3+5/5+9/7 +13/9++n times
Solution:
import java.io.*;
class series2
{
public static void main(String arg[])throws IOException
{
DataInputStream br= new DataInputStream(System.in);
int a=1,b=3;
float s=0f;
System.out.println("Enter the value of n.: ");
int n=Integer.parseInt(br.readLine());
for(int i=1;i
-
8/6/2019 Chapter 2 Program Solution(Control Structure & Classes)
14/14
QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. Deepak Gaikar
int t=1;
double s,s0=0, v0=0,a=0;
DataInputStream in=new DataInputStream(System.in);
System.out.print("\nEnter value of a : ");
a=Integer.parseInt(in.readLine());
s=s0+v0*t+1.0/2*a*t*t;
System.out.println("When t = "+t+" Distance travelled by the Object = "+s);
//this for loop will calculate distance s at time t=5,10,15,20,.....,100.
for(t=5; t