ISC 12th
Computer project
|
January 3
2016
|
The
solution of five questions are giving here .first two questions have
solution along with Algorithm
|
@ shuklasanjeev15@gmail.com
|
|
|
|
Question :
A bank intends to design a program to display the
denomination of an input amount, upto 5 digits. The available denomination with
the bank are of rupees 1000, 500, 100, 50, 20, 10 ,5, 2 and 1.
Design a program to accept the amount from the user
and display the break-up in descending order of denominations. (i,e preference
should be given to the highest denomination available) along with the total
number of notes. [Note: only the denomination used should be displayed]. Also
print the amount in words according to the digits.
Example 1:
INPUT: 14836
OUTPUT: ONE FOUR EIGHT THREE SIX
DENOMINATION:
1000 X 14 =14000
500 X 1 =500
100 X 3 =300
50 X 1 =50
5 X 1 =5
1 X 1 =1
Algorithm
Step-1: Enter the amount in n
Step-2: Store the basic denominations
(1000, 500, 100, 50, 20, 10, 5, 2, 1) in
an array
Step-3: Run a loop to access the array
Step-4: Divide the amount n by each value
in the array to get the quotient
Step-5: If the quotient is not zero,
display the denomination and update amount.
Step-6: To display the denomination digits in words, create an array and
store the digits in
words
Step-7: Now run a while loop to reverse
the original number.
Step-8: Run another loop and extract each
digit of the reversed number.
Step-9: Print each digit in words using
the array just created.
Step-10: End
Solution
import java.util.*;
public class bank
{
public static void main(String args[])
throws InputMismatchException{
Scanner scan=new Scanner(System.in);
int amt;
System.out.print("Enter a five-digit
amount : ");
amt=scan.nextInt();
if(amt>99999){
System.out.println("INVALID
AMOUNT.");}
else{
int a[]={1000,500,100,50,20,10,5,2,1};int
i,p,r,b,t,count=0,x;
p=amt;
x=amt;
String ones[]={"one","two","three","four","five",
"six","seven","eight","nine"};
r=0;
while(p>0){
r=r*10+p%10;
p/=10;
}
while(r>0){
b=r%10;
System.out.print(ones[b-1].toUpperCase()+"
");
r/=10;
}
System.out.println();
for(i=0;i< a.length;i++){
t=amt/a[i];
if(t!=0){
System.out.println(a[i]+"X"+t+"="+(t*a[i]));
amt=amt%a[i];
count=count+t;
}}
System.out.println("Number of
notes:"+count);
System.out.println("Total
amount:"+x);
}}
}
Question :
Write a program to enter a sentence and
print it in ascending order of its word lengths.
A sentence may either terminate with
a period (.).................................................
Algorithm
Step-1: Enter a string
Step-2: Find the length of the string
Step-3: Create a string array
Step-4: Run a loop ‘i’ to access the string
Step-5: Extract word from the string and store it in the string array
Step-6: Continue the process until all the words are stored in the array
Step-7: Now run a loop to access the array
Step-8: Run two loops for sorting, say i and j
Step-9: If length of the ith element
is greater than the length of the jth element
swap them.
Step-10: Continue the process till the entire array is sorted
Step-11: Display the words in the array along with a blank space
Step-12: End
Solution
import java.io.*;
public class sorting
{
public static void
main()throws IOException
{
BufferedReader br=new
BufferedReader(
new
InputStreamReader(System.in));
System.out.println("Enter the string : ");
String
str=br.readLine();
int l=str.length();
String a[]=new
String[l];
int p=0,x=0,i,j;
String t;
for(i=0;i< l;i++)
{
char ch=str.charAt(i);
if(ch==' '||ch=='.')
{
a[x]=str.substring(p,i);
System.out.print(a[x]+" ");
x++;
p=i+1;
}
}
System.out.println();
for(i=0;i< x;i++)
{
for(j=0;j<
x-i-1;j++)
{
if(a[j].length() >
a[j+1].length())
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
String s=a[0];
char ch=s.charAt(0);
System.out.print(Character.toUpperCase(ch));
System.out.print(s.substring(1));
for(i=1;i< x;i++)
{
System.out.print("
"+a[i]);
}
System.out.print(".");
}}
Question :………………………
import java.util.*;
public class day_date_year
{
public static void main(String args[])
throws InputMismatchException{
Scanner scan=new Scanner(System.in);
System.out.println("ENTER DAY NUMER(>=1 AND <=366) :
");
int day_number=scan.nextInt();
System.out.println("ENTER YEAR(4 DIGIT) : ");
int year=scan.nextInt();
System.out.println("ENTER DATE AFTER(N)(>=1 AND <=100)
: ");
int n=scan.nextInt();
if(day_number<1 || day_number>366)
System.out.println("INVALID DAY NUMBER.");
else if(year<1000 || year >9999)
System.out.println("INVALID YEAR");
else if(n<1 || n>100)
System.out.println("INVALID DATE AFTER VALUE.");
else{
String
month_names[]={"JANUARY", "FEBRUARY","MARCH",
"APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER",
"OCTOBER","NOVERMBER","DECEMBER"};
int
month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int i, day,
month,day_after;
String suffix;
//IF IT IS A LEAP YEAR
FEBRURAY SHOULD HAVE 29 DAYS
if(year%400==0 ||
(year%100!=0 && year%4==0))
month_days[1]=29;
i=0;
//FIND THE DATE
CORRESPONDING TO THE DAY NUMBER
day=day_number;
while(day>month_days[i])
{
day-=month_days[i];
i++;
}
month=i;
//ADD SUFFIX AS PER
THE DAY
if(day%10==1
&& day/10!=1)
suffix="ST";
else if(day%10==2
&& day/10!=1)
suffix="ND";
else if(day%10==3
&& day/10!=1)
suffix="RD";
else
suffix="TH";
System.out.println("OUTPUT:");
//FIRST PART OF THE
OUTPUT
System.out.println(day+suffix+" "+
month_names[month]+" "+year);
//TO CALCULATE DATE
AFTER N DAYS
day_after=day_number+n;
i=0;
while(day_after>month_days[i])
{
day_after-=month_days[i];
i++;
if(i==12){
i=0;
year++;
if(year%400==0
|| (year%100!=0 && year%4==0))
month_days[1]=29;
}
}
day=day_after;
month=i;
//ADD SUFFIX AS PER
THE DAY
if(day%10==1
&& day/10!=1)
suffix="ST";
else if(day%10==2
&& day/10!=1)
suffix="ND";
else if(day%10==3
&& day/10!=1)
suffix="RD";
else
suffix="TH";
//SECOND PART OF THE
OUTPUT
System.out.println(day+suffix+"
"+
month_names[month]+" "+year);
}
}
}
Question :
mobius Function
program.........................
import java.util.*;
public class mobiusFn
{
int n;
mobiusFn()
{
n = 0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
n = sc.nextInt();
}
int
primeFac()
{
int a=n, i=2, m=0, c=0, f=0;
while(a > 1) // loop to generate prime factors
{
c = 0; // variable to store frequency of every prime factor
while(a%i == 0) // if 'i' is a prime factor
{
c++; // counting frequency of
'i'
f++; // counting no of prime
factors
a=a/i;
}
i++;
if(c > 1) // returning '0' if prime factors are repeated
return 0;
}
return f; // returning no. of prime factors
}
void display() // function to display value of mobius function
{
int mob,x;
if(n == 1) // condition 1
mob = 1;
else
{
x = primeFac();
if(x == 0) // condition 2
mob = 0;
else // condition 3
mob = (int)Math.pow(-1,x);
}
System.out.println("Value of Mobius Function : "+mob);
}
public static void main(String args[])
{
mobiusFn ob = new mobiusFn();
ob.input();
ob.display();
}}
Question :palindrome program……………………….
import java.math.BigInteger;
import java.util.Scanner;
public class palindrome
{
public static void main(String[] args) {
int step = 0;
BigInteger num1, num2, sum;
String original, reverse, str1, str2;
Scanner in = new Scanner(System.in);
System.out.println("Please enter an integer");
original = in.nextLine();
while (!isNumeric(original)) {
System.out.println("Please enter an integer");
original = in.nextLine();
}
str1 = original;
reverse = str2 = reverseString(original);
while (!(isPalindrome(str1, str2))) {
num1 = new BigInteger(str1);
num2 = new BigInteger(str2);
sum = num1.add(num2);
step++;
if (step > 10000) {
System.out.print(original +
" is a Lychrel numbers");
break;
}
str1 = String.valueOf(sum);
reverse = str2 = reverseString(str1);
}
// Otherwise the Lychrel number would also print like a palindrome
if (isPalindrome(reverse, reverseString(reverse))) {
System.out.print(original + " gets palindromic after " + step
+ " steps: " + reverse);
}
}
public static boolean isNumeric(String str) {
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) return false;
}
return true;
}
/*
*
Go through the whole string
*
add the last character to an empty string
*
you get the reversed
*
*/
public static String reverseString(String str) {
String reverse = "";
int length = str.length();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + str.charAt(i);
return reverse;
}
//
If they are same they are Palindrome
public static boolean isPalindrome(String orig, String rev) {
return (orig.equals(rev)); }}
To be
continued...................................