COMPUTER
COMPUTER
Algorithm
STEP 1 - START
STEP 2 - INPUT amt
STEP 3 - z=amt%10, g=amt/10
STEP 4 - IF g!=1 THEN GOTO STEP 5 OTHERWISE GOTO STEP 6
STEP 5 - PRINT x2[g-1]+" "+x1[z]
STEP 6 - PRINT x[amt-9]
STEP 7 – END
Solution
import java.io.*;
class Num2Words
{
public static void main(String args[])throws IOException //main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any Number(less than 99)");
int amt=Integer.parseInt(br.readLine()); //accepting number
int z,g;
String x[]= {“”,"Ten" ,"Eleven" ,"Twelve" ,"Thirteen" ,"Fourteen" ,"Fifteen" ,"Sixteen" ,"Seventeen" ,"Eighteen"
,"Nineteen"};
String x1[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
String x2[]={"","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
z=amt%10; //finding the number in words
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+" "+x1[z]);
else System.out.println(x[amt-9]);
}
Variable Description Table
variable Datatype Purpose
br bufferReader BufferReader object
z Int Calculating and storing the remainder
g Int Calculating and storing the value
x String[] Array storing no in words
X1 String[] Array storing no in words
X2 String[] Array storing no in words
amt Int Storing value
Output
Question 2
Write a program in java to calculate factorial of a number using recursion.
Algorithm
STEP 1 - START
STEP 2 - INPUT n
STEP 3 - IF(n<2) THEN return 1 OTHERWISE return (n * fact(n-1))
STEP 4 – END
Solution
import java.io.*;
class Factorial
{
public static void main(String args[]) throws IOException //main function
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no =");
int n = Integer.parseInt(br.readLine()); //accepting no.
Factorial obj = new Factorial();
long f = obj.fact(n);
System.out.println("Factorial ="+f); //displaying factorial
}
public long fact(int n) //recursive fact()
{
if(n<2)
return 1;
else
return (n*fact(n-1));
}
}
Output:
Question 3
Write a PROGRAM to enter a number at run time in a member function and by using a
logical method check if the number is a Kaprekar number or not.(A kaprekar number
is a number whose sum of digits square is equal to the number)
Example:- 9; 92 = 81 => 8+1=9.
Algorithm
STEP 1- Start
STEP 2- Enter the number to be checked
STEP 3- Square the number
STEP 4- Find the sum of digits of the square
STEP 5- If the sum is equal to the number then go to 6 else go to 7
STEP 6- Print number is a kaprekar number, go to end
STEP 7- Print number is not a kaprekar number
STEP 8- End
Solution
import java.util.*; //IMPORTING SCANNER CLASS
class kaprekar //CLASS BEGINS
{
int n;
void input() //MEMBER METHOD
{
System.out.println("ENTER A NUMBER"); //PRINTING
Scanner d= new Scanner(System.in);
n=d.nextInt();
}
void check() //MEMBER METHOD
{
int p,a,b; //INITIALIZING
int c=0;
int x=n*n
p=x;
while(x>0 )
{
x=x/10;
c++;
}
if(c%2==0
{
a=p%(int)Math.pow(10,(c/2)); //CALCULATIONS
b=p/(int)Math.pow(10,(c/2));
if((a+b)==n)
System.out.println(n+" is a Kaprekar number");
else
System.out.println(n+" is not a Kaprekar number"); //PRINTING
}
}
public static void main(String args[])throws Exception //MAIN METHOD
{
kaprekar obj = new kaprekar();
obj.input();
obj.check();
}
}
Output:
Question 4
A smith number is a composite number, the sum of whose digits is the sum of the
digits of its prime factors obtained as a result of prime factorization (excluding 1).
The first few such numbers are 4, 22, 27, 58, 85, 94, 121.....
Example 1.
666 Prime factors are 2, 3, 3 and 37 Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors (2+3+3+(3+7) = 18
Write a program to input a number and check whether it is a smith number or not.
Sample data:
Input: 94
Output: SMITH Number
Input: 102
Output: NOT SMITH NUMBER
Algorithm
STEP 1- Input n
STEP 2-Run a while loop to find the sum of digits of n
STEP 3- Now run a for loop from 2 to the number
STEP 4- Look for the factors of n
STEP 5- If a factor is found, run a while loop to store its sum of digits Step-6: Update the number by dividing it
with the factor
STEP 7- Decrement the loop counter so that the same factor is checked again
STEP 8- Outside the loop compare if the two sums are equal or not
STEP 9- If they are equal display “Smith number” else display “Not Smith number”
STEP 10- End
Solution
import java.util.*;
class smithno
{
public static void main(String sr[])throws InputMismatchException
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int n=sc.nextInt();
int p,q,i,sod=0,sopf=0,t;
p=q=n;
//Find the sum of all the digits of the number
while(p>0)
{
sod+=p%10; p/=10;
}
for(i=2;i<=q;i++)
{
if(q%i==0)
{ //check if „i‟ is a factor
t=i;
while(t>0)
{ //find the sum of the digits of the factor
sopf+=t%10;
t/=10;
}
q=q/i;
i--; //decrement the factor so that next time the same factor is checked again and again
}
}
if(sod==sopf) // if sum of digits and sum of prime factors are equal, it is smith number
System.out.println("Smith number");
Else
System.out.println("Not Smith number");
} //end of main
} //end of class
Output:
Question 5
Write a program in java to display the fibonacci series using recursion
The Fibonacci series is the sequence of numbers (also called Fibonacci numbers),
where every number is the sum of the preceding two numbers
Algorithm
STEP 1 - start
STEP 2 - input n
STEP 3 - if(n<=1) then return 1 otherwise return (fib(n-1) +fib(n-2))
STEP 4 – end
Solution
import java.io.*;
class Fibonacci
{
public static void main(String args[]) throws IOException //main function
{
Fibonacci obj = new Fibonacci();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no of term ="); //accepting no. of terms
int n = Integer.parseInt(br.readLine());
System.out.println();
for(int i=1;i<=n;i++) //Fibonacci element display loop
{
int f = obj.fib(i);
System.out.print(f+" ");
}
}
public int fib(int n) //Recursive function fib() for calculation of Fibonacci element
{
if(n<=1)
return n;
else
return (fib(n-1) +fib(n-2));
}
}
Output
Question 6
Write a program in java to convert celsius into Fahrenheit
using inheritance.
Algorithm
STEP 1 – START
STEP 2 -- Input temperature „celcius‟ in celcius
STEP 3 – far=1.8*celcius + 32
STEP 4 – Display far
STEP 5 – END
Solution
import java.io.*;
class C2F
{
public static void main(String args[])throws IOException //main function
{
Temperature ob= new Temperature();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter temperature in Celsius"); //accepting temperature
double temp=ob.convert(Double.parseDouble(br.readLine()));
System.out.println("The temperature in fahrenheit is = "+temp);
}
}
class Temperature extends C2F
{
double convert(double celcius) //function to convert Celsius to fahrenheit
{
double far=1.8*celcius+32.0;
return far;
}
}
Output:
Question 7
Write a program in java to input a decimal no and print its binary equivalent.
Algorithm
STEP 1-Start
STEP 2-Input n
STEP 3-b=0,c=0
STEP 4-rem=n%2
STEP 5-n=n/2(Taking integer part)
STEP 6-b=b+rem*10^c
STEP 7-c=c+1
STEP 8-repeat steps 4-7 till n>=0
STEP 9-Print b
STEP 10-End
Solution
import java.util.*; //CALLING FUNCTIONS OF SCANNER CLASS
class binary
int x=ob.nextInt();
int num,r;
num=x;
bin=r+bin;
Output
Question 8
write a program in java to search an element in an array using linear search.
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1
STEP 6 - FROM i=0 to i<n REPEAT STEP 7
STEP 7 - IF (a[i] == v) THEN flag =i
STEP 8 - IF (flag=-1) THEN GOTO STEP 9 OTHERWISE GOTO STEP 10
STEP 9 - PRINT “ not found”
STEP 10 - PRINT v+" found at position - "+flag
STEP 11 – END
Solution
import java.io.*;
class LinearSearch
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public LinearSearch(int nn)
{
n=nn;
}
public void input() throws IOException //function for obtaining values from user
{
System.out.println("enter elements");
for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(br.readLine());
}
}
public void display() //function displaying array values
{
System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
}
public void search(int v ) //linear search function
{
int flag=-1;
for(int i=0; i<n ; i++)
{
if(a[i] == v)
flag =i;
}
if(flag== -1 )
System.out.println("not found");
else System.out.println(v+" found at position - "+flag);
}
public static void main(String args[]) throws IOException //main function
{
LinearSearch obj = new LinearSearch(10);
obj.input();
obj.display();
System.out.println("enter no. to be searched -"); //accepting the values to be searched
int v = Integer.parseInt(br.readLine());
obj.search(v);
}
}
Output:
Question 9
Design a program that accepts 3 integers and with the help of them, displays a
GEOMETRIC PROGRESSION SERIES and the sum of all terms in that series. The
specifications are: -
Class name: GPseries
DataMembers :
a : to store first term
r : to store common ratio
n : to store limit
Member Functions :
void readdata() - to input a, r, n.
long nThterm(int) - to return nTh term of the series
long sum() - to return sum of the series
void showseries() - to display the series and sum.
Algorithm
STEP 1-Start
STEP 2-Create method readdata() for accepting „a‟, „r‟, and „n‟ from the user.
STEP 3-Create method int nThterm(int) for finding the nth term of the series.
STEP 4-Create method int sum() for returning the sum of the series.
STEP 5-Create method showseries() for displaying the series.
STEP 6-Finally created main(), that executes the entire program
Solution
import java.util.Scanner;
class GPseries
{
int a,r,n; //Declaring the global variables
/*Function to take input for the global variables*/
void readdata()
{
Scanner kv = new Scanner (System.in);
System.out.print("Enter first term : ");
a=kv.nextInt();
System.out.print("Enter common ratio : ");
r =kv.nextInt();
System.out.print("Enter limit for the series : ");
n=kv.nextInt();
}
/**Function to return nTh term of the series*/
long nThterm(int b)
{
return a*(int)Math.pow(r,b-1);
}
/** Function to return sum of the series*/
long sum()
{
return a*(int)(Math.pow(r,n)-1)/(r-1);
}
/** Function to display the series and sum*/
void showseries()
{
for(int i=1;i<=n;i++)
System.out.print(nThterm(i)+" ");
System.out.println();
System.out.println("Sum of the series = "+sum());
}
/** main() function to call the respective functions accordingly*/
public static void main(String args[])
{
GPseries a=new GPseries();
a.readdata();
System.out.println("GENERATED GP SERIES -- :");
a.showseries();
}
}
Output:
Question 10
Write a program in java To print the calendar of the particular year and month of
that year.
Algorithm
STEP 1-Start
STEP 2-Take an array having the number of days.
STEP 3-Enter the month
STEP 4-Enter the year
STEP 5-Check whether the year is leap or not. If it is a leap year add 1 to the number of days and to the second
subscript of taken array
STEP 6-Display the name of the days of the week and print the dates up to the limit which is obtained by
confirming the taken array.
STEP 7-End
Solution
import java.util.*;
class calender
{
/**main() function to call the respective functions accordingly*/
public static void main(String args[])
{
Scanner ob= new Scanner(System.in);
System.out.println("Enter the year");
int yr=ob.nextInt(); //taking the required year
System.out.println("Enter the month");
int mn=ob.nextInt(); //taking the required month
int d=0;
if(mn==2) //checking for leap year
{
if(yr%4==0)
d=29;
else
d=28;
}
else
{
if(mn==1|| mn==3||mn==5||mn==7||mn==8||mn==10|| mn==12)
d=31;
else
{
if(mn==4 || mn==6 || mn==9 || mn==11)
d=30;
else
System.out.println("Invalid month.");
}
}
int m=mn-2;
if(m<0)
m=13+m;
int c=yr/100;
int y=yr%100;
if(y==99 && (mn==1 || mn==2))
c=c-1;
int w=(1+(int)(2.6*m-0.2)-2*c+y+(int)(y/4)+(int)(c/4))%7;
System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
int flag=0;
for(int j=1;j<=w;j++)
{
System.out.print("\t ");
flag++;
}
for(int i=1;i<=d;i++) //printing the dates
{
System.out.print(i+"\t ");
flag++;
if(flag==7)
{
System.out.println();
flag=0;
}
}
}
Output:
Question 11
Write a PROGRAM to enter a password at runtime and check wether it is “ admin” or not. The user must be
given three chances to enter the correct password.
Algorithm
STEP 1-Start
STEP 2-Create a counter(say c) and initialize it with 0
STEP 3-Print Enter the password
STEP 4-If password entered is Admin then go to 5 else go to 6
STEP 5-Print Ok, go to end
STEP 5-Print a Sorry try again, and increase the value of c
STEP 6-Repeat steps 3-6 until the value of becomes 3
STEP 7-End
Solution
import java.util.*;
class Password
{ //CLASS BEGINS
public static char password_check(String s)
{
String p="ADMIN";
if(p.equalsIgnoreCase(s)) //CONDITION
{
return '0'; //RETURNING
}
else
return 's'; //RETURNING
}
public static void main(String args[])throws Exception
{ //MAIN BEGINS
char ch;
Scanner ob=new Scanner(System.in);
System.out.println("ENTER THE PASSWORD");
for(int i=1;i<=3;i++)
{
String s=ob.next();
ch=password_check(s);
if(ch=='0')
{
System.out.println("OK");
break;
}
if((ch=='s')&&(i!=3))
System.out.println("SORRY TRY AGAIN");
if((ch=='s')&&(i==3))
{
System.out.println("SORRY NO MORE CHANCES"); //PRINTING
}
}
} //MAIN ENDS
} //CLASS ENDS
Question 12
Write a program in java to find the search element using binary search technique.
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1 , l=0, u=n-1
STEP 6 - IF(l<=u && flag=-1) REPEAT STEP 7 AND Step 8
STEP 7 - m = (l+u)/2
STEP 8 - IF (a[m] == v) THEN flag =m OTHERWISE GOTO STEP 9
STEP 9 - IF (a[m] < v) THEN l = m+1 OTHERWISE u =m-1
STEP 10 - IF (flag=-1) THEN GOTO STEP 11 OTHERWISE GOTO STEP 12
STEP 11 - PRINT “ not found”
STEP 12 - PRINT v+" found at position - "+flag
STEP 13 - END
Solution
import java.io.*;
class BinarySearch
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public BinarySearch(int nn) //default constructor
{
n=nn;
}
public void input() throws IOException //function accepting array elements
{
System.out.println("enter elements");
for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(br.readLine());
}
}
public void display() //displaying array elements
{
System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
}
public void search(int v) //function to search array elements using binary search
{
int l=0;
int u = n-1;
int m;
int flag=-1;
while( l<=u && flag == -1)
{
m = (l+u)/2;
if(a[m] == v)
flag = m;
else if(a[m] < v)
l = m+1;
else u = m-1;
}
if(flag== -1 )
System.out.println("not found");
else System.out.println(v+" found at position - "+flag);
}
public static void main(String args[]) throws IOException //main function
{
BinarySearch obj = new BinarySearch(10);
obj.input();
obj.display();
System.out.println("enter no. to be searched -");
int v = Integer.parseInt(br.readLine()); //accepting integer to be searched by binary search
obj.search(v);
}
}
Output:
Question 13
Write a PROGRAM to input a String and print it in following pattern.
H
HE
HEL
HELL
HELLO
HELL
HEL
HE
H
Algorithm
STEP 1 - Start
STEP 2 - Input string s
STEP 3 - Input alphabets in string
STEP 4 - Print first element in first line
STEP 5 - First two elements in second line
STEP 6 - Repeat step 4-5 till whole word is printed.
STEP 7 - Print on next line by removing last word
STEP 8 - Next line printing by removing last and second last word
STEP 9 - Repeat till first alphabet is only left
STEP 10 - Print new string s
STEP 11 – End
Solution
import java.util.*;
class Pattern
{
public static void main(String args[ ]) //main() to call other functions
{
Scanner ob= new Scanner (System.in);
System.out.println("Enter a string");
Question 14
Design a program to print the following pattern upto the inputted number of lines.
*
* *
* * *
Algorithm
STEP 1- Start.
STEP 2- Accepted the no. of lines for the pattern in variable „n‟, from the user.
STEP 3- Each line of the pattern has total (n+(n-1)) characters, that include spaces and asterisks at appropriate
places. The variable int cpl holds the same.
STEP 4- Using three simple for() loops, the pattern is printed.
STEP 5- End
Solution
class format
{ //CLASS BEGINS
public static void main(int n)
{ //MAIN BEGINS
int cpl= (n)+(n-1);
for(int i=n ; i>=1 ; i--) //LOOP
{
if(i==1)
{
for(int e=cpl ; e>=1; e--)
System.out.print("*");
}
else
{
for(int j=1 ; j<=i ; j++)
{
if(j==i)
System.out.print("*");
else
System.out.print(" ");
}
for(int k=(cpl-i) ; k>=1 ;k--)
{
if(k==i)
System.out.print("*");
Else
System.out.print(" ");
}
System.out.println();
}
}
} //MAIN ENDS
} //CLASS ENDS
Output:
Question 15
write a program to state binomial theorem:
class name-binomial
instance variables-double a,x
member methods-
1.binomial()-non-parameterised constructor.
2.long factorial(long f)-to find factorial of given number.
3.double ncr(int n,int r)-to return ncr=(n!/((n-r)!*r!)).
4.double sum(int n).
Algorithm
STEP 1-Start.
STEP 2-Create a method long factorial to calculate factorial of given
number.
STEP 3-Create a method double nCr to calculate the nCr=(n!/((n-r)!*r!)).
STEP 4-Create a method double sum to calculate the sum of binomial
expression.
STEP 5-Specify these methods in the main method.
STEP 6-End.
Solution
import java.io.*;
class binomial
{
double x,a;
public binomial()
{
x=0;
a=0;
}
public long factorial(long f)
{
int x3; int ft=1;
for(x3=1;x3<=f;x3++)
{
ft=ft*x3;
}
return ft;
}
public double nCr(int n,int r)
{
long p,q,y;
double ncr;
p=factorial(n);
q=factorial(n-r);
y=factorial(r);
ncr=p/(q*y);
return ncr;
}
public double sum(int n)
{
double s=0,t;
int p=n;
for(int xt=0;xt<=n;xt++)
{
t=nCr(n,xt)*(Math.pow(x,p))*(Math.pow(a,xt));
s=s+t;
p--;
}
return s;
}
public void main(int p)
{
double k;
k=sum(p);
System.out.print(k);
}
}
Output:
Question 16
Write a program In java to sort an array using selection sort
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1
STEP 6 - FROM i=0 to i<n-1 REPEAT STEP 7 to STEP 11
STEP 7 - min =i
STEP 8 - FROM j=i+1 to j<n REPEAT STEP 8
STEP 9 - IF(a[j]<a[min]) then min =j
STEP 10 - IF (min!=i) GOTO STEP 11
STEP 11 - temp = a[i], a[i] =a[min], a[min] = temp
Solution
import java.io.*;
class SelectionSort
{
int n,i;
int a[] = new int[100];
public SelectionSort(int nn) //parameterized constructor
{
n=nn;
}
public void input() throws IOException //function accepting array elements
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter elements");
for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(br.readLine());
}
}
public void display() //function displaying array elements
{
System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
}
public void sort() //function sorting array elements using selection sort technique
{
int j,temp,min;
for(i=0;i<n-1;i++)
{
min =i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
min =j;
}
if(min!=i)
{
temp = a[i];
a[i] =a[min];
a[min] = temp;
}
}
}
public static void main(String args[]) throws IOException //main function
{
SelectionSort x = new SelectionSort(5);
x.input();
System.out.print("Before sorting - ");
x.display();
System.out.print("After sorting - ");
x.sort();
x.display();
}
}
Output:
Question 17
Write a program in java to sort an array using bubble sort
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1
STEP 6 - FROM i=0 to i<n-1 REPEAT STEP 7 to STEP 9
STEP 7 - FROM j=i+1 to j<n REPEAT STEP 8
STEP 8 - IF(a[j] > a[j+1]) THEN GOTO STEP 9
STEP 9 - temp = a[i], a[i] =a[min], a[min] = temp
STEP 10 – END
Solution
import java.io.*;
class BubbleSort
{
int n,i;
int a[] = new int[100];
public BubbleSort(int nn) //parameterized constructor
{
n=nn;
}
public void input() throws IOException //function accepting array elements
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter elements");
for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(br.readLine());
}
}
public void display() //function displaying array elements
{
System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
}
public void sort() //function sorting array elements using Bubble Sort technique
{
int j,temp;
for(i=0 ; i<n-1 ; i++)
{
for(j=0 ; j<n-1-i ; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] =a[j+1];
a[j+1] = temp;
}
}
}
}
public static void main(String args[]) throws IOException //main function
{
BubbleSort x = new BubbleSort(5);
x.input();
System.out.print("Before sorting - ");
x.display();
System.out.print("After sorting - ");
x.sort();
x.display();
}
}
Output:
Question 18
Write a PROGRAM to input a date and print it in manual form.
Algorithm
STEP 1 - Start
STEP 2 - Enter the date
STEP 3 - Extract the last four digits of the date and store it in a variable(say y)
STEP 4 - Extract the next two digits of the date and store it in a variable(say m)
STEP 5 - Extract the next two digits of the date and store it in a variable(say d)
STEP 6 - Print d and „st‟,‟nd‟,‟rd‟ or „th‟ accordingly
STEP 7 - If m =1 then print January, if it is 2 then print February and so on
STEP 8 - Now print y
STEP 9 – End
Solution
import java.util.*;
class date //CLASS BEGINS
{
public static void main(String arg[]) //MAIN METHOD
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter the date");
int t=ob.nextInt();
String A[] = new String[12]; //FALL THROUGH
A[0] ="January";
A[1] ="February";
A[2] ="March";
A[3] ="April";
A[4] ="May";
A[5] ="June";
A[6] ="July";
A[7] ="August";
A[8] ="September";
A[9] ="October";
A[10] ="November";
A[11] ="December";
int y=t%10000;
int m=(int)(t/10000)%100;
int d=(int)(t/1000000);
if(d==1 || d==21 || d==31) //CONDITION
{
System.out.println(d+"st "+A[m-1]+" "+y);
}
else
{
if(d==2 || d==22) //CONDITION
{
System.out.println(d+"nd "+A[m-1]+" "+y);
}
else
{
if(d==3 || d==23)
{
System.out.println(d+"rd "+A[m-1]+" "+y); //PRINTING
}
else
{
System.out.println(d+"th "+A[m-1]+" "+y); //PRINTING
}
}
}
} //MAIN ENDS
} //CLASS ENDS
Output:
Question 19
Write a PROGRAM to generate prime factors of a number input by the user.
For example:
Prime factors of 196 are 2, 7
Algorithm
STEP 1 - Start
STEP 2 - Enter the number by the user
STEP 3 - Take a counter(say i) and initialize it with 1
STEP 4 - If the number is divisible by i then go to 5 else go to 7
STEP 5 - If the number is prime then go to 6 else go to 7
STEP 6 - Print i , if i is equal to the number or greater than it then go to end, else go to 7
STEP 7 - Increase i by 1
STEP 8 - Go to 4
STEP 9 - End
Solution
import java.util.*;
class primeFactors
{
static int n;
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.println("Input a number");
n=ob.nextInt(); //Input of the number
System.out.println("The Prime Factors are: ");
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
if(isPrime(i))
System.out.print(i+" ");
}
}
}
/** Function to check for the prime number
* /return true – for prime number*/
*/ return false – for non-prime number */
public static booleanisPrime(int a)
{
int c=0;
for(int i=1;i<=a;i++)
{
if(a%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}
}
Output:
Question 20
Write a program in java to display a spiral matrix
Algorithm
STEP 1 - START
STEP 2 - INPUT a[][]
STEP 3 - IF p!=(int)Math.pow(l,2) GOTO STEP 4
STEP 4 - IF co!=0 GOTO STEP 5
STEP 5 - re=1
STEP 6 - IF ri=1;ri<=k1-re;ri++ GOTO STEP 7
STEP 7 - p++,c++
STEP 8 - IF c==l GOTO STEP 9
STEP 9 - BREAK
STEP 10 - a[r][c]=p
STEP 11 - IF c==l GOTO STEP 12
STEP 12 - BREAK
STEP 13 - IF dw=1 GOTO STEP 14
STEP 14 - p++,r++,a[r][c]=p
STEP 15 - IF le=1 GOTO STEP 16
STEP 16 - p++,c--,a[r][c]=p
STEP 17 - IF up=1 GOTO STEP 18
STEP 18 - p++,r--,a[r][c]=p
STEP 19 - k1=k1+2, k2=k2+2 & co++
STEP 20 - up++ & IF up<=k2-1 GOTO STEP 18
STEP 21 - le++ & IF le<=k2-1 GOTO STEP 16
STEP 22 - dw++ & IF dw<=k1-1 GOTO STEP 14
STEP 23 - IF y=0 GOTO STEP 24
STEP 24 - IF yy=0 GOTO STEP 25
STEP 25 - PRINT "\t"+a[y][yy]) & ()
STEP 26 - yy++ & IF yy<l GOTO STEP 25
STEP 27 - y++ & IF y<l GOTO STEP 24
STEP 28 – END
Solution
import java.io.*;
class SpiralMatrix
{
public static void main(String[] args) throws IOException //main function
{
int a[][],r,c,k1=2,k2=3,p=0,co=0,re=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the dimension of matrix A x A =");
int l = Integer.parseInt(br.readLine()); //accepting dimension of square spiral matrix
a=new int[l][l];
r=l/2;c=r-1;
if(l%2==0)
{
System.out.println("wrong entry for spiral path");
System.exit(0);
}
/*Calculating and displaying spiral matrix*/
while(p!=(int)Math.pow(l,2))
{
if(co!=0)
re=1;
for(int ri=1;ri<=k1-re;ri++)
{
p++;c++;if(c==l)break;a[r][c]=p;}
if(c==l)break;
for(int dw=1;dw<=k1-1;dw++)
{
p++;r++;a[r][c]=p;}
for(int le=1;le<=k2-1;le++)
{
p++;c--;a[r][c]=p;}
for(int up=1;up<=k2-1;up++)
{
p++;r--;a[r][c]=p;}
k1=k1+2;
k2=k2+2;
co++;
}
for(int y=0;y<l;y++) //Displaying matrix
{
for(int yy=0;yy<l;yy++)
System.out.print("\t"+a[y][yy]);
System.out.println();
System.out.println();
}
Output:
Question 21
Write a program in java to display a magic square
Algorithm
STEP 1 - START
STEP 2 - arr[][]=new int[n][n],c=n/2-1,r=1,num
STEP 3 - IF num=1;num<=n*n;num++ GOTO STEP 4
STEP 4 - r--,c++
STEP 5 - IF r==-1 GOTO STEP 6
STEP 6 - r=n-1
STEP 7 - IF c>n-1 GOTO STEP 8
STEP 8 - c=0
STEP 9 - IF arr[r][c]!=0 GOTO STEP 10
STEP 10 - r=r+2 & c--
STEP 11 - num++ & IF num<=n*n GOTO STEP 4
STEP 12 - arr[r][c]=num
STEP 13 - IF r==0&&c==0 GOTO STEP 14
STEP 14 - r=n-1, c=1 & arr[r][c]=++num
STEP 15 - IF c==n-1&&r==0 GOTO STEP 16
STEP 16 - arr[++r][c]=++num
STEP 17 - PRINT ()
STEP 18 - IFr=0 GOTO STEP 19
STEP 19 - IF c=0 GOT STEP 20
STEP 20 - PRINT arr[r][c]+" " & ()
STEP 21 - c++ & IF c<n GOTO STEP 20
STEP 21 - r++ & r<n GOTO STEP 19
STEP 22 – ENd
Solution
/*A Magic Square is a square whose sum of diagonal elements, row elements and coloumn
elements is the same*/
import java.io.*;
class MagicSquare
{
public static void main(String args[])throws Exception //main function
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the dimension of magical square=");
int n = Integer.parseInt(br.readLine()); //accepting dimensions
int arr[][]=new int[n][n],c=n/2-1,r=1,num;
for(num=1;num<=n*n;num++) //loop for finding magic square elements
{
r--;
c++;
if(r==-1)
r=n-1;
if(c>n-1)
c=0;
if(arr[r][c]!=0)
{
r=r+2;
c--;
}
arr[r][c]=num;
if(r==0&&c==0)
{
r=n-1;
c=1;
arr[r][c]=++num;
}
if(c==n-1&&r==0)
arr[++r][c]=++num;
}
System.out.println();
for(r=0;r<n;r++) //loop displaying magic square
{
for(c=0;c<n;c++)
System.out.print(arr[r][c]+" ");
System.out.println();
}
Output:
Question 22
Write a program in java to To Generate Sum of All Elements of a Double Dimensional
Array of 5*5 Subscripts
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM x =0 to x<5 REPEAT STEP 4
STEP 4 - FROM y =0 to y<5 REPEAT STEP 5
STEP 5 - PRINT (a[x][y]+" "
STEP 6 - FROM x =0 to x<5 REPEAT STEP 7
STEP 7 - FROM y =0 to y<5 REPEAT STEP 8
STEP 8 - Sum=Sum+a[x][y]
STEP 9 - PRINT Sum
STEP10 – END
Solution
import java.io.*;
class MatrixSum
{
public static void main(String args[])throws IOException //main function
{
Int a[][]=new int[5][5];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<5;x++) //loop for reading array
{
for(y=0;y<5;y++)
{
z=Integer.parseInt(aa.readLine()); //accepting array element
a[x][y]=z;
}
}
System.out.println("Array -"); //loop for printing array
for(x=0;x<5;x++)
{
for(y=0;y<5;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
for(x=0;x<5;x++) //loop for printing sum of array ele
{
for(y=0;y<5;y++)
{
Sum=Sum+a[x][y];
}
}
System.out.println("Sum of Array elements="+Sum); //displaying sum
}
}
Output:
Question 23
Write a program in java to To Find Sum of Each Column of a Double Dimensional
Array
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM x =0 to x<4 REPEAT STEP 4
STEP 4 - FROM y =0 to y<4 REPEAT STEP 5
STEP 5 - PRINT (a[x][y]+" "
STEP 6 - FROM x =0 to x<4 REPEAT STEP 7 , STEP 9 and STEP 10
STEP 7 - FROM y =0 to y<4 REPEAT STEP 8
STEP 8 - Sum=Sum+a[x][y] ,
STEP 9 - PRINT Sum
STEP 10 - Sum = 0
STEP11 – END
Solution
import java.io.*;
class ColoumnSum
{
public static void main(String args[])throws IOException //main function
{
int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array"); //reading array
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
z=Integer.parseInt(aa.readLine());
a[x][y]=z;
}
}
System.out.println("Array -"); //printing the array in matrix form
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
for(y=0;y<4;y++)
{
for(x=0;x<4;x++)
{
Sum=Sum+a[x][y];
}
System.out.println("S
Sum of column "+(y+1)+" is "+Sum); //printing sum of coloumn
Sum=0;
}
}
}
Output:
Question 24
Write a program in java to Find Sum of Diagonal of a Double Dimensional Array of
4*4 Subscripts
Algorithm
STEP 1- START
STEP 2- INPUT a[]
STEP 3- FROM x =0 to x<4 REPEAT STEP 4
STEP 4- FROM y =0 to y<4 REPEAT STEP 5
STEP 5- PRINT (a[x][y]+" "
STEP 6- FROM x =0 to x<4 REPEAT STEP 7
STEP 7 - Sum=Sum+a[x][y] , y=y+1
STEP 9- PRINT Sum
STEP 10 - Sum = 0
STEP11- END
Solution
import java.io.*;
class DiagonalSum
{
public static void main(String args[])throws IOException //main function
{
int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++) //Reading array
{
for(y=0;y<4;y++)
{
z=Integer.parseInt(aa.readLine());
a[x][y]=z;
}
}
System.out.println("Array -");
for(x=0;x<4;x++) //displaying array
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
y=0;
for(x=0;x<4;x++) //loop for finding sum of diagonal
{
Sum=Sum+a[x][y];
y=y+1;
}
System.out.println("Sum of diagonal is "+Sum); //displaying the sum of diagonal
Sum=0;
}
}
Output:
Question 25
Write a program in java to create a string and replace
all vowels with *
Algorithm
STEP 1 - START
STEP 2 - a = "Computer Applications"
STEP 3 - x= 0
STEP 4 - FROM z =0 to z<a.length() REPEAT STEP 5
STEP 5 - if(a.charAt(z)=='a'||a.charAt(z)=='e'||a.charAt(z)=='i'||a.charAt(z)=='o'||a.charAt(z)=='u‟) THEN a.setCharAt(z,'*')
STEP 6 - PRINT "New String -"+a
STEP 7 – END
Solution
import java.io.*;
class VowelReplace
{
public static void main(String args[])throws IOException //main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a String”);
StringBuffer a=new StringBuffer(br.readLine()); //accepting a string
System.out.println("Original String -"+a);
int z=0;
for(z=0;z<a.length();z++) //loop for replacing vowels with "*"
{
if(a.charAt(z)=='a'|| a.charAt(z)=='e'|| a.charAt(z)=='i'|| a.charAt(z)=='o'|| a.charAt(z)=='u')
a.setCharAt(z,'*');
}
System.out.println("New String -"+a); //displaying the result
}
}
Output:
Question 26
Write a program in java To Convert a Decimal Number to a Roman Numeral
Algorithm
STEP 1 – START
STEP 2 – Enter number num
STEP 3 -- hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}
STEP 4 -- ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
STEP 5 -- unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
STEP 6 – Display hund[num/100] and ten[(num/10)%10] and unit[num%10]
STEP 7 – END
Solution
import java.io.*;
public class Dec2Roman
{
public static void main() throws IOException //main function
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter Number : ");
int num=Integer.parseInt(in.readLine()); //accepting decimal number
String hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
String ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
String unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
/*Displaying equivalent roman number*/
System.out.println("Roman Equivalent= " + hund[num/100] + ten[(num/10)%10] + unit[num%10] );
}
}
Output:
Question 27
Write a program in java To Display the Entered String
in Alphabetical Order.
Algorithm
STEP 1 - START
STEP 2 - str = "" , l = 0
STEP 3 - INPUT string str
STEP 4 - l =str.length()
STEP 5 - FROM i=0 to i<l REPEAT STEP 6
STEP 6 - c[i] = str.charAt(i)
STEP 7 - FROM i=0 to i<l-1 REPEAT STEP 8
STEP 8 - FROM j=0 to i<l-1 REPEAT STEP 9
STEP 9 - temp =c[j], c[j] = c[j+1] , c[j+1] = temp
STEP 10 - FROM i=0 to i<l REPEAT STEP 11
STEP 11 - PRINT c[i]
STEP 12 – END
Solution
public import java.io.*;
class Alpha
{
String str;
int l;
char c[] = new char[100];
public Alpha() //Alpha() constructor
{
str = "";
l =0;
}
public void readword() throws IOException //function to read input string
{
System.out.println("enter word - ");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
l = str.length();
}
public void arrange() //function to arrange string in ascending order
{
int i,j;
char temp;
for(i=0;i<l;i++)
{
c[i]= str.charAt(i);
}
for(i=0;i<l-1;i++) //loops for swapping of characters
{
for(j=0;j<l-1-i;j++)
{
if(c[j] > c[j+1])
{
temp = c[j];
c[j] = c[j+1];
c[j+1] = temp;
}
}
}
}
public void display() //function to display the rearranged string
{
System.out.println();
for(int i=0;i<l;i++)
{
System.out.print(c[i]);
}
}
static void main(String args[]) throws IOException //main function
{
Alpha obj = new Alpha();
obj.readword();
obj.arrange();
obj.display();
}
}
Output:
Question 28
Write a program in java To Check if Entered String is
Palindrome or Not
Algorithm
STEP 1 - START
STEP 2 - INPUT string s
STEP 3 - StringBuffer sb = s
STEP 4 - sb.reverse
STEP 5 - String rev = sb
STEP 6 - IF rev = s GOTO STEP 7 OTHERWISE GOTO STEP 8
STEP 7 - PRINT " Palindrome"
STEP 8 - PRINT " Not Palindrome"
STEP 9 – END
Solution
import java.io.*;
class Palindrome
{
public static void main(String args[]) throws IOException //main function
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string=");
String s = br.readLine(); //accepting the string
StringBuffer sb = new StringBuffer(s);
sb.reverse(); //reversing the string
String rev = new String(sb);
if(s.equalsIgnoreCase(rev)) //checking for palindrome
System.out.println("Palindrome " ); //displaying the result
else System.out.println("Not Palindrome " );
}
}
Output :
Question 29
write a program to check whether the number is
armstrong or not
Algorithm
STEP 1- Start
STEP 2- Store the number in variable „n‟.
STEP 3- Store the copy of the numbering variable z
STEP 4- Extract the digit of the number
STEP 5- Find the sum of digit cube sum and store the sum in
variable s
STEP 6 - check whether sum of the number is equivalent to z or not
STEP 7- end
Solution
import java.io.*;
class Armstrong
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int n,z,s=0,k;
System.out.print("enter any number");
n=Integer.parseInt(obj.readLine());
z=n;
while(n!=0)
{
k=n%10;
s=s+k*k*k;
n=n/10;
}
if(z==s)
System.out.println("The number is armstrong");
else
System.out.println("The number is not armstrong");
}
}
Output:
Question 30
write a program to enter two numbers and print
their hcf & lcm
Algorithm
STEP 1- Start.
STEP 2- find the product of both the number
STEP 3- Run a loop from 1 to product of both numbers
STEP 4- Find the HCF by taking remainder of both the number using
modulas operator
STEP 5- Print the HCF
STEP 6-Divide the product of both the number by HCF to find LCM
STEP 7-End.
Solution
java.io.*;
class Hcf
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int a,b,h=0,l,i,p;
System.out.print("enter first number");
a=Integer.parseInt(obj.readLine());
System.out.print("enter second number");
b=Integer.parseInt(obj.readLine());
p=a*b;
for(i=1;i<=p;i++)
{
if(a%i==0 && b%i==0)
h=i;
}
System.out.println("the HCF is"+h);
l=p/h;
System.out.println("the LCM is"+l);
}
}
Output:
Question 31
write a program to show the use of recursion in
calculation of power
Algorithm
STEP 1- Start
STEP 2- Make a function power with 2 arguments
STEP 3- if the second argument is 0 then the resultant is always 1
STEP 4- if the second argument is 1 then the first number is the
resultant
STEP 5- Multiply the number with itself until the second number
becomes 0 using recursive technique
STEP 6 - End
Solution
import java.io.*;
class Recursion
{
int power(int a,int b)
{
if(b==0)
return 1;
else if(b==1)
return a;
else
return a*power(a,b-1);
}
}
Output:
Question 32
write a program to print the full name and display
the initials of all except the surname and then
print surname completely
Algorithm
STEP 1- Start
STEP 2- Enter name
STEP 3- Extract the surname by using substring function and last
indexof „ „ function
STEP 4. - run the loop and if the space is found print the character
present at „ „+1 position
STEP 5. -Print the surname
STEP 6 - End
Solution
import java.io.*;
class Surname
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
String s,r="";
int l,k,i;
System.out.print("enter any String");
s=obj.readLine();
k=s.lastIndexOf(' ');
r=s.substring(k+1);
System.out.print(s.charAt(0)+".");
for(i=0;i<k;i++)
{
if(s.charAt(i)==' ')
System.out.print(s.charAt(i+1)+".");
}
System.out.print(r);
}
}
Variable Description table
Variable Data Type Purpose
S string To store name
K int To store the last index position of space
W string To store surname
I int For Looping
Output:
Question 33
write a program to input a square matrixwith the
original matrix print its transpose in the matrix
format
Algorithm
STEP 1 - reflect A over its main diagonal (which runs top-left to bottomright)
to obtain AT
STEP 2 - write the rows of A as the columns of AT
STEP 3 - write the columns of A as the rows of AT
STEP 4 -visually rotate A 90 degrees clockwise, and mirror the image
in a vertical line to obtain AT
STEP 5 - Formally, the (i,j) element of AT is the (j,i) element of A.
Solution
import java.io.*;
class Transpose
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[3][3];
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("enter elements");
a[i][j]=Integer.parseInt(obj.readLine());
}
}
System.out.println("ORIGINAL MATRIX\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("TRANSPOSE MATRIX\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[j][i]+"\t");
}
System.out.println();
}
}
}
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int i,j,k=0,c=0,n;
int a[]=new int[100];
System.out.print("enter the limit");
n=Integer.parseInt(obj.readLine());
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
{
a[k]=i;
k++;
}
c=0;
}
for(i=0;i<k;i++)
{
System.out.print(a[i]+"\t");
}
Output
}
}
Question 34
write a program to transfer all prime number from
1 to limit to a single dimensional array and display
the array
Algorithm
STEP 1 -Start
STEP 2 -. Start a loop from 1 to limit
STEP. 3 - Extract all the prime number of that range and transfer to a
Single dimensional array
STEP 4 - Print the array
STEP 5 - End
Variable Description table
Variable Data Type purpose
A int array
I int Looping
J int Looping
K int counter
C int counter
Output:
Question 35
write a program to compute the sum of angles
DATA MEMBERS:
Int deg,min
MEMBER METHODS:
Angle() constructor
void inputAngle( ) to accept the Angle
SumofAngle(Angle,Angle) To compute sum of angle
Algorithm
STEP 1- Start.
STEP 2- Create a non-parameterized constructor to initialized initial value
zero to instance variable.
STEP 3- Create a method inputAngle() to accept the Angle in degree and
minutes.
STEP 4- Create a method SumofAngle(Angle,Angle) to compute the sum
of angles in degree and minutes.
STEP 5- Create a main method to illustrate the above methods.
STEP 6- End.
Solution
import java.io.*;
class Angle
{ int deg,min;
public Angle()
{
deg=min=0;
}
public void inputAngle()throws IOException
{
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
System.out.println("Enter angle in degree");
deg=Integer.parseInt(br.readLine());
System.out.println("Enter angle in minutes");
min=Integer.parseInt(br.readLine());
}
public Angle SumofAngle(Angle T1,Angle T2)
{
Angle ob=new Angle();
ob.deg=T1.deg+T2.deg;
ob.min=T1.min+T2.min;
if(ob.min>=60)
ob.min=ob.min-60;
ob.deg++;
return ob;
}
public void main()throws IOException
{
Angle ob1=new Angle();
Angle ob2=new Angle();
Angle ob3=new Angle();
ob1.inputAngle();
ob2.inputAngle();
ob3=SumofAngle(ob1,ob2);
System.out.println(ob3.deg+" "+ob3.min);
}
}
Output:
Question 36
Write a program To enter year and the no. of days passed in that year. The program
should print the date of the current day. You may assume no. of days is less than365
Algorithm
STEP 1- Start
STEP2- Enter no. of days passed & year.
STEP 3- Make an array a[12] whose each element is the no. of days x as are STEP 5- there in that
month.
STEP 6- Check if year is divisible by 100 &400,then a[1]=29.
STEP 7- Else if year is divisible by 4,a[1]=29.
STEP 8- Take t=0
STEP 9- Generate loop in b from 0 to 12
STEP 10- If t+a[b]>=x,then r=x-t & break.
STEP 11- else t=t+a[b].
STEP 12- End loop.
STEP 13- Print date r,month b+1,year y.
STEP 14- End.
Solution
class Da
{
public static void main(int x,int y)
{
int t=0,r=0,b=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(y%100==0 && y%400==0)
a[1]=29;
else if(y%4==0)
a[1]=29;
for( b=0;b<12;b++)
{
if(t+a[b]>=x)
{
r=x-t;
break;
}
else
t=t+a[b];
}
System.out.println("Date is "+r+"-"+(b+1)+"-"+y);
}
}
Output:
Question 37
Write a PROGRAM to enter a number and check whether it’s a special number or not .
Algorithm
STEP 1- Start
STEP 2- Enter a number and store it in a variable( say n)
STEP 3- Extract a digit from n
STEP 4- Find factorial of the current extracted digit.
STEP 5- Repeat steps 3-4 until all the digits are extracted.
STEP 6- Store the sum of the digits in a variable( say s).
STEP 7- If n==s., the go to 8 else 10
STEP 8- Display “It is a special number”
STEP 9- Go to end
STEP 10- Display “The number is not a special number”
STEP 11- End
Solution
import java.util.*;
class special
{
public long fact(int x)
{
int i,f=1;
for (i=1;i<=x;i++)
{
f=f*i;
}
return f;
}
public static void main(String args[])
{
Scanner obs= new Scanner(System.in);
System.out.println("Enter a number");
special ob= new special();
int a=obs.nextInt();
int r=0,n=a,s=0;
while(a>0)
{
int k=a%10;
r=ob.fact(k);
s=s+r;
a=a/10;
}
if (s==n)
System.out.println(n+" is a special number.");
else
System.out.println(n+" is not a special number.");
}
}
Variable Description table
Variable Data type purpose
A int To store the number
I int As a loop counter
F int To store the factorial
K int To store the extracted digit
S int To store the sum of factorial
Output:
Question 38
Write a program that accepts a string and print a new string having the next
character of the English Alphabet System, in place of each character in the original
string.
Algorithm
STEP 1- Start
STEP 2- Accepted the entered string in variable „s‟.
STEP 3- Stored the length of the entered String in variable „l‟.
STEP 4- For a character of the string, stored its ASCII value in „j‟ using implicit datatype conversion.
STEP 5- Ascii value in „j‟ checked. If it‟s between 65(A) and 89(Y) -- OR – between 97(a) and 121(y), then j=j+1;
STEP 6- If Ascii value in „j‟ is found to be equal to 90(Z) or 122(z) or any other number, then in order to keep the
character same, j=j;
STEP 7- The final ASCII value in „j‟ is again converted into character – using Explicit conversion AND is concated
to the output storing string variable „str‟.
STEP 8- STEP 5 to STEP 8 are repeated until all the characters of the original string get checked.
STEP 9- The variable „str‟ finally holds the output string.
STEP 10- End
Solution
import java.io.*;
class WORD
{
public static void main(String[] args) throws IOException
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER YOUR STRING::");
String s=br.readLine();
char ch;
int i,j,l=s.length();
String str="";
for(i=0;i<l;i++)
{
ch=s.charAt(i);
j=ch;
if((j>=65 && j<90) || (j>=97 && j<122))
j=j+1;
else
j=j;
str=str+(char)j;
}
System.out.println(str);
System.out.println();
}
}
Output:
Question 39
Write a program to enter a string, print the original string with toggled case.
Algorithm
STEP 1- Start
STEP 2- Enter a string
STEP 3- Extract its first alphabet
STEP 4- If the alphabet is in Upper Case then go to 5 else 6
STEP 5- Change the alphabet into Lower Case, go to 7
STEP 6- Change the alphabet into Upper Case
STEP 7- Extract the next alphabet
STEP 8- Repeat steps 4-7 until all the alphabets are toggled
STEP 9- Print the new string
STEP 10- End
Solution
import java.util.*;
class case_convert
{
String s,n;
case_convert()
{
s="";
n="";
}
void input()
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter a string");
s=ob.nextLine();
}
void convert()
{
int l=s.length();
char x;
for(int i=0;i<l;i++)
{
x=s.charAt(i);
if(x>=65 && x<=90)
n=n+(char)(x+32);
else
if(x>=97 && x<=122)
n=n+(char)(x-32);
else
n=n+x;
}
}
void display()
{
System.out.println("\nOriginalString:- "+s+"\nNew String:- "+n);
}
public static void main(String args[])
{
case_convert ob1= new case_convert();
ob1.input();
ob1.convert();
ob1.display();
}
}
Output:
Question 40
Write a program to enter a number and find and print its primorial. (Primorial of a
number is defined as the product of all prime numbers less than or equal to the
number.)
Algorithm
STEP 1- Start
STEP 2- Enter a number and store it in a variable(say n)
STEP 3- Take a=2, prod=1
STEP 4- If a is a prime number then multiply it with prod, and store the result in STEP 5- prod else go to 5
STEP 6- Increase a by 1
STEP 7- Repeat steps 4-5 until value of a is equal to n
STEP 8- Print the value of prod with suitable statement
STEP 9- End
Solution
import java.util.*;
class primorial
{
public static void main(String args[])
{
Scanner ob= new Scanner(System.in);
System.out.println("Enter the limit");
int p= ob.nextInt();
int i,prod=1,c;
for(i=2;i<=p;i++)
{
c=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
prod=prod*i;
}
System.out.println("Primorial of "+p+" is "+prod);
}
}
Output:
BIBLIOGRAPHY
www.google.com
ISC Understanding Computer Science Class 12th