0% found this document useful (0 votes)
8 views17 pages

Tosh Programs

The document contains multiple Java programs that check for various types of numbers including Armstrong, Prime, Automorphic, Pronic, Abundant, Niven, Spy, Twisted Prime, Special, Co-Prime, Duck, Buzz, Palindrome, Perfect, and Dudeney numbers. Each program includes user input, logic to determine the type of number, and output statements to inform the user of the result. The programs utilize basic control structures and mathematical operations to perform their checks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views17 pages

Tosh Programs

The document contains multiple Java programs that check for various types of numbers including Armstrong, Prime, Automorphic, Pronic, Abundant, Niven, Spy, Twisted Prime, Special, Co-Prime, Duck, Buzz, Palindrome, Perfect, and Dudeney numbers. Each program includes user input, logic to determine the type of number, and output statements to inform the user of the result. The programs utilize basic control structures and mathematical operations to perform their checks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Write a program to input a three digit number.

Use a method int


Armstrong(int n) to accept the number. The method returns 1, if the
number is Armstrong, otherwise zero(0).

Sample Output: 153 ⇒ 13 + 53 + 33 = 153


Sample Input: 153

It is an Armstrong Number.

Import [Link].*;
Import [Link].*;
Import [Link].*;
import [Link];

public class ArmstrongNumber


{
public int armstrong (int n) { // creating a function

int num = n, cubeSum = 0;

while (num > 0) {


int digit = num % 10;
cubeSum = cubeSum + (digit * digit * digit);
num /= 10;
}

if (cubeSum == n)
return 1;
else
return 0;
}

public static void main(String args[])


{
Scanner in = new Scanner([Link]);
[Link]("Enter Number: ");
int num = [Link]();

ArmstrongNumber obj = new ArmstrongNumber();


int r = [Link](num);

if (r == 1)
[Link](num + " is an Armstrong number");
else
[Link](num + " is not an Armstrong number");
}
}
Prime-Number
import [Link];

public class KboatPrimeCheck


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
int c = 0;

for (int i = 1; i <= n; i++) {


if (n % i == 0) {
c++;
}
}

if (c == 2) {
[Link](n + " is a prime number");
}
else {
[Link](n + " is not a prime number");
}
}
}

Write a menu driven program to accept a number from the user and
check whether it is a Prime number or an Automorphic number.
(a) Prime number: (A number is said to be prime, if it is only
divisible by 1 and itself)
Example: 3,5,7,11
(b) Automorphic number: (Automorphic number is the number which
is contained in the last digit(s) of its square.)
Example: 25 is an Automorphic number as its square is 625 and 25 is present
as the last two digits.

import [Link];

public class KboatPrimeAutomorphic


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("1. Prime number");
[Link]("2. Automorphic number");
[Link]("Enter your choice: ");
int choice = [Link]();
[Link]("Enter number: ");
int num = [Link]();

switch (choice) {
case 1:
int c = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
c++;
}
}
if (c == 2)
[Link](num + " is Prime");
else
[Link](num + " is not Prime");
break;

case 2:
int numCopy = num;
int sq = num * num;
int d = 0;

/*
* Count the number of
* digits in num
*/
while(num > 0) {
d++;
num /= 10;
}

/*
* Extract the last d digits
* from square of num
*/
int ld = (int)(sq % [Link](10, d));

if (ld == numCopy)
[Link](numCopy + " is automorphic");
else
[Link](numCopy + " is not automorphic");
break;

default:
[Link]("Incorrect Choice");
break;
}
}
}

Pronic numbers
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

public class KboatPronicNumber


{
public void pronicCheck() {
Scanner in = new Scanner([Link]);
[Link]("Enter the number to check: ");
int num = [Link]();

boolean isPronic = false;

for (int i = 1; i <= num - 1; i++) {


if (i * (i + 1) == num) {
isPronic = true;
break;
}
}

if (isPronic)
[Link](num + " is a pronic number");
else
[Link](num + " is not a pronic number");
}
}

An Abundant number is a number for which the sum of its proper


factors is greater than the number itself. Write a program to input a
number and check and print whether it is an Abundant number or
not.
Example:
Consider the number 12.
Factors of 12 = 1, 2, 3, 4, 6 Sum of factors = 1 + 2 + 3 + 4 + 6 = 16
As 16 > 12 so 12 is an Abundant number.
public class KboatAbundantNumber
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number: ");
int n = [Link]();
int sum = 0;

for (int i = 1; i < n; i++) {


if (n % i == 0)
sum += i;
}

if (sum > n)
[Link](n + " is an abundant number");
else
[Link](n + " is not an abundant number");
}
}

Niven number- A number is said to be Niven which is divisible by the


sum of its digits
Example: Sample Input 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.

public class KboatNivenNumber


{
public void checkNiven() {
Scanner in = new Scanner([Link]);
[Link]("Enter number: ");
int num = [Link]();
int orgNum = num;

int digitSum = 0;

while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
}

/*
* digitSum != 0 check prevents
* division by zero error for the
* case when users gives the number
* 0 as input
*/
if (digitSum != 0 && orgNum % digitSum == 0)
[Link](orgNum + " is a Niven number");
else
[Link](orgNum + " is not a Niven number");
}
}

Spy Number- A number is spy if the sum of its digits equals the
product of its digits
Example: Sample Input: 1124
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1*1*2*4 = 8
public class KboatSpyNumber
{
public void spyNumCheck() {

Scanner in = new Scanner([Link]);

[Link]("Enter Number: ");


int num = [Link]();

int digit, sum = 0;


int orgNum = num;
int prod = 1;

while (num > 0) {


digit = num % 10;

sum += digit;
prod *= digit;
num /= 10;
}

if (sum == prod)
[Link](orgNum + " is Spy Number");
else
[Link](orgNum + " is not Spy Number");

}
}
Twisted Prime- if the new number obtained after reversing the digits
is also a prime number.
Sample Input: 167
Sample Output: 761
167 is a 'Twisted Prime'.

public class KboatTwistedPrime


{
public void twistedPrimeCheck() {
Scanner in = new Scanner([Link]);
[Link]("Enter number: ");
int num = [Link]();

if (num == 1) {
[Link](num + " is not a twisted prime number");
}
else {
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}

if (isPrime) {

int t = num;
int revNum = 0;

while (t != 0) {
int digit = t % 10;
t /= 10;
revNum = revNum * 10 + digit;
}

for (int i = 2; i <= revNum / 2; i++) {


if (revNum % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime)
[Link](num + " is a twisted prime number");
else
[Link](num + " is not a twisted prime number");
}

}
}

A special two-digit number is such that when the sum of its digits is
added to the product of its digits, the result is equal to the original
two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59

public class KboatSpecialNumber


{
public void checkNumber() {

Scanner in = new Scanner([Link]);

[Link]("Enter a 2 digit number: ");


int orgNum = [Link]();

int num = orgNum;


int count = 0, digitSum = 0, digitProduct = 1;

while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
digitProduct *= digit;
count++;
}

if (count != 2)
[Link]("Invalid input, please enter a 2-digit number");
else if ((digitSum + digitProduct) == orgNum)
[Link]("Special 2-digit number");
else
[Link]("Not a special 2-digit number");

}
}
Co-Prime numbers-Two numbers whose HCF is 1. Like 14, 15

import [Link];
public class KboatCoprime
{
public void checkCoprime() {

Scanner in = new Scanner([Link]);


[Link]("Enter a: ");
int a = [Link]();
[Link]("Enter b: ");
int b = [Link]();

int hcf = 1;

for (int i = 2; i <= a && i <= b; i++) {


if (a % i == 0 && b % i == 0)
{
hcf = i;
break;
}
}

if (hcf == 1)
[Link](a + " and " + b + " are co-prime");
else
[Link](a + " and " + b + " are not co-prime");
}
}

Duck Number - if the digit zero is (0) present in it. Like 5063, 870
import [Link];
import [Link];
import [Link];

boolean isDuck=false;
if(num>0)
{
if ( (num%10) == 0)
{
isDuck=true;
break;
}
num=num/10;
}
If(isDuck==false)
SOP=not duck no
Else
SOP=Duck
OR

If(isDuck) / if(0) isDuck==false


{
Jkl;;;;
}

If(!isDuck)/if(1) isDuck==false

isDuck=false
isDuck=true

public class KboatDuckNumber


{
public void duckNumberCheck() throws IOException {

InputStreamReader reader = new InputStreamReader([Link]);


BufferedReader in = new BufferedReader(reader);
[Link]("Enter number: ");
boolean isDuck = false;
boolean firstZero = false;
int c = 0, d;

/*
* 10 is the ASCII code of newline
* We will read from inputstream
* one character at a time till we
* encounter a newline i.e. enter
*/
while((d = [Link]()) != 10) {

char ch = (char)d;

if (c == 0 && ch == '0' )
firstZero = true;

if (!firstZero && ch == '0')


isDuck = true;

c++;
}
if (isDuck)
[Link]("Duck Number");
else
[Link]("Not a Duck Number");
}
}

BUZZ Number – divisible by 7 or ends with 7


Buzz Numbers' between p and q (where p<q).
import [Link];

public class KboatBuzzNumber


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter p: ");
int p = [Link]();
[Link]("Enter q: ");
int q = [Link]();
if (p < q) {
[Link]("Buzz Numbers between "
+ p + " and " + q);
for (int i = p; i <= q; i++) {
if (i % 10 == 7 || i % 7 == 0)
[Link](i);
}
}
else {
[Link]("Invalid Inputs!!!");
[Link]("p should be less than q");
}

}
}

Palindrome or a Perfect number.


(a) Palindrome number: (A number is a Palindrome which when read
in reverse order is same as in the right order)
Example: 11, 101, 151 etc.
(b) Perfect number: (A number is called Perfect if it is equal to the
sum of its factors other than the number itself.)
Example: 6 = 1 + 2 + 3

import [Link];

public class KboatPalinOrPerfect


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("1. Palindrome number");
[Link]("2. Perfect number");
[Link]("Enter your choice: ");
int choice = [Link]();
[Link]("Enter number: ");
int num = [Link]();

switch (choice) {
case 1:
int copyNum = num;
int revNum = 0;

while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}

if (revNum == num)
[Link](num + " is palindrome");
else
[Link](num + " is not palindrome");
break;

case 2:
int sum = 0;

for (int i = 1; i <= num / 2; i++) {


if (num % i == 0) {
sum += i;
}
}

if (num == sum)
[Link](num + " is a perfect number");
else
[Link](num + " is not a perfect number");
break;

default:
[Link]("Incorrect Choice");
break;
}
}
}

Dudeney number- a positive integer that is a perfect cube such that the
sum of its digits is equal to the cube root of the number.
Consider the number 512.
Sum of digits = 5 + 1 + 2 = 8
Cube root of 512 = 8

public class KboatDudeneyNumber


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the number: ");
int n = [Link]();

//Check if n is a perfect cube


int cubeRoot = (int)[Link]([Link](n));
if (cubeRoot * cubeRoot * cubeRoot == n) {
//If n is perfect cube then find
//sum of its digits
int s = 0;
int t = n;
while (t != 0) {
int d = t % 10;
s += d;
t /= 10;
}

if (s == cubeRoot) {
[Link](n + " is a Dudeney number");
}
else {
[Link](n + " is not a Dudeney number");
}
}
else {
[Link](n + " is not a Dudeney number");
}
}
}

Tech number - Tech number has even number of digits. If the number is
split in two equal halves, then the square of sum of these halves is equal to
the number itself.
Consider the number 3025
Square of sum of the halves of 3025 = (30 + 25)2
= (55)2
= 3025 is a tech number.

public class TechNumbers


{
public static void main(String args[]) {
for (int i = 1000; i <= 9999; i++) {
int secondHalf = i % 100;
int firstHalf = i / 100;
int sum = firstHalf + secondHalf;
if (i == sum * sum)
[Link](i);
}
}
}

Write a program to display all the numbers between m and n input


from the keyboard (where m<n, m>0, n>0), check and print the
numbers that are perfect square. e.g. 25, 36, 49, are said to be
perfect square numbers.
import [Link];

public class KboatPerfectSquare


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter m: ");
int m = [Link]();
[Link]("Enter n: ");
int n = [Link]();

if (m < n && m > 0 && n > 0) {


for (int i = m; i <= n; i++) {
[Link]("Number = " + i);
double sroot = [Link](i);
if (sroot == [Link](sroot))
[Link](i + " is a perfect square");
}
}
else {
[Link]("Invalid input");
}
}
}

Write a program to input a number and count the number of digits.


The program further checks whether the number contains odd
number of digits or even number of digits.
Sample Input: 749
Sample Output: Number of digits=3
The number contains odd number of digits.

import [Link];

public class KboatDigitCount


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
int dc = 0;

while (n != 0) {
dc++;
n /= 10;
}

[Link]("Number of digits = " + dc);

if (dc % 2 == 0)
[Link]("The number contains even number of digits");
else
[Link]("The number contains odd number of digits");
}
}

Write a program to input a number and display the new number


after reversing the digits of the original number. The program also
displays the absolute difference between the original number and
the reversed number.
Sample Input: 194
Sample Output: 491
Absolute Difference= 297

import [Link];

public class KboatDigitReverse


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Number: ");
int orgNum = [Link]();

int copyNum = orgNum;


int revNum = 0;

while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}

int diff = revNum - orgNum;


[Link]("Reversed Number = " + revNum);
[Link]("Absolute Difference = " + [Link](diff));
}
}

The Greatest Common Divisor (GCD) of two integers is calculated by


the continued division method. Divide the larger number by the
smaller, the remainder then divides the previous divisor. The
process repeats unless the remainder reaches to zero. The last
divisor results in GCD.
Sample Input: 45, 20
Sample Output: GCD=5

import [Link];

public class KboatGCD


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
[Link]("GCD=" + a);
}
}
Write a program to find leap year
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

You might also like