[go: up one dir, main page]

0% found this document useful (0 votes)
306 views3 pages

Disarium Number

This document contains code snippets for 3 Java programs: 1) A Disarium number checker that takes user input, sums the digits raised to the number of digits, and checks if it equals the original number. 2) A Happy number checker that takes a number, sums the squares of its digits repeatedly until reaching 1 (making it happy) or other number (not happy). 3) A pattern printer that takes user input for number of rows, and prints the numbers 1 to the row number in the upper half and lower half of the pattern.

Uploaded by

ADI
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)
306 views3 pages

Disarium Number

This document contains code snippets for 3 Java programs: 1) A Disarium number checker that takes user input, sums the digits raised to the number of digits, and checks if it equals the original number. 2) A Happy number checker that takes a number, sums the squares of its digits repeatedly until reaching 1 (making it happy) or other number (not happy). 3) A pattern printer that takes user input for number of rows, and prints the numbers 1 to the row number in the upper half and lower half of the pattern.

Uploaded by

ADI
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
You are on page 1/ 3

DISARIUM NUMBER

import java.util.Scanner;

class Number
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Input a number : ");
int num = sc.nextInt();
int copy = num, d = 0, sum = 0;
String s = Integer.toString(num);
int len = s.length();

while(copy>0)
{
d = copy % 10;
sum = sum + (int)Math.pow(d,len);
len--;
copy = copy / 10;
}

if(sum == num)
System.out.println("Disarium Number.");
else
System.out.println("Not a Disarium Number.");
}
}

happy number
import java.util.*;

class HappyNumber

public static void main(String[]args)

int num,sum=0,a,b;

Scanner sc = new Scanner(System..in);

System.out.print("Enter a number : " );// accepting the value

num = sc.nextInt();

b=num;// accepted value is given back b;

sum=num;

do{

num=sum;sum=0;// num gets the sum's value. Sum becomes 0.


do{

a=num%10;

sum+=a*a;

num/=10;

}while(num>0);// checking the value of num if more than 0 then loop.

}while(sum>9);// checking the value of num if more than 9 then loop.

if(sum==1)// if the end value is 1 then it is a happy number

System.out.println(b+" is a Happy number");

else

System.out.println(b+" is not a happy number ");

}// end of main

}//end of class

pattern
import java.util.Scanner;

class MainClass

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern....!!!");

//Printing upper half of the pattern

for (int i = rows; i >= 1; i--)

for (int j = 1; j <= i; j++)

{
System.out.print(j+" ");

System.out.println();

//Printing lower half of the pattern

for (int i = 2; i <= rows; i++)

for (int j = 1; j <= i; j++)

System.out.print(j+" ");

System.out.println();

sc.close();

You might also like