Beruflich Dokumente
Kultur Dokumente
Deepak Gaikar
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 Harmonic series : 1 + 1/2 + 1/3 + ......+ 1/n
{
public static void main(String args[]) Enter a number : 3
{
Harmonic series for 3 is : 1.8333334
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<n;i++)
sum= sum + (float)1/(i+1);
System.out.println("\nHarmonic series for "+n+" is :
"+sum);
}
catch(Exception e)
{
System.out.println("I/O Error");
}
}
}
2 Write a program in Java to find nCr and nPr.
Sol:
int i , n , r , P , C ;
n = 6; r=3;
factorial = factorial * i ;
temp = temp * i ;
result = result * i ;
P = factorial / temp ;
C = P / result ;
{
public static void main(String args[])
{
int ch,i,j,in=1;
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i&&i<=3;j++)
System.out.print("\t"); //to leave space before stars for upper triangle
for(j=1;j<=i-1&&i>3;j++) //to leave space before stars for lower triangle
System.out.print("\t");
for(j=1;j<=in;j++) //to display stars
System.out.print("*\t");
if(i+1<=3)
in=in+2; //to decide no of stars in each row
else
in=in-2;
System.out.print("\n");
}
}
}
4 Write a program to display the following pattern.
* * * * *
* * * *
* * *
* *
*
Solution:
class pattern2
{
public static void main(String args[])
{
for(int i=1; i<=5; i++)
QUESTIONS BANK (CP-II) ( Using java) Compiled by : Prof. Deepak Gaikar
{
for(int j=1; j<=i-1; j++)
System.out.print(" ");
for(int k=1; k<=6-i; k++)
System.out.print("*");
System.out.println();
}
}
}
5 Write a java program to find factorial of a given number. the number will be provided
by user at run time.
Note:
The question require the number should be taken by user at run time there are various
ways to take input in java like using
1) BufferedReader class 2) DataInputStream class 3)Scanner class
The solution using BufferedReader class however student can use any valid class
to take input .
Solution:
// to find the Factorial value
import java.io.*;
class Factorial
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int fact=1;
System.out.print("Enter any Number : ");
int n=Integer.parseInt(br.readLine( ));
if(n>=0)
{
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");
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<=5; i++)
{
for(j=i; j>=1; j--)
{
System.out.print(j%2+" ");
}
System.out.println();
}
}
}
QUESTIONS BANK (CP-II) 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<=n; i++)
{
System.out.print(a+" ");
c=a+b;
a=b;
b=c;
}
}
}
10 Write a program to check whether given number is prime number or not and display
respectively.
/* Program to display whether a number is prime or not*/
import java.io.*;
class prime
{
public static void main(String args[])throws IOException
{
int i,num=0;
DataInputStream br = new DataInputStream(System.in);
System.out.println("Enter the no:");
num=Integer.parseInt(br.readLine());
QUESTIONS BANK (CP-II) Compiled by : Prof. Deepak Gaikar
i=2;
while(i<=num-1)
{
if(num%i==0)
{
System.out.print("Not a prime Number.");
break;
}
i++;
}
if(i==num)
System.out.print("Prime Number.");
}
}
11 Write a Program to display (EVEN) or ODD words for the input of integer no
Solution:
import java.io.*;
class even
{
public static void main(String args[])
{
DataInputStream br = new DataInputStream(System.in);
System.out.println("Enter any integer no.: ");
try
{
int n=Integer.parseInt(br.readLine());
if(n%2==0)
System.out.println("EVEN");
else
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<=n;i++) // in for loop take i as double not int...
{
s+=(float)1/a*z;
a+=1;
z*=-1;
}
System.out.print("Sum of the Series = "+s);
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<=n; i++)
{
sum+=1/i*m;
m*=-1;
}
System.out.print("Sum of the Series = "+sum);
}
catch(Exception e)
{
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<=100; t+=5)
{
s=s0+v0*t+1.0/2*a*t*t;
System.out.println("When t = "+t+" Distance travelled by the Object = "+s);
}
}
}