Basic Important Number programs for ICSE part-5
To learn more java programshttps://wordpress-343193-1101484.cloudwaysapps.com/string-programs-part-5
To learn more java programs
String Programs part 5
Number programs ICSE
Happy Number:
The happy number is an important number program can be defined as a number that will result in one when it is replaced by the sum of the square of its digits repeatedly.
import java.io.*;
class Happy
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
int num=Integer.parseInt(br.readLine());
int sum=0;
double a=0;
while(num>=0)
{
a=num%10;
a=Math.pow(a,2);
sum+=a;
num=num/10;
if(num==0)
{
if(sum<10)
break;
else
{
num=sum;
sum=0;
continue;
}
}
}
if(sum==1)
System.out.println("It is a happy number");
else
System.out.println("It is a happy number");
}
}Keith number:
An n-digit number Num is a Keith Number if it forms a Fibonacci-like sequence that begins with the digits of the number itself. For example, 197 is a Keith number since it generates the sequence 1, 9, 7, 1+9+7=17, 9+7+17=33, 7+17+33=57, +17+33+57=107,33+57+107=197
The first few keith numbers are 14, 19, 28, 47, 61, 75, 197
import java.io.*;
class KEITH
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
int num=Integer.parseInt(br.readLine());
String n=Integer.toString(num);
int len=n.length();
int arr[]=new int[len]; // the last len terms of the series
for(int i=0;i<len;i++)
arr[i]=n.charAt(i)-'0'; // start with the digits of the number
int term=0,pos=0;
while(term<num)
{
term=0;
for(int i=0;i<len;i++) // next term = sum of the last len terms
term+=arr[i];
arr[pos]=term; // replace the oldest term
pos=(pos+1)%len;
}
if(term==num && len>1)
System.out.println("It is a Keith number");
else
System.out.println("It is not a Keith number");
}
}For videos related to java programming
http://www.youtube.com/user/trushnatej
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.