0% found this document useful (0 votes)
32 views4 pages

Class 9 Comp Important Programs

The document contains Java programs for checking various types of numbers: Pronic, Niven, Neon, Happy, and Magic numbers. Each program prompts the user for input and determines if the number fits the criteria for the respective type. Examples and explanations of each number type are provided alongside the code implementations.
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)
32 views4 pages

Class 9 Comp Important Programs

The document contains Java programs for checking various types of numbers: Pronic, Niven, Neon, Happy, and Magic numbers. Each program prompts the user for input and determines if the number fits the criteria for the respective type. Examples and explanations of each number type are provided alongside the code implementations.
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/ 4

ST.

MARY’S ENGLISH SCHOOL


CLASS –9
SUBJECT – Computer Application
CH –IMPORTANT PROGRAMS
1.Write a program to input a number and check and print whether it is a Pronic number
or not. [Pronic number is the number which is the product of two consecutive integers.]
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
import java.util.Scanner;

public class PronicNumber


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();

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;

public class NivenNumber


{
public static void main(String args[])
Page1|4
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();
int sum = 0,ld, n=num;
while (num != 0)
{ ld=num%10;
sum = sum+ld;
num=num /10;
}

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

Hence, 320 is a Happy Number.

Write a program in Java to enter a number and check if it is a Happy Number or not.

import java.util.Scanner;

public class HappyNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number to check: ");
long num = in.nextLong();
long sum = 0;
long n = num;
do {
sum = 0;
while (n != 0) {
int d = (int)(n % 10);
sum += d * d;
n /= 10;
}
n = sum;
} while (sum > 6);

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;

public class MagicNum


{

public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter number to check: ");
int num = in.nextInt();
int n = num;

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

You might also like