Computer Practicals Ashmeet
Computer Practicals Ashmeet
CLASS : 11
SECTION : A
ROLL NUMBER : 26
COMPUTER PROJECT
SEMESTER II
1. Write a program to print the sum of the rows and columns of a
rectangular matrix.
Algorithm:
i. Import the Scanner class from the java.util package
ii. Create a public class called "SumRec"
iii. Define the main method within the class
iv. Create a Scanner object "sc" to take input from the user
v. Prompt the user to enter the number of rows
vi. Read the number of rows entered by the user and store it in
the "rows" variable
vii. Prompt the user to enter the number of columns
viii. Read the number of columns entered by the user and store
it in the "columns" variable
ix. Prompt the user to enter the elements of the two-
dimensional array
x. Create a two-dimensional array called "array" with "rows"
number of rows and "columns" number of columns
xi. Use a nested loop to input the elements of the array and
print the array
xii. Use another nested loop to calculate the sum of each row
and print it
xiii. Use another nested loop to calculate the sum of each
column and print it
xiv. End the program.
import java.util.*;
public class SumRec {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number of rows");
int rows = sc.nextInt();
System.out.println("Enter the number of Columns");
int coloumbs = sc.nextInt();
System.out.println("Enter the Elemnts");
int array[][]=new int[rows][columns];
for(int i=0;i<rows;i++)
{
for(int j=0;j<coloumbs;j++)
{
array[i][j] = sc.nextInt();
System.out.print(array[i][j]+" ");
}
System.out.println();
}
sc.close();
for(int i=0;i<rows;i++)
{
int sum=0;
for(int j=0;j<coloumbs;j++)
{
sum=sum+array[i][j];
}
System.out.println("Sum of rows "+(i+1)+"="+sum);
}
for(int i=0;i<coloumbs;i++)
{
int sum=0;
for(int j=0;j<rows;j++)
{
sum=sum+array[j][i];
}
System.out.println("Sum of Columns "+(i+1)+"="+sum);
}
}
}
Sample Input:
3
3
123
456
789
Sample Output:
Sum of row 1 = 6
Sum of row 1 = 15
Sum of row 1 = 24
Sum of column 1 = 12
Sum of column 1 = 15
Sum of column 1 = 18
Variable Table:
Variable
Name Data Type Purpose
Stores the number of rows of the
rows int two-dimensional array
Stores the number of columns of
columns int the two-dimensional array
Algorithm:
i. Import the Scanner class from the java.util package
ii. Create a class named WordWise and define a constructor
with an empty string named str.
iii. Create a method named readsent to read a sentence from
the user. In this method, a Scanner object is created to take
input from the user and store it in the str variable.
iv. Create a method named freq_vowel to find the frequency
of vowels in a word. This method takes a word as a
parameter and returns the frequency of vowels in the word.
The word is converted to lowercase using toLowerCase()
method. Then, a loop is used to traverse the word and
count the frequency of vowels using an if-else condition.
v. Create a method named arrange to arrange the words in the
sentence based on the frequency of vowels. In this method,
the sentence is tokenized using StringTokenizer class and
the words are stored in a variable. Using a while loop, the
frequency of vowels in each word is calculated by calling
the freq_vowel method and the words are printed along
with their frequency.
vi. In the main method, an object of the WordWise class is
created and the readsent method is called to read the
sentence from the user. Then, the arrange method is called
to arrange the words in the sentence based on the
frequency of vowels.
vii. The program ends.
import java.util.*;
public class WordWise {
String str;
WordWise()
{
str="";
}
void readsent()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Sentence");
str=sc.nextLine();
sc.close();
}
int freq_vowel(String w)
{
int len = w.length();
int freq=0;
w=w.toLowerCase();
for(int i=0;i<len;i++)
{
char ch = w.charAt(i);
if(ch =='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
freq++;
}
}
return freq;
}
void arrange()
{
StringTokenizer st = new StringTokenizer(str);
while(st.hasMoreTokens())
{
String w = st.nextToken();
System.out.println(w+" Freq="+freq_vowel(w));
}
}
public static void main(String[] args) {
WordWise obj = new WordWise();
obj.readsent();
obj.arrange();
}
Sample Input:
I am Divyansh
Sample Output:
I Freq= 1
am Freq= 1
Divyansh Freq= 3
Variable Table:
Variable Name Data Type Purpose
Algorithm:
i. Import the Scanner class from the java.util package
ii. The class Employee is declared.
iii. Declare the static variables: name, age, hra, pf, da, basic,
and net.
iv. Create an instance method accept() that accepts input from
the user for the employee's name, age, and basic salary.
v. Create an instance method calc() that calculates the
employee's HRA, DA, PF, and net salary. The calculation
of HRA is done by multiplying 18.5% of the basic salary.
The calculation of DA is done by multiplying 17.45% of
the basic salary. The calculation of PF is done by
multiplying 8.10% of the basic salary. If the employee's
age is greater than 50, the net salary is calculated by
adding HRA, DA, PF and 5000, otherwise, the net salary
is calculated by adding HRA, DA, and PF.
vi. Create an instance method print() that prints the
employee's basic salary, HRA, DA, PF, and net salary.
vii. The main method is declared and an object of the
Employee class is created.
viii. The accept() method is called on the object created to
accept the input from the user.
ix. The calc() method is called on the object to calculate the
employee's HRA, DA, PF, and net salary.
x. The print() method is called on the object to print the
employee's basic salary, HRA, DA, PF, and net salary.
xi. The program ends.
import java.util.*;
class Employee
{
static String name;
static int age;
static double hra;
static double pf;
static double da;
static double basic;
static double net=0;
void accept()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the name");
name = sc.nextLine();
System.out.println("Enter the age");
age = sc.nextInt();
System.out.println("Enter the basic salary");
basic = sc.nextInt();
sc.close();
}
void calc()
{
hra = ((18.5/100)*basic);
da = ((17.45/100)*basic);
pf = ((8.10/100)*basic);
if(age>50)
{
net = hra +da +pf+5000;
}
else
{
net = hra+ da +pf;
}
}
void print()
{
System.out.println("Basic Salary = "+basic);
System.out.println("HRA = "+hra);
System.out.println("PA = "+da);
System.out.println("PF = "+pf);
System.out.println("Net Salary = "+net);
}
public static void main(String[] args) {
Employee obj = new Employee();
obj.accept();
obj.calc();
obj.print();
}
}
Sample Input:
Divyansh
61
5000
Sample Output:
Basic Salary = 5000.0
HRA = 925.0
PA = 872.4999999999999
PF = 405.0
Net Salary = 7202.5
Variable Table:
Algorithm:
i. Import the Scanner class from java.util package
ii. Create a class named "Matrix"
iii. Create a Scanner object to get input from the user
iv. In the main method, prompt the user to enter a number
v. Check if the number is greater than or equal to 10 or less
than or equal to 3
vi. If the number is not within the desired range, print "Invalid
input" and end the program
vii. If the number is within the desired range, create a 2D char
array with the size of n x n, where n is the user input
viii. Prompt the user to enter three characters
ix. Store the first character in all the corners of the matrix
(m[0][0], m[0][n-1], m[n-1][0], m[n-1][n-1])
x. For all the other elements on the first row and last row,
store the second character (m[0][1]-m[0][n-2], m[n-1][1]-
m[n-1][n-2])
xi. For all the elements on the first column and last column,
store the second character (m[1][0]-m[n-2][0], m[1][n-1]-
m[n-2][n-1])
xii. For all the remaining elements in the matrix, store the third
character
xiii. Use two for loops to print the matrix
xiv. End the program.
import java.util.*;
class Matrix
{
Scanner sc=new Scanner(System.in);
void main()
{
System.out.println("Enter a number");
int n=sc.nextInt();
if(n>=10 || n<=3)
{
System.out.println("Invalid input");
}
else
{
char m[][]=new char[n][n];
int i,j;
System.out.println("Enter three characters");
char c1=sc.next().charAt(0);
char c2=sc.next().charAt(0);
char c3=sc.next().charAt(0);
m[0][0]=c1;
m[0][n-1]=c1;
m[n-1][0]=c1;
m[n-1][n-1]=c1;
for(i=1;i<n-1;i++)
{
m[0][i]=c2;
}
for(i=1;i<n-1;i++)
{
m[n-1][i]=c2;
}
for(i=1;i<n-1;i++)
{
m[i][0]=c2;
}
for(i=1;i<n-1;i++)
{
m[i][n-1]=c2;
}
for(i=1;i<n-1;i++)
{
for(j=1;j<n-1;j++)
{
m[i][j]=c3;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
}
}
Sample Input:
3
&
@
#
Sample output:
&@&
@#@
&@&
Variable Table:
Algorithm:
i. Import the Scanner class from the java.util package.
ii. Create a class named 'ascendnum'.
iii. Inside the class, create a main method.
iv. Inside the main method, create a Scanner object to read
input from the user.
v. Prompt the user to enter a number 'n'.
vi. Create variables to store the number in ascending order
'nf', number of digits 'count', a copy of the original number
'copy', the last digit 'd', and loop variables 'i', 'j' and 'c'.
vii. Calculate the number of digits in 'n' by continuously
dividing it by 10 and incrementing the 'count' until 'copy'
is less than or equal to 0.
viii. Create an array 'arr' with size 'count' to store each digit of
'n'.
ix. Extract each digit of 'n' by repeatedly taking the remainder
of 'copy' divided by 10, and store it in 'arr'.
x. Sort the array 'arr' in ascending order using a nested for
loop and bubble sort.
xi. Create the number 'nf' in ascending order by multiplying
'nf' by 10 and adding each digit from the sorted array 'arr'.
xii. Finally, print the number in ascending order 'nf'.
xiii. The program ends.
import java.util.*;
public class ascendnum
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int nf=0;
int count=0;
int copy=n;
int d=0,i,j,c=0;
while(copy>0)
{
count++;//digit counter
copy/=10;
}
int arr[]=new int[count];
copy=n;
while(copy>0)
{
d=copy%10;
arr[c++]=d;
copy/=10;
}
for(i=0;i<count;i++)
{
for(j=0;j<count-1-i;j++)
{
if(arr[j]>arr[j+1])
{
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
for(i=0;i<count;i++)
{
nf=nf*10+arr[i];
}
System.out.println("Number in ascending order is "+nf);
}
}
Sample Input:
6985326
Sample Output:
2356689
Variable Table:
Algorithm:
i. Import the java.util package to use the Scanner class.
ii. Define the main method of the class LargestWord.
iii. Create a Scanner object 'sc' to get the input sentence from
the user.
iv. Read the sentence as input and store it in the string
variable 'sent'.
v. Convert the sentence to lowercase and close the Scanner.
vi. Split the sentence into words and store each word in an
array of strings 'arr'.
vii. Get the length of the array 'arr' and store it in the variable
'len'.
viii. Create a temporary string variable 'temp' to hold the values
during swapping.
ix. Use nested for loops to compare each word with the other
words.
x. If the length of the first word is greater than the length of
the second word, swap the two words.
xi. If the length of the first word is equal to the length of the
second word, compare the two words lexicographically. If
the first word is lexicographically greater than the second
word, swap the two words.
xii. Repeat the above steps for all words in the array.
xiii. The last word in the sorted array will be the largest word
in the sentence.
xiv. Print the largest word in the sentence.
xv. The program ends.
import java.util.*;
public class LargestWord {
public static void main(String[] args) throws
NoSuchElementException{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Sentence");
String sent = sc.nextLine();
sent=sent.toLowerCase();
sc.close();
String arr[] = sent.split(" ");
int len = arr.length;
String temp="";
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(arr[i].length()>arr[j].length())
{
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
else if(arr[i].length()==arr[j].length())
{
if(arr[i].compareTo(arr[j])>0)
{
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
}
}
System.out.println("The largest word is: "+arr[len-1]);
}
}
Sample Input:
My name is Divyansh
Sample Output:
The largest word is: Divyansh
Variable Table:
Algorithm:
i. Import the Scanner class from the java.util package.
ii. Define the main class "rangearr".
iii. Create a Scanner object "sc" to take input from the user.
iv. Declare an array "arr" of size 20 to store the numbers
entered by the user.
v. Using a for loop, take input of 20 numbers from the user
and store them in the "arr" array.
vi. Initialize two variables, "max" and "min" to the first
element of the array "arr".
vii. Use another for loop to iterate over the elements of the
array.
viii. For each iteration, compare the current element with the
maximum and minimum values. If it is greater than the
current maximum value, update the maximum value. If it
is less than the current minimum value, update the
minimum value.
ix. After the for loop, subtract the minimum value from the
maximum value to get the range.
x. Finally, print the range as the output of the program.
xi. The program ends.
import java.util.*;
public class rangearr
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double arr[]=new double[20];
int i;
System.out.println("Enter 20 Numbers");
for(i=0;i<20;i++)
{
arr[i] = sc.nextInt();
}
double max=arr[0];
double min=arr[0];
double range=0.0;
for(i=0;i<20;i++)
{
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
range=max-min;
System.out.println("The range is : "+range);
}
}
Sample input:
1 2 3 4 5 6 7 8 910 11 12 13 14 15 16 17 18 19 20 21
Sample Output:
The range is: 909.0
Variable Table:
Variable
Name Data Type Purpose
Algorithm:
i. Import the Scanner class from the java.util package.
ii. Create a class named "String_2" and define the variables 's'
as a string, and initialize an empty constructor.
iii. Create an "Input" method to take the input sentence from
the user using Scanner class.
iv. Create a boolean method named "isValid" to check if the
last character of the sentence is ".", "!" or "?"
v. If the last character is ".", "!" or "?", then the method will
return "true". If not, it will return false.
vi. Create an "Operate" method to split the sentence into
words using the StringTokenizer class and store the words
in an array.
vii. Sort the words in the array in ascending order of their
length.
viii. Print the original sentence and the sorted words.
ix. Create a method named "Vowel" to count the number of
vowels in a given word.
x. Create the main method to create an object of the class
"String_2" and call the methods "Input", "Operate" and
"Vowel"
xi. If the "isValid" method returns false, the program will
print "Not a valid string".
xii. If the "isValid" method returns true, the program will
execute the "Operate" method and sort the words in the
sentence.
xiii. The program ends.
import java.util.*;
import java.io.*;
class String_2
{
String s;
void Input()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter a sentence ending with ?,. or !");
s = sc.nextLine();
}
boolean isValid()
{
int i = s.length() - 1;
if(".?!".indexOf(s.charAt(i)) != -1)
return true ;
else
return false ;
}
void Operate()
{
StringTokenizer str = new StringTokenizer (s);
int k =0,i,j,p;
if(isValid() == true)
{
String arr[] = new String[str.countTokens()];
while(str.hasMoreTokens())
{
arr[k++] = str.nextToken();
}
for(i =0; i < k; i ++)
{
for(j =0; j < k - i-1; j ++)
{
if (arr[j].length() > arr[j + 1].length())
{
String temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println(s);
for(i=0;i<k;i++)
{
System.out.println(arr[i]);
}
}
else
{
System.out.println("Not a valid string");
}
}
int Vowel(String w)
{
int i,j=0;
for(i=0;i<w.length();i++)
{
if("AEIOUaeiou".indexOf(w.charAt(i)) != -1)
{
j=j+1;
}
}
return j;
}
Sample input:
My name is Divyansh?
Sample Output:
is
My
name
Divyansh
Variable Table:
Variable
Name Data Type Purpose
Algorithm:
i. Import the Scanner class from the java.util package.
ii. Create an object of the class 'Buzz'.
iii. Read the limit, 'n', for the number of buzz numbers to be
displayed.
iv. Call the 'Number' method with the parameters '7' (start
value), '1' (counter), and 'n' (limit).
v. Inside the 'Number' method, check if the counter is less
than or equal to the limit.
vi. If the counter is less than or equal to the limit, check if the
number is divisible by 7 or the last digit of the number is
7.
vii. If the condition is true, print the number and increment the
counter by 1.
viii. Call the 'Number' method again with the updated
parameters (number + 1, counter, limit).
ix. Repeat steps 4 to 7 until the counter becomes greater than
the limit.
x. If the counter becomes greater than the limit, the program
terminates.
xi. The program ends.
import java.util.Scanner ;
class Buzz
{
void Number(int n,int c,int a)
{
if(c<=a)
{
if((n%10)==7 || n%7 ==0)
{
System.out.print(n+" ");
c ++ ;
}
Number(n+1,c,a);
}
}
void main()
{
Buzz obj = new Buzz();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the limit");
int n = sc.nextInt();
System.out.println("Buzz numbers are");
obj.Number(7,1,n);
}
}
Sample input:
5
Sample Output:
7 14 17 21 27
Variable Table:
Variable
Name Data Type Description
Algorithm:
i. Import the Scanner class from the java.util package.
ii. Start the program with the main method.
iii. Create an object of the class Capital using the statement
Capital obj = new Capital().
iv. Call the input() method to get the input sentence from the
user. The input() method creates a Scanner object sc to
read the input and stores the sentence in the sent variable.
v. Call the display() method to display the number of words
starting with a capital letter.
vi. In the display() method, first, call the isCap(sent) method
to check if there are any words starting with a capital
letter.
vii. The isCap(sent) method adds a space at the beginning of
the sentence and checks for a space before each word. If
the first character of a word is uppercase, then it
increments the frequency counter freq. At the end of the
loop, the flag variable is set to true if there are any words
starting with a capital letter.
viii. After the isCap(sent) method returns a value, the display()
method checks the value of flag. If flag is true, it prints the
frequency of words starting with a capital letter. If flag is
false, it prints "No capital letter found".
ix. End the program.
import java.util.*;
public class Capital {
static int freq;
static String sent;
Capital()
{
freq=0;
sent="";
}
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
sent = sc.nextLine();
sc.close();
}
public static boolean isCap(String w)
{
String sent2 = " "+sent;
boolean flag=false;
for(int i=0;i<sent2.length();i++)
{
if(sent2.charAt(i)==' ')
{
char ch2 = sent2.charAt(i+1);
if(Character.isUpperCase(ch2)==true)
{
flag=true;
freq++;
}
else{
flag=false;
}
}
}
if(flag==true)
{
return true;
}
else
{
return false;
}
}
public void display()
{
boolean flag = Capital.isCap(sent);
if(flag==true)
{
System.out.println("The Number of words Starting with
Capital letter is: "+freq);
}
else{
System.out.println("No capital letter found");
}
}
public static void main(String[] args) {
Capital obj = new Capital();
obj.input();
obj.display();
}
}
Sample input:
My name is Divyansh
Sample Output:
The Number of words starting with Capital letter is: 2
Variable table:
Variable Data
Name Type Purpose
Algorithm:
import java.util.*;
class goldbach
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt(),flag=0;
sc.close();
if(n%2==1 || n<8)
{
System.out.println("Not a Goldbach number");
}
else
{
for(int i=3;i<n;i+=2)
{
if(check(i) && check(n-i))
{
System.out.println("Goldbach number");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println("Not a Goldbach number");
}
}
}
boolean check(int a)
{
int c=0;
for(int j=2;j<a;j++)
{
if(a%j==0)
{
c++;
}
}
if(c==0)
{
return true;
}
return false;
}
}
Sample input:
66
Sample Output:
Goldbatch Number
Variable Table:
Variable Data
Name Type Purpose
Algorithm:
import java.util.*;
public class Fibonacci {
static int a=0,b=1,c=0;
public static void fibo(int num)
{
if(num>=1)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
c=0;
fibo(num-1);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter the Length of the Fibonacci
Series");
int num = sc.nextInt();
sc.close();
System.out.print(a+" "+b);
fibo(num);
}
}
Sample input:
8
Sample output:
0 1 1 2 3 5 8 13 21 34
Variable Table:
Algorithm:
import java.util.*;
class Combinations {
void main() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a three-digit number:");
int num = sc.nextInt();
sc.close();
int[] digits = new int[3];
int i = 0;
while (num > 0) {
int digit = num % 10;
digits[i] = digit;
num /= 10;
i++;
}
for (int j = 0; j < 3; j++) {
int[] combination = {digits[0], digits[1], digits[2]};
int firstDigit = combination[j];
for (int k = 0; k < 3; k++) {
if (k != j) {
int secondDigit = combination[k];
for (int l = 0; l < 3; l++) {
if (l != k && l != j) {
int thirdDigit = combination[l];
int result = (firstDigit * 100) + (secondDigit *
10) + thirdDigit;
System.out.println(result);
}
}
}
}
}
}
}
Sample Input:
369
Sample Output:
963
936
693
639
396
369
Variable Table:
Variable Data
Name Type Purpose
Algorithm:
i. Import the java.util package.
ii. Create a public class named DiagonalSwap2d.
iii. Define the main method.
iv. Create a Scanner object sc to get user input.
v. Ask the user to enter the size of the 2D array.
vi. Create a 2D array with the specified size and take inputs for
the elements of the array.
vii. Display the original 2D array.
viii. Use two nested for loops to swap the diagonals of the 2D
array.
a. The outer for loop runs from 0 to size-1.
b. The inner for loop swaps the diagonal elements.
ix. Display the modified 2D array after swapping the diagonals.
x. End the program.
import java.util.*;
public class DiagonalSwap2d {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter the size of the 2d Array");
int size = sc.nextInt();
System.out.println("Enter the Elemnts");
int array[][]=new int[size][size];
int temp=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
array[i][j] = sc.nextInt();
System.out.print(array[i][j]+" ");
}
System.out.println();
}
sc.close();
// for swapping the diagonals we use this loop
for(int i=0;i<size;i++)
{
temp = array[i][i];
array[i][i] = array[i][size-i-1];
array[i][size-1-i]=temp;
}
// Reprinting the Modified Array
System.out.println("Array after swapping");
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
}
Sample input:
3
123
456
789
Sample output:
321
456
987
Variable table:
Algorithm:
i. Import the java.util package.
ii. Create a public class named HappyWrod.
iii. Define the main method.
iv. Read a word from the user using a Scanner object.
v. Convert the word to uppercase and store it in a String
variable word.
vi. Initialize an empty String variable wordValueStr to
store the numerical value of the word.
vii. Loop through each character in the word and convert
it to its corresponding numerical value (A=1, B=2, ...,
Z=26) by subtracting 64 from its ASCII code.
viii. Append the numerical value to the wordValueStr.
ix. Convert the wordValueStr to an integer wordValue.
x. Check if wordValue is a "happy number" by calling
the isHappyNumber method. A number is called a
"happy number" if the sum of the squares of its digits
eventually becomes 1. The method isHappyNumber
calculates the sum of the squares of the digits of the
number and returns true if the sum is 1 and false
otherwise.
xi. If wordValue is a "happy number", print "A Happy
Word". If not, print "Not a Happy Word".
import java.util.*;
public class HappyWord
{
static boolean isHappyNumber(int num)
{
int sum = 0;
int n = num;
do {
sum = 0;
while (n != 0) {
int d = (int)(n % 10);
sum += d * d;
n /= 10;
}
n = sum;
} while (sum > 6);
return (sum == 1);
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = in.next();
word = word.toUpperCase();
String wordValueStr = "";
int len = word.length();
for (int i = 0; i < len; i++) {
wordValueStr += String.valueOf(word.charAt(i) - 64);
}
int wordValue = Integer.parseInt(wordValueStr);
boolean isHappy = isHappyNumber(wordValue);
if (isHappy)
System.out.println("A Happy Word");
else
System.out.println("Not a Happy Word");
}
}
Sample Input:
VAT
Sample Output:
A Happy Word
Variable Table:
Variable Data
Name Type Purpose
To store the word entered by the
word String user.
To store the numerical value of
wordValueStr String the word.