Class 9 Comp Important Programs
Class 9 Comp Important Programs
int c=0;
for (int i = 1; i <num ; i++)
{
if (i * (i + 1) == num)
{
c++;
}
}
if (c==1)
System.out.println(num + " is a pronic number");
else
System.out.println(num + " is not a pronic number");
}
}
2. Write a program to input a number and check and print whether it is a Niven number or not. [A
Niven Number is a positive integer that is divisible by the sum of its digits. .]
Examples:
81
8+1=9
81 is divisible by 9
import java.util.Scanner;
if(n% sum==0)
System.out.println(n + " is a Niven Number.");
else
System.out.println(n + " is not a Niven Number.");
}
}
3. Write a program to input a number and check and print whether it is a Neon number or not. [A
positive integer whose sum of digits of its square is equal to the number itself is called a neon number..]
Examples:
9
Square of 9= 81
8+1=9
import java.util.*;
public class NeonNumber
{
public static void main(String args[])
{
int sum = 0, n;
Scanner in= new Scanner(System.in);
System.out.print("Enter the number to check: ");
//raeds an integer from the user
n = in.nextInt();
//calculate square of the given number
int sq = n * n;
//loop executes until the condition becomes false
while(sq!= 0)
{
//find the last digit of the square
int digit = sq% 10;
//adds digits to the variable sum
sum = sum + digit;
//removes the last digit of the variable square
sq = sq / 10;
}
//compares the given number (n) with sum
if(n == sum)
System.out.println(n + " is a Neon Number.");
else
System.out.println(n + " is not a Neon Number.");
} }
Page2|4
4. A happy number is a number which eventually reaches 1 when replaced by the sum of the square of each
digit. For example, consider the number 320.
32 + 22 + 02 ⇒ 9 + 4 + 0 = 13
12 + 32 ⇒ 1 + 9 = 10
12 + 02 ⇒ 1 + 0 = 1
Write a program in Java to enter a number and check if it is a Happy Number or not.
import java.util.Scanner;
if (sum == 1) {
System.out.println(num + " is a Happy Number");
}
else {
System.out.println(num + " is not a Happy Number");
}
}
}
Page3|4
5. Write a program to input a number and check whether it is 'Magic Number' or not. Display the
message accordingly.
A number is said to be a magic number if the eventual sum of digits of the number is one.
Sample Input : 55
Then, 5 + 5 = 10, 1 + 0 = 1
Sample Output: Hence, 55 is a Magic Number.
Similarly, 289 is a Magic Number.
import java.util.Scanner;
while (n > 9) {
int sum = 0;
while (n != 0) {
int d = n % 10;
n /= 10;
sum += d;
}
n = sum;
}
if (n == 1)
System.out.println(num + " is Magic Number");
else
System.out.println(num + " is not Magic Number");
}
}
Page4|4