Sie sind auf Seite 1von 35

Solved programs

Question:

Write a Program in Java to input 2 numbers and find their Greatest Common Divisor (GCD).
Note: If the 2 numbers are 54 and 24, then the divisors (factors) of 54 are: 1, 2, 3, 6, 9, 18, 27,
54.
Similarly the divisors (factors) of 24 are: 1, 2, 3, 4, 6, 8, 12, 24.
The numbers that these two lists share in common are the common divisors (factors) of 54 and
24: 1, 2, 3, 6.
The greatest (highest) of these is 6. That is the greatest common divisor or the highest common
factor of 54 and 24.
import java.io.*;
class Hcf
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the First no : ");
int n1=Integer.parseInt(br.readLine());
System.out.print("Enter the Second no : ");
int n2=Integer.parseInt(br.readLine());
int hcf=0;
int min = Math.min(n1,n2);
for(int i=min; i >= 1; i--)
{
if(n1%i == 0 && n2%i == 0)
{
hcf = i;
break;
}
}
System.out.print("\nThe hcf of "+n1+" and "+n2+" = "+hcf);
}

Output:
Enter the First no : 54
Enter the Second no : 24
The hcf of 54 and 24 = 6

Question:
Write a program to input a word from the user and remove the duplicate characters present in it.
Example:
INPUT
OUTPUT abc
INPUT
OUTPUT javforschl
INPUT
OUTPUT Misp

abcabcabc

javaforschool
Mississippi

Programming Code:
8 import java.io.*;
9 class RemoveDupChar
{
1
public static void main(String args[])throws IOException
0
{
BufferedReader br = new BufferedReader(new
11
InputStreamReader(System.in));
1
System.out.print("Enter any word : ");
2
String s = br.readLine();
1
int l = s.length();
3
char ch;
String ans="";
1
4
for(int i=0; i<l; i++)
1
{
5
ch = s.charAt(i);
1
if(ch!=' ')
ans = ans + ch;
6
s
=
s.replace(ch,' '); //Replacing all occurrence of the current
1
character by a space
7
}
1
8
System.out.println("Word after removing duplicate characters : " +
1 ans);
}
9
}
2
0
2
1
2
2
2

3
2
4
2
5
2
6
2
7
2
8
2
9
Output:
Example
Enter
any
word
Word after removing duplicate characters : Misp
Example
Enter
any
word
Word after removing duplicate characters : Atiude

1:
Mississippi

2:
Attitude

Question:
Write a Program in Java to input a number and check whether it is a Disarium Number or not.
Note: A number will be called DISARIUM if sum of its digits powered with their respective
position is equal to the original number.
For example 135 is a DISARIUM
(Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc)
Programming Code:

4
5
6
7
8
9
1

import java.io.*;
class Disarium
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
System.out.print("Enter a number : ");
int n = Integer.parseInt(br.readLine());
int copy = n, d = 0, sum = 0;
String s = Integer.toString(n); //converting the number into a
String
int len = s.length(); //finding the length of the number i.e.

0
11
1
2
1
3
1
4
1
5
1
6
1
no.of digits
7
1
while(copy>0)
8
{
1
d = copy % 10; //extracting the last digit
sum = sum + (int)Math.pow(d,len);
9
len--;
2
copy = copy / 10;
0
}
2
1
if(sum == n)
System.out.println(n+" is a Disarium Number.");
2
else
2
System.out.println(n+" is not a Disarium Number.");
2
}
3
}
2
4
2
5
2
6
2
7
2
8

Output:
Enter a Number : 135
135 is a Disarium Number.

Enter a Number : 219


219 is not a Disarium Number.
Enter a Number : 89
89 is a Disarium Number.
Question:
Define a class named movieMagic with the following description:
Instance variables/data members:
int year

to store the year of release of a movie

String title

to store the title of the movie.

float rating

to store the popularity rating of the movie. (minimum rating = 0.0 and
maximum rating = 5.0)
Member Methods:
(i)
movieMagic()
String data member to .

Default constructor to initialize numeric data members to 0 and

(ii)

To input and store year, title and rating.

void accept()

(iii)
void display()
as per the table below.

To display the title of a movie and a message based on the rating

Rating

Message to be displayed

0.0 to 2.0

Flop

2.1 to 3.4

Semi-hit

3.5 to 4.5

Hit

4.6 to 5.0

Super Hit

Write a main method to create an object of the class and call the above member methods.
Programming Code:
1
2
3

import java.io.*;
class movieMagic
{
int year;

4
String title;
5
float rating;
6
movieMagic() // default constructor
7
{
8
year = 0;
9
rating = 0.0f; // notice the 'f'
10
title = "";
11
}
12
void accept() throws IOException
13
{
14
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15
System.out.print("Enter the title of the movie : ");
16
title = br.readLine();
System.out.print("Enter the year of its release : ");
17
year = Integer.parseInt(br.readLine());
18
System.out.print("Enter the movie rating : ");
19
rating = Float.parseFloat(br.readLine());
20
}
21
22
void display()
{
23
System.out.println("The
title
of the movie is : "+title);
24
if( rating >= 0.0 && rating <= 2.0 ) {
25
System.out.println("The movie was a Flop");
26
} else if( rating >= 2.1 && rating <= 3.4 ) {
System.out.println("The movie was a Semi-hit");
27
} else if( rating >= 3.5 && rating <= 4.5 ) {
28
System.out.println("The movie was a Hit");
29
} else if( rating >= 4.6 && rating <= 5.0 ) {
30
System.out.println("The movie was a Super Hit");
31
} else {
32 System.out.println("Enter a valid movie rating in between 0.0 and 5.0");
}
33
}
34
35
public static void main(String args[]) throws IOException
36
{
37 movieMagic ob = new movieMagic(); // creating object of the class movieMagic
38
ob.accept();
ob.display();
39
}
40
}

Output:
Enter the title of the movie : Spiderman
Enter the year of its release : 2013
Enter the movie rating : 4.2

The title of the movie is : Spiderman


The movie was a Hit
Question:
A special two-digit number is such that when the sum of the digits is added to the product of its
digits, the result is equal to the original two-digit number.
Example:
Consider the number 59.Sum of digits = 5+9=14
Product of its digits = 5 x 9 = 45
Sum of the digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its
digits. If the value is equal to the number input, output the message special-two digit number
otherwise, output the message Not a special two-digit number.
Programming Code:
1 import java.io.*;
2 class{ Special_Q5_ICSE2014
3
public static void main(String args[])throws IOException
4
{
BufferedReader br=new BufferedReader (new
5
InputStreamReader(System.in));
6
System.out.print("Enter a 2 digit number : ");
7
int n = Integer.parseInt(br.readLine());
8
9
int first, last, sum, pro;
1
if(n<10 || n>99) //Checking whether entered number is 2 digit or
0 not
System.out.println("Invalid Input! Number should have 2
11
digits only.");
1
else
2
{
first = n/10; //Finding the first digit
1
last = n%10; //Finding the last digit
3
sum = first + last; //Finding the sum of the digits
1
pro = first * last; //Finding the product of the digits
4
1
if((sum + pro) == n)
5
{
System.out.println("Output : The number "+n+" is a
1
Special
Two-Digit
Number.");
6
}
1
else
7
{
1
System.out.println("Output : The number is Not a
8 Special Two-Digit Number.");
}
1
}

9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0

}
}

Output:
1. Enter a 2 digit number : 79
Output : The number 79 is a Special Two-Digit Number.
2. Enter a 2 digit number : 47
Output : The number is Not a Special Two-Digit Number.
Question:
Write a program to input a word from the user and remove the consecutive repeated characters
by replacing the sequence of repeated characters by its single occurrence.
Example:
INPUT
OUTPUT Java
INPUT
OUTPUT Heisgoing

Jaaavvvvvvvvaaaaaaaaaaa

Heeeiiiissggoiinggg

Programming Code:
1
2
3
4
5
6
7
8
9 import java.io.*;
RemoveRepChar
1 class
{
0
public static void main(String args[])throws IOException
11
{
BufferedReader br=new BufferedReader(new
1
InputStreamReader(System.in));
2
System.out.print("Enter any word: "); // Inputting the word
1
String s = br.readLine();
3
1
s = s + " "; // Adding a space at the end of the word
4
int l=s.length(); // Finding the length of the word
String ans=""; // Variable to store the final result
1
char ch1,ch2;
5
1
for(int i=0; i<l-1; i++)
6
{
1
ch1=s.charAt(i); // Extracting the first character
ch2=s.charAt(i+1); // Extracting the next character
7
1
8 // Adding the first extracted character to the result if the current and the
1 next characters are different
9
if(ch1!=ch2)
2
{
0
ans = ans + ch1;
}
2
}
1
System.out.println("Word after removing repeated characters =
2 "+ans); // Printing the result
2
}
2 }
3
2
4
2
5
2
6

Output:
Example
Enter
any
word:
Word after removing repeated characters = Java
Example
Enter
any
word:
Word after removing repeated characters = isfun

1:
Jaaavvvvvvvvaaaaaaaaaaa
2:
iiiiiisssssfffffffffffffuunnnnn

Question:
Write a program to input a number. Count and print the frequency of each digit present in that
number. The output should be given as:
Sample Input: 44514621
Sample Output:
=====================
Digit
Frequency
=====================
1
2
2
1
4
3
5
1
6
1
Programming Code:
1 import java.io.*;
2 class Digit_Freq
3 {
public static void main(String args[])throws IOException
4
{
5
BufferedReader br=new BufferedReader(new
6 InputStreamReader(System.in));
System.out.print("Enter any number : ");
7
int n = Integer.parseInt(br.readLine());
8
9
int freq[]=new int[10]; //array for storing frequency of all digits
1
0
for(int i=0; i<10; i++)
11
{
freq[i]=0; //intializing the count of every digit with '0'
1
}
2
1
/*Note: Frequency of digit '0' is stored in freq[0], frequency of
3 '1' in freq[1] and so on*/
1
4
System.out.println("Output:");
System.out.println("===================="); //this is just for
1

5
1
6
1
7
1
8
1
9
2
0
2
1
2 styling the look of the output
2
System.out.println("Digit\tFrequency");
2
System.out.println("====================");
3
int d;
2
while(n>0)
4
{
2
d=n%10; //extracting digit from the end
5
freq[d]++; //increasing the frequency of that digit.
n=n/10;
2
}
6
2
for(int i=0; i<10; i++)
7
{
2
if(freq[i]!=0) //printing only those digits whose count is
8 not '0'
System.out.println("
"+i+"\t
"+freq[i]);
2
}
9
}
3
}
0
3
1
3
2
3
3
3
4
3
5
3
6
Output:

Enter any number : 937825770


Output:
=====================
Digit
Frequency
=====================
0
1
2
1
3
1
5
1
7
3
8
1
9
1
Question:
Write a program to input a string (word). Convert it into lowercase letters. Count and print the
frequency of each alphabet present in the string. The output should be given as:
Sample Input: Alphabets
Sample Output:
==========================
Alphabet
Frequency
==========================
a
2
b
1
e
1
h
1
l
1
p
1
s
1
t
1
Programming Code:
1 import java.io.*;
AlphabetFreq
2 class
{
3
public static void main(String args[])throws IOException
4
{
BufferedReader br=new BufferedReader(new
5
InputStreamReader(System.in));
6
System.out.print("Enter any string: ");
7
String s = br.readLine();
8
9
s=s.toLowerCase(); //converting the string into lowercase
1
int l=s.length(); //finding the length of the string
0
char ch;
11
System.out.println("Output:");

1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4

System.out.println("=========================="); //this is just for


styling the look of the output
System.out.println("Alphabet\tFrequency");
System.out.println("==========================");
/* Counting frequency of alphabets begins below */
int count=0;
for(char i='a'; i<='z'; i++)
{
count = 0;
for(int j=0; j<l; j++)
{
ch=s.charAt(j); //extracting characters of the string
one by one
if(ch==i) //first checking the whole string for 'a',
then 'b' and so on
count++; //increasing count of those aplhabets which
are present in the string
}
if(count!=0)//printing only those alphabets whose count is
not '0'
{
System.out.println(i+"\t\t"+count);
}
}
}
}

3
5
Output:
Enter any string: ilovejavaforschool
Output:
==========================
Alphabet
Frequency
==========================
a
2
c
1
e
1
f
1
h
1
i
1
j
1
l
2
o
4
r
1
s
1
v
2
Question:
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 ..
Examples:
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
2. 4937775
Prime factors are 3, 5, 5, 65837
Sum of the digits are (4+9+3+7+7+7+5) = 42
Sum of the digits of the factors (3+5+5+(6+5+8+3+7)) = 42
Write a program to input a number and display whether the number is a Smith number or not.

Sample data:
Input

94

Output

SMITH Number

Input

102

Output

NOT SMITH Number

Input

666

Output

SMITH Number

Input

999

Output

NOT SMITH Number

Programming Code:
1 import java.io.*;
2 class Smith
{
3
static BufferedReader br=new BufferedReader(new
4 InputStreamReader(System.in));
//function for finding sum of digits
5
int sumDig(int n)
6
{
7
int s=0;
8
while(n>0)
9
{
s=s+n%10;
1
n=n/10;
0
}
11
return s;
1
}
2
//function for generating prime factors and finding their sum
1
int sumPrimeFact(int n)
3
{
1
int i=2, sum=0;
4
while(n>1)
1
{
if(n%i==0)
5
{
1
sum=sum+sumDig(i); //Here 'i' is the prime factor of 'n' and
6 we are finding its
sum
1
n=n/i;
7
}
else
1
i++;
8
}
1
return sum;
9
}
2
0 public static void main(String args[]) throws IOException
2 {
Smith ob=new Smith();
1
System.out.print("Enter a Number : ");
2
int n=Integer.parseInt(br.readLine());
2
int a=ob.sumDig(n);// finding sum of digit

2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3 }
5 }
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3

int b=ob.sumPrimeFact(n); //finding sum of prime factors


System.out.println("Sum of Digit = "+a);
System.out.println("Sum of Prime Factor = "+b);
if(a==b)
System.out.print("It is a Smith Number");
else
System.out.print("It is Not a Smith Number");

Output:

1. Enter
Sum
Sum
of
It is a Smith Number

a
of
Prime

2. Enter
a
Sum
of
Sum
of
It is Not a Smith Number
3. Enter
Sum
Sum
of
It is a Smith Number

Number
Digit

Number
Digit

a
of

4937775
42
42

:
=
Factor

Number
Digit
Prime

102
3
13

=
Factor

Prime

94
13
13

:
=
Factor

Question:

Write a Program in Java to input a number in Decimal number system and convert it into its
equivalent number in the Hexadecimal number system.
Solution:
import java.io.*;
class Dec2Hex
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
System.out.print("Enter a decimal number : ");
int n=Integer.parseInt(br.readLine());

7
8
9
1
0
11
1
int r;
2
String s=""; //variable for storing the result
1
//array storing the digits (as characters) in a hexadecimal number
3
system
1
char
4 dig[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
1
5
while(n>0)
{
1
r=n%16; //finding remainder by dividing the number by 16
6
s=dig[r]+s; //adding the remainder to the result
1
n=n/16;
7
}
1
System.out.println("Output = "+s);
}
8
1 }
9

2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
Output:
Enter a decimal number : 47
Output = 2F
Enter a decimal number : 1243
Output = 4DB
Question:

Write a Program in Java to input a number in Decimal number system and convert it into its
equivalent number in the Octal number system.
Solution:
1
2
3
4
5
6
7
8
9

import java.io.*;
class Dec2Oct
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
System.out.print("Enter a decimal number : ");
int n=Integer.parseInt(br.readLine());
int r;

1
0
11
1
2
1
3
1
String s=""; //variable for storing the result
4
//array storing the digits (as characters) in the octal number
1
system
5
char dig[]={'0','1','2','3','4','5','6','7'};
1
6
while(n>0)
1
{
r=n%8; //finding remainder by dividing the number by 8
7
s=dig[r]+s; //adding the remainder to the result and
1
reversing at the same time
8
n=n/8;
1
}
9
System.out.println("Output = "+s);
}
2
0 }
2
1
2
2
2
3
Output:
Enter a decimal number : 25
Output = 31
Enter a decimal number : 465
Output = 721
Question:

Write a Program in Java to input a number in Decimal number system and convert it into its
equivalent number in the Binary number system.
Solution:
1 import java.io.*;
Dec2Bin
2 class
{

3
4
5
6
7
8
9
public static void main(String args[])throws IOException
1
{
0
BufferedReader br=new BufferedReader (new
11InputStreamReader(System.in));
System.out.print("Enter a decimal number : ");
1
int n=Integer.parseInt(br.readLine());
2
1
int r;
3
String s=""; //variable for storing the result
1
4
char dig[]={'0','1'}; //array storing the digits (as characters) in
1 a binary number system
5
while(n>0)
1
{
6
r=n%2; //finding remainder by dividing the number by 2
1
s=dig[r]+s; //adding the remainder to the result and
7 reversing at the same time
n=n/2;
1
}
8
System.out.println("Output = "+s);
1
}
9 }
2
0
2
1
2
2
Output:
Enter a decimal number : 25
Output = 11001
Enter a decimal number : 47
Output = 101111
Enter a decimal number : 6
Output = 110
Question:

Write a Program in Java to input a number and check whether it is a Duck Number or not.
Note: A Duck number is a number which has zeroes present in it, but there should be no zero
present in the beginning of the number. For example 3210, 7056, 8430709 are all duck numbers
whereas 08237, 04309 are not.
Solution:
1 import java.io.*;
2 class Duck_No
{
3
public static void main(String args[])throws IOException
4
{
BufferedReader br=new BufferedReader(new
5
6 InputStreamReader(System.in));
System.out.print("Enter any number : ");
7
String n=br.readLine(); //inputting the number and storing it in a
8 String
9
1
int l=n.length(); //finding the length (number of digit) of the
number
0
int c=0; //variable for counting number of zero digits
11
char ch;
1
2
for(int i=1;i<l;i++)
1
{
ch=n.charAt(i); //extracting each digit and checking whether it
3
1 is a '0' or not
if(ch=='0')
4
c++;
1
}
5
1
char f=n.charAt(0); //taking out the first digit of the inputted
number
6
1
if(c>0 && f!='0')
7
System.out.println("It is a duck number");
1
else
8
System.out.println("It is not a duck number");
1
}
9 }
2
0
2
1
2
2
2
3
2
4

2
5
2
6
2
7
Output:
Enter any number : 4013
It is a duck number
Enter any number : 0134
It is not a duck number
Enter any number : 67300987
It is a duck number
This is the Java programming code written in BlueJ which swaps the values of two Strings
without using any third (temp) variable.
Programming Code:
1 import java.io.*;
Swap_Strings
2 class
{
3
public static void main(String args[])throws IOException
4
{
BufferedReader br=new BufferedReader (new InputStreamReader
5
(System.in));
6
System.out.print("Enter the 1st String : ");
7
String s1=br.readLine();
8
int len1=s1.length();
9
1
System.out.print("Enter the 2nd String : ");
String s2=br.readLine();
0
11
System.out.println("-------------------------------");
1
System.out.println("Strings Before Swapping : ");
2
System.out.println("1st String = "+s1);
1
System.out.println("2nd String = "+s2);
3
/*Swapping Process Begins*/
1
s1=s1+s2;
4
s2=s1.substring(0,len1);
1
s1=s1.substring(len1);
5
/*Swapping Process Ends*/
1
6
System.out.println("-------------------------------");

1
7
1
8
1
9
2
0
2
1
2
2
}
2
3
2
4
2
5
2
6

System.out.println("Strings After Swapping : ");


System.out.println("1st String = "+s1);
System.out.println("2nd String = "+s2);

Output:
Enter the 1st String : Java For
Enter the 2nd String : School is Fun
Strings Before Swapping :
1st String = Java For
2nd String = School is Fun
Strings After Swapping :
1st String = School is Fun
2nd String = Java For
Question:
Write a program to find the shortest and the longest word in a sentence and print them along with
their length.
Sample Input: I am learning Java
Sample Output:
Shortest word = I
Length = 1
Longest word = learning
Length = 8

Solution:
1 import java.io.*;
2 class Short_long_word
{
3 public static void main(String args[])throws IOException
4
{
BufferedReader br=new BufferedReader(new
5
InputStreamReader(System.in));
6
System.out.print("Enter any sentence : "); //inputting the sentence
7
String s=br.readLine();
8
9
s=s+" "; //adding a space at the end, to extract the last word also
1
int len=s.length(); //finding the length of the sentence
String x="",maxw="",minw="";
0
char ch;
11
int p,maxl=0,minl=len;
1
2
for(int i=0;i<len;i++)
1
{
ch=s.charAt(i); //extracting characters of the string one at a
3
1 time
if(ch!=' ')
4
{
1
x=x+ch; //adding characters to form word if character is not
5 space
}
1
else
6
{
1
p=x.length();
7
1
if(p<minl) //checking for minimum length
8
{
minl=p;
1
minw=x;
9
}
2
0
if(p>maxl) //checking for maximum length
2
{
maxl=p;
1
maxw=x;
2
}
2
x=""; //emptying the temporary variable to store next word
2
}
3
}
System.out.println("Shortest word = "+minw+"nLength = "+minl);
2
System.out.println("Longest word = "+maxw+"nLength = "+maxl);
4
}
2 }
5
2
6
2
7

2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
Output:
Enter any sentence : I am learning Java
Shortest word = I
Length = 1
Longest word = learning
Length = 8
Question:
A bank intends to design a program to display the denomination of an input amount, up to 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 denomination. (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].
Example:

INPUT: 14788
OUTPUT:
DENOMINATIONS:
1000 x 14 = 14000
500
x 1
= 500
100
x 2
= 200
50
x 1
= 50
20
x 1
= 20
10
x 1
= 10
5
x 1
= 5
2
x 1
= 2
1
x 1
= 1

TOTAL
= 14788

Total Number of Notes = 23


Solution:
1 import java.io.*;
2 class Denominations
{
3 public static void main(String args[])throws IOException
4 {
5 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
6 int den[]={1000,500,100,50,20,10,5,2,1}; //storing all the denominations in
an array
7 System.out.print("Enter any Amount: "); //Entering an amount
8 int amount=Integer.parseInt(br.readLine());
9
1 int copy=amount; //Making a copy of the amount
0 int totalNotes=0,count=0;
11
1 System.out.println("\nDENOMINATIONS: \n");
2
for(int i=0;i<9;i++) //Since there are 9 different types of notes, hence we
1 check for each note.
3 {
1 count=amount/den[i]; // counting number of den[i] notes
4 if(count!=0) //printing that denomination if the count is not zero
1 {System.out.println(den[i]+"\tx\t"+count+"\t= "+den[i]*count);
5 }
1 totalNotes=totalNotes+count; //finding the total number of notes
6 amount=amount%den[i]; //finding the remaining amount whose denomination is
1 to be found
}
7

1
8
1
9
2
0
2
1
2
2
2 System.out.println("--------------------------------");
3 System.out.println("TOTAL\t\t\t= "+copy); //printing the total amount
2 System.out.println("--------------------------------");
4 System.out.println("Total Number of Notes\t= "+totalNotes); //printing the
2 total number of notes
}
5 }
2
6
2
7
2
8
2
9
3
0
Output:
Enter any Amount: 14856
DENOMINATIONS:
1000 x 14 = 14000
500
x 1
= 500
100
x 3
= 300
50
x 1
= 50
5
x 1
= 5
1
x 1
= 1

TOTAL
= 14856

Total Number of Notes = 21


Question:

Write a program in JAVA to find the Prime factors of a number.


Prime factors of a number are those factors which are prime in nature and by which the number
itself is completely divisible (1 will not be taken as prime number).
Few
Prime
Factors
Prime Factors of 6 are 2, 3

such
of

24

are

numbers
2,

2,

2,

are:
3

Solution:
10 import java.io.*;
11 class PrimeFactors
{
12 public static void main(String args[]) throws IOException
13 {
14 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
15 int n;
a Number : ");
16 System.out.print("Enter
n=Integer.parseInt(br.readLine());
17
18 System.out.print("The Prime Factors of "+n+" are : ");
19
20 int i=2;
21 while(n>1)
22 {
if(n%i == 0)
23
{
24
System.out.print(i+" ");
n=n/i;
25
}
26
else
27
i++;
28 }
29 }
30 }
Output:
Enter
a
The Prime Factors of 378 are: 2 3 3 3 7

Number

Program on Decimal to Roman Number Conversion


[Method 2]
Solution:

378

1 import java.io.*;
2 class Dec2Roman_Method2
{
3
public static void main(String args[]) throws IOException
4 {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
5
int num;
6
String str="";
7
System.out.print("Enter a Number : ");
8
num=Integer.parseInt(br.readLine()); //accepting decimal number
9
1
/*Arrays storing the unique symbols of Roman Number System
and also the corresponding decimal equivalents in the second array*/
0
String
roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX",
11
"V", "IV", "I"};
1
int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
2
1
if(num>0 && num<4000) //checking whether the number entered is
3 within the range [1-3999]
{
1
(int i=0; i<13; i++) // i<13 because the total unique numbers
4 stored in theforabove
array=13
1
{
5
/*The while loop is for printing repeated digits like XXX
1 for 30 etc
and is also calculating the equivalent Roman number by
6
adding the corresponding
1
Roman Digits from the Array to the String str*/
7
while (num >= decimal[i])
1
{
num = num-decimal[i];
8
str = str+roman[i];
1
}
9
}
2
System.out.println("Roman Equivalent = "+str); //Printing the Roman
0 equivalent
}
2
1
2 range*/ /*Displaying an error message if the number entered is out of
2
else
2
System.out.println("nYou entered a number out of Range.nPlease enter
3 a number in the range [1-3999]");
2 }
}
4
2
5
2
6
2
7
2
8

2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
Output:
Enter
a
Roman Equivalent = DCCCLXIII

Number

863

Question:

Write a program in Java to find the Roman equivalent of any Decimal number entered by the
user. [The number entered should be within the Range 1-3999]
Solution:
1 import java.io.*;
Dec2Roman_Method1
2 class
{
3 public static void main(String args[]) throws IOException
4 {
5 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
6 System.out.print("Enter a Number : ");
int num=Integer.parseInt(br.readLine()); //accepting decimal number
7
8 if(num>0 && num<4000) //checking whether the number entered is within the
9 range [1-3999]
1 {
0
11/*Saving the Roman equivalent of the thousand, hundred, ten and units place
1 of a decimal number*/
String thou[]={"","M","MM","MMM"};
2 String hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};

1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0

String ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
String unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
/*Finding the digits in the thousand, hundred, ten and units place*/
int th=num/1000;
int h=(num/100)%10;
int t=(num/10)%10;
int u=num%10;
/*Displaying equivalent roman number*/
System.out.println("Roman Equivalent= "+thou[th]+hund[h]+ten[t]+unit[u]);
}
/*Displaying an error message if the number entered is out of range*/
else
System.out.println("nYou entered a number out of Range.nPlease enter a
number in the range [1-3999]");
}
}

Output:
Enter
a
Roman Equivalent = MMMCDLXXXI
Question:

Number

3482

Write a program to accept a sentence and print only the first letter of each word of the sentence
in
capital
letters
separated
by
a
full
stop.
Example
:
INPUT
SENTENCE
:
This
is
a
cat
OUTPUT : T.I.A.C.
Solution:
import java.io.*;
8 class Initials
{
9 public static void main(String args[])throws IOException
1 {
0 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
11String s;
char x;
1 int l;
2 System.out.print("Enter any sentence: ");
1 s=br.readLine();
3 s=" "+s; //adding a space infront of the inputted sentence or a name
1 s=s.toUpperCase(); //converting the sentence into Upper Case (Capital
Letters)
4 l=s.length(); //finding the length of the sentence</span>
1 System.out.print("Output = ");

5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2

for(int i=0;i<l;i++)
{
x=s.charAt(i); //taking out one character at a time from the sentence
if(x==' ') //if the character is a space, printing the next Character along
with a fullstop
System.out.print(s.charAt(i+1)+".");
}
}
}

7
2
8
2
9
Output:
Enter
any
Output = J.F.S.S.

sentence:

Java

for

school

Programming Code:
1 import java.io.*;
2 class{ Swapping_Method2
3
public static void main(String args[])throws IOException
4
{
BufferedReader br=new BufferedReader(new
5
InputStreamReader(System.in));
6
int a,b;
7
System.out.print("Enter the 1st no: ");
8
a=Integer.parseInt(br.readLine());
9
System.out.print("Enter the 2nd no: ");
b=Integer.parseInt(br.readLine());
1
0
System.out.println("-------------------------------");
11
System.out.println("The numbers before swapping are");
1
System.out.println("a = "+a);
2
System.out.println("b = "+b);
1
//Beginning of Swapping
3
a=a+b;
1
b=a-b;
4
a=a-b;
1
5
//End of Swapping
1
System.out.println("-------------------------------");
System.out.println("The numbers after swapping are");
6
System.out.println("a = "+a);
1
System.out.println("b = "+b);
7
}
1
}
8
1
9
2
0
2
1
2

students

2
2
3
2
4
2
5
Output:
Enter the 1st no: 25
Enter the 2nd no: 13
The numbers before swapping are
a = 25
b = 13
The numbers after swapping are
a = 13
b = 25
Programming Code:
1 import java.io.*;
2 class{ Swapping_Method1
3
public static void main(String args[])throws IOException
4
{
BufferedReader br=new BufferedReader(new
5
InputStreamReader(System.in));
6
int a,b;
7
System.out.print("Enter the 1st no: ");
8
a=Integer.parseInt(br.readLine());
9
System.out.print("Enter the 2nd no: ");
b=Integer.parseInt(br.readLine());
1
0
System.out.println("-------------------------------");
11
System.out.println("The numbers before swapping are");
1
System.out.println("a = "+a);
2
System.out.println("b = "+b);
1
//Beginning of Swapping
3
a=a^b;
1
b=a^b;
4
a=a^b;
1
//End of Swapping
5
1
System.out.println("-------------------------------");
System.out.println("The numbers after swapping are");
6
System.out.println("a = "+a);

1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5

System.out.println("b = "+b);
}

Output:
Enter the 1st no: 25
Enter the 2nd no: 13
The numbers before swapping are
a = 25
b = 13
The numbers after swapping are
a = 13
b = 25

Das könnte Ihnen auch gefallen