0% found this document useful (0 votes)
39 views45 pages

Comp

The document provides an index of 10 programming problems along with their page numbers. It then provides the full problem statement, algorithm and source code for Problem 1 - Write a program to input a natural number less than 1000 and display it in words. The problem asks the user to input a number, checks if it is less than 1000, then displays the number in words (e.g. 29 as "TWENTY NINE"). The algorithm breaks the number into hundreds, tens and ones places and uses arrays to map digits to their word representations. The source code implements this algorithm in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views45 pages

Comp

The document provides an index of 10 programming problems along with their page numbers. It then provides the full problem statement, algorithm and source code for Problem 1 - Write a program to input a natural number less than 1000 and display it in words. The problem asks the user to input a number, checks if it is less than 1000, then displays the number in words (e.g. 29 as "TWENTY NINE"). The algorithm breaks the number into hundreds, tens and ones places and uses arrays to map digits to their word representations. The source code implements this algorithm in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

INDEX

S.no. Programs Page no.

1. Write a program to input a natural number less than 1000 and display it in words. 4
2. Write a program to print Kaprekar numbers between a given range. 15
3. Write a program to check if a number is Smith number or not. 18
4. Write a program to print the frequency of each element in an array. 25
5. Write a program to sort an array using insertion sort. 28
6. Write a program to print the frequency of each word in a string. 49
7. Write a program to arrange a sentence in ascending order of its word lengths. 53
8. Write a program to sort a sentence in alphabetical order. 57
9. Write a program to sort the border elements of a n x n matrix in ascending order. 63
10. Write a program to print the prime elements of a matrix along with their position. 67
PROGRAM 1

Write a program to input a natural number less than 1000 and display it in

words.Test your program on the sample data and some random data.

Example –

INPUT: 29
OUTPUT: TWENTY NINE

INPUT: 17001
OUTPUT: OUT OF RANGE

Algorithm

Step-1: INPUT n
Step-2: IF n>=1000 THEN exit
Step-3: Create three string arrays ones[], teens[] and tens[] and store ones, teens and
tens in words.
Step-4: IF n>=100 AND n<1000 THEN GOTO Step 5 ELSE GOTO Step 12
Step-5: set a = n/100
Step-6: store ones[a-1+ + “hundred” in result
Step-7: update n as n/100
Step-8: IF n mod 10 = 0 AND n<>0 THEN GOTO Step 9 ELSE GOTO Step 11
Step-9: set a=n/10
Step-10: IF result = NULL THEN set result= tens[n-1]
ELSE set result= result +” and “+tens*a-1]
Step-11: set n as n mod 10
Step-12: IF n>20 AND n<100 THEN GOTO Step 13 ELSE GOTO Step 16
Step-13: set a as n/10
Step-14: set b as n mod 10
Step-15: IF result = NULL THEN set result as tens[a-1++” “+tens*b-1]
ELSE set result as result+” and “+tens*a-1++” and “+tens*b-1]
Step-16: IF n>10 AND n<20 THEN GOTO Step 17 ELSE GOTO Step 19
Step-17: set a as n mod 10

Step-18: IF result = NULL THEN set result as teens[a-1]

ELSE set result as result+” and “+teens*a-1]

Step-19: IF n<10 AND n<>0 THEN GOTO Step 20 ELSE GOTO Step 21

Step-20: IF result=NULL THEN set result as ones[n-1]

ELSE set result as result+ “ and “ +ones*n-1]

Step-21: DISPLAY result

Step-20: END

Source Code

import java.util.*;

class Question1{
public static void main(String args[])
throws InputMismatchException{

Scanner scan=new Scanner(System.in);

int n;

System.out.println("Enter a number less than 1000");


n=scan.nextInt();

if(n>=1000)
{
System.out.println("OUT OF RANGE");
}else{
String result,h="",t="",o="";
int a,b,c;
String ones[]={"one", "two","three","four","five",
"six","seven","eight","nine"};
String teens[]={"eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen"};
String tens[]={"ten","twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety"};

if(n>=100 && n<1000){


a=n/100;
result=ones[a-1]+" hundred";
n=n%100;
}
if(n%10==0 && n!=0){
a=n/10;
if(result!=null){
result+=" and ";
result+=tens[a-1];

else{
result=tens[a-1];
}
n=n%10;
}
if(n>20 && n<100){
a=n/10;
b=n%10;
if(result!=null){
result+=" and ";
result+=tens[a-1]+" "+ones[b-1];
}else{
result=tens[a-1]+" "+ones[b-1];
}
}
if(n>10 && n<20){
a=n%10;
if(result!=null){
result+=" and ";
result+=teens[a-1];
}else{
result=teens[a-1];
}
}
if(n<10 && n!=0){

if(result!=null)
{
result+=" and ";
result+=ones[n-1];
}else{
result=ones[n-1];
}

System.out.println("\n"+result.toUpperCase());

} //end of main
} //end of class
PROGRAM 2
Given the two positive integers p and q, where p < q. Write a program to determine
howmany kaprekar numbers are there in the range between 'p' and 'q'(both
inclusive) and output them.

About 'kaprekar' number:


A positive whole number 'n' that has 'd' number of digits is squared and split into 2
pieces,a right hand piece that has 'd' digits and a left hand piece that has remaining
'd' or 'd-1' digits. If sum of the pieces is equal to the number then it's a kaprekar
number.

SAMPLE DATA:

INPUT:
p=1
Q=1000

OUTPUT:

THE KAPREKAR NUMBERS ARE:


1,9,45,55,99,297,999

FREQUENCY OF KAPREKAR NUMBERS IS:8

Algorithm

Step-1: Enter the limits


Step-2: Run a loop using the entered limits
Step-3: For each number in the range, count the number of digits(d) in it
Step-4: Find the square of the number
Step-5: Now, divide the square in two parts
Step-6: To do this, first store 10 raised to the power d in t
Step-7: For first part, calculate square mod t
Step-8: For second part, calculate square/t
Step-9: Now add the two parts
Step-10: If the sum is equal to the original number then it is a kaprekar number, print it and
count it.
Step-11: If the sum is not equal to the original number it is not a kaprekar number
Step-12: Continue the process till all the numbers in the range are checked
Step-13: Display the frequency of kaprekar numbers.
Step-14: End

Source Code
import java.util.*;

class Question4{
public static void main(String args[])throws InputMismatchException{

Scanner scan=new Scanner(System.in);

System.out.println("Enter the range : ");


int p=scan.nextInt();
int q=scan.nextInt();

int d,i,n,a,b,s,freq;
freq=0; // to find the frequency of kaprekar numbers

System.out.println("The Kaprekar numbers are : ");

for(i=p;i<=q;i++)
{
n=i;
d=0; //to store the number of digits

//count the number of digits in the number


while(n>0){
d++;
n/=10;
}

s=i*i; // find the square of the number

//extract 'd' digits from the right of the square of the number
a=s%(int)Math.pow(10,d);

//extract 'd' or 'd-1' digits from the left of the square of the number
b=s/(int)Math.pow(10,d);

//Check if the two parts add up to the original number i.e. Condition for Kaprekar number
if(a+b==i){
System.out.print(i+" ");
freq++;
}
}

System.out.println("\nFREQUENCY OF KAPREKAR NUMBER IS : "+freq);


} //end of main
} //end of class
PROGRAM 3
A smith number is a composite number, the sum of whose digits is the sum of the
digits ofits 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
Source Code

import java.util.*;
class Question5{
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 until it is not a factor. This is the prime factorization method.
}
}
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
PROGRAM 4
Write a program to create an array of n integers and display the frequency of each element of the array.

Example:

Input:
Enter the number of terms: 10 Enter 10 integers: 1 2 2 2 3 4 3 4 5 6
Output:
Frequency of 1: 1
Frequency of 2: 3
Frequency of 3: 2
Frequency of 4: 2
Frequency of 5: 1
Frequency of 6: 1

Algorithm
Step-1: Input the size of the array as n

Step-2: Create an array of n elements

Step-3: Run a loop i from 0 to n

Step-4: Run a loop j from i+1 to n

Step-5: If the elements a[i] and a[j] are equal and a[j] is not zero, count and put zero in a[j]

Step-6: Outside the inner loop, print frequency of a[i] if it is not zero

Step-7: End

Source Code
import java.util.*;

class Question7{

public static void main(String args[])throws InputMismatchException{

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of terms: ");

int n=sc.nextInt();

int a[]=new int[n];

int i;
System.out.println("Enter "+n+ " integers");

a[i]=sc.nextInt();

int j,f;

for(i=0;i<n;i++)

for(i=0;i<n;i++)
{
f=1;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j] && a[j]!=0)
{
f++; a[j]=0;
}
}
if(a[i]!=0)
System.out.println("Frequency of "+a[i]+" : "+f);
}

} //end of main
} //end of class
PROGRAM 5
Write a program to create an array of n integers and sort the array in ascending order
usingInsertion sort technique.

Example –

INPUT:
Enter the size of the array: 5
Enter 5 elements: 5 9 7 3 -4

OUTPUT:
-4 3 5 7 9

Algorithm
Step-1: Input the size of the array n

Step-2: Create an array a[] and enter n integers in it.

Step-3: Now to apply insertion sort on this array, run a loop ‘i’ from 1 to n

Step-4: Store the element a[i] in k

Step-5: Run a reverse loop ‘j’ from i-1 till j>=0 and k<a[j]

Step-6: Copy all the elements one position to their right

Step-7: Once the inner loop terminates copy k in a[j+1]

Step-8: Close the loop

Step-9: Display the elements in sorted order using a loop

Step-10:End
Source Code

import java.util.*;

class Question8 {
public static void main(String args[])throws InputMismatchException{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the size of the array: ");


int n=sc.nextInt();

int a[]=new int[n];


int i,j,k;

System.out.println("Enter "+n+" integers:");

for(i=0;i<n;i++){
a[i]=sc.nextInt();
}

for(i=1;i<n;i++){
k=a[i];
for(j=i-1;j>=0 && k<a[j];j--){ //Shift the elements to the right until the condition is false
a[j+1]=a[j];
}
a[j+1]=k; //Store the element in the proper position of the array
}

System.out.println("Array in ascending order: ");

for(i=0;i<n;i++){
System.out.print(a[i]+" ");
}
}
PROGRAM 6
Write a program to enter a string and print the frequency of each word in a string.

Example –

Input:

Enter a string: The need for enlightenment is the need of the hour.

Output:

Word Frequency

The 3

need 2

for 1

enlightenment 1

is 1

of 1

hour 1

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: Set f=1
Step-9: Run another loop to check equality of elements of the array
Step-10: If equal, increment f by 1 and shift all the elements to the left
by one place and decrease the size of the array
Step-11: Display the element along with its frequency f
Step-12: Repeat the process for all the elements of the array
Step-13: End

Source Code
import java.io.*;

class Question15
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String");
String s=br.readLine();
s=s+" ";
int i,t=0,p=0,f=0,j,k;
int l=s.length();
String a[]=new String[l];
String b;

for(i=0;i<l;i++)
{
if(s.charAt(i)==' ')
{
b= s.substring(p,i);
a[t]=b;
t++;
p=i+1;
}
}

for(i=0;i<t;i++)
{
f=1;
for(j=i+1;j<t;j++)
{
if(a[i].equals(a[j]))
{
f++;
for(k=j;k<t-1;k++)
{
a[k]=a[k+1];
}

t--;
j--;
}
}
System.out.println("Frequency of " + a[i] + " : " + f);
}

} //end of main
} //end of class
PROGRAM 7
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 (.), exclamation mark (!) or a question mark
(?).

Example –

INPUT:

Enter a sentence: Days are longer in summers.

OUTPUT:

in are days longer summers.

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
Source Code
import java.io.*;

class Question16
{
public static void main (String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

System.out.print("enter the string");


String s = br.readLine();
s=s.toLowerCase();
int l= s.length();
String a[]= new String[l];
int t=0,i,p,j;
String g;
char k;
p=0;

for(i=0;i<l;i++)
{
k=s.charAt(i);
if(k==' ' || k== '.' || k==’!’ || k==’?’)// Check for delimiters to extract words
{
String b = s.substring(p,i);
a[t]= b; //Store the words in an array
t++;
p=i+1;
}
}
//Using bubble sort to arrange the string array in ascending order of word length
for(i=0;i<t;i++)
{
for(j=0;j<t-i-1;j++)
{
if(a[j].length()>a[j+1].length()) //if the word on the left is smaller in length, swap it.
{
g=a[j];
a[j]=a[j+1];
a[j+1]=g;
}
}
}

for(i=0;i<t;i++)
{
System.out.print(a*i++” “); //print the words with a blank space
}
System.out.print("."); //display period to make it a sentence

} //end of main
} //end of class
PROGRAM 8
Write a program to enter a sentence and sort it in alphabetical order. You can convert the
sentence in either uppercase or lowercase before sorting.

Example –
INPUT:
Enter a sentence: Every cloud has a silver lining.
OUTPUT:

a cloud every has lining silver

Algorithm

Step-1: Enter a sentence

Step-2: Convert it into lowercase

Step-3: Take the length of the string

Step-4: Run a loop to access the string

Step-5: Extract a character

Step-6: If character is blank space or a period, extract the word

and store it in an array

Step-7: Continue the process until all the words are stored in the array

Step-8: Now run two loops i and j for sorting

Step-9: Compare the strings in the array

Step-10: If ith string is greater than jth string swap them

Step-11: Continue the process till the entire array is sorted in ascending order

Step-12: Run a loop and display array elements with a blank space.

Step-13: End
Source Code
import java.io.*;

public class Question17 {


public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter a string : ");


String str = br.readLine();

str=str.toLowerCase();
int l=str.length();
String a[]=new String[l];

int i,j,p=0,x=0;

for(i=0;i<l;i++){
char ch=str.charAt(i);
if(ch==' ' || ch=='.'){ //Extract the word when a space or period is found
String temp=str.substring(p,i);
a[x++]=temp; //store the word in a string array
p=i+1;
}
}

//Sort the array alphabetically


for(i=0;i<x;i++){ // limit is x since array a[] has x elements
for(j=i+1;j<x;j++){
if(a[i].compareTo(a[j])>0){ //Comparing strings
String temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Sentence in alphabetical order: ");
for(i=0;i<x;i++)
System.out.print(a[i]+" ");

} //end of main
} //end of class
PROGRAM 9

Write a program to sort the border elements of a n x n matrix in ascending order.

Example –

INPUT:

Enter the number of rows for a square matrix: 3

Enter 9 elements:

4 9 3
1 7 6
5 8 2

OUTPUT:

Matrix with border in ascending order:

1 2 3
9 5 4
8 7 6

Algorithm

Step-1: Input the number of rows for a square matrix

Step-2: Create an n x n matrix and enter elements in it

Step-3: Run two loops to access the matrix

Step-4: Check for border elements

Step-5: If found, store them in a single dimension array, say b[]

Step-6: Continue the process till all the border elements are stored in b[]

Step-7: Now, sort the array b[] in ascending order using any sorting technique

Step-8: To store the sorted border elements in the matrix, run a loop for the

first row and store elements from b[] in the matrix


Step-9: Run a loop for last column and do the same

Step-10: Run a loop for last row and then first column to store the elements from b[] to matrix

Step-11: Finally, print the matrix that now has border elements sorted in ascending order.

Step-12: End

Source Code
import java.util.*;

public class Question19 {


public static void main(String args[])throws InputMismatchException{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows for a sqaure matrix: ");
int n=sc.nextInt();

int a[][]=new int[n][n];


int i,j,s=0;

System.out.println("Enter "+(n*n)+" elements:");


for(i=0;i<n;i++){
for(j=0;j<n;j++){
a[i][j]=sc.nextInt();
}
}

int b[]=new int[n*n];


int x=0;

//Run loops to store the border elements in a single dimension array


for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==0 || j==0 || i==n-1 || j==n-1){
b[x++]=a[i][j];
}
}
}

// Sort the array containing border elements


for(i=0;i<x;i++){
for(j=i+1;j<x;j++){
if(b[i]>b[j]){
int t=b[i];
b[i]=b[j];
b[j]=t;
}
}

//Now store the sorted border elements back to the matrix


int c=0;
for(i=0;i<n;i++) //Store the sorted elements in the first row
a[0][i]=b[c++];

for(i=1;i<n;i++) //Store the sorted elements in the last column


a[i][n-1]=b[c++];

for(i=n-2;i>=0;i--) //Store the sorted elements in the last row


a[n-1][i]=b[c++];

for(i=n-2;i>0;i--) //Store the sorted elements in the first column


a[i][0]=b[c++];

for(i=0;i<n;i++){
for(j=0;j<n;j++){
System.out.print(a[i][j]+" ") ;
}
System.out.println();
}

} //end of main
} //end of class
PROGRAM 10
Write a program to create a m x n matrix and print the prime elements in it along with the row
and column in which they are present.

Example –

INPUT:

Enter the row and column for the matrix: 3


4

Enter 12 elements:

4 5 1 6
8 25 30 2
16 9 45 3

OUTPUT:

Prime Element Row Column

5 0 1

2 1 3

3 2 3

Algorithm

Step-1: Input the number of rows and columns as m and n

Step-2: Create a m x n matrix

Step-3: Enter m x n elements in the matrix

Step-4: Run a loop for the rows m

Step-5: Run a loop for the columns n


Step-6: Now run a loop to find the number of factors of the matrix element

Step-7: If the number of factors is 2 print the element, its row index and column index

Step-8: Continue process for all the elements

Step-9: End

Source Code
import java.util.*;
public class Question20{
public static void main(String args[])throws InputMismatchException{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows and columns of a matrix: ");
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
int i,j,k,s=0;

System.out.println("Enter "+(m*n)+" elements:");


for(i=0;i<m;i++){
for(j=0;j<n;j++){
a[i][j]=sc.nextInt();
}
}
System.out.println("Prime Element\t Row\t Column");
for(i=0;i<m;i++){
for(j=0;j<n;j++){

int c=0;
for(k=1;k<=a[i][j];k++){
if(a[i][j]%k==0){
c++;
}
}
if(c==2){
System.out.println(a[i][j]+"\t\t"+i+"\t\t"+j);
}
}
}

} //end of main
} //end of class

You might also like