Number Programs in java part-4
for more java programs https://wordpress-343193-1101484.cloudwaysapps.com/double-dimensional-arraymatrix-programs-part-i
In this post, we will discuss some basic and simple number programs in java .
for more java programs
Double Dimensional Array(Matrix) Program I (Maximum minimum element, sum, average)
- A program to calculate GCD(greatest common divisor) of two numbers:-
The greatest common divisor (gcd) of two or more integers, is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4.
import java.util.*;
class gcd{
public static void main(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 numbers:");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("\n ****Calculating GCD****\n");
int t=0;
if(a>b){
t=a;
a=b;
b=t;
}
if(a==0){
System.out.println("GCD:"+a);
b=0;
}
while(b!=0){
t=0;
if(b%a==0){
System.out.println("GCD:"+a);
break;
}
else{
t=b%a;
b=a;
a=t;
}
}
}
}
output:-
Enter 2 numbers
12 8
****Calculating GCD****
GCD 4
2. Automorphic Number:-
An automorphic number is a number whose square "ends" in the same digits as the number itself. For example, 52 = 25, 62 = 36
import java.util.*;
class automorphic{
public static void main(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number:");
int n=sc.nextInt();
double sq=Math.pow(n,2);
int t=n,c=0,d=0;
while(t>0){
d=t%10;
c=c+1;;
t/=10;
}
double s=Math.pow(10,c);
int r=(int)sq%(int)s;
if(r==n){
System.out.println("It is automorphic.");
}
else{
System.out.println("Not automorphic.");
}
}
}

For videos related to java programming
http://www.youtube.com/user/trushnatej
Related Posts
Continue learning with these related articles

String Methods in Java for ICSE Class 10: 25 Output Questions with Answers
ICSE Class 10 Computer Applications practice on String class methods: length, charAt, indexOf, lastIndexOf, substring, replace, trim, compareTo, equals and more, with 25 board-style output questions and step-by-step answers.

Special Number Programs in Java for ICSE: Armstrong, Spy, Neon, Disarium, Pronic and More
Every special number asked in ICSE Computer Applications – prime, perfect, Armstrong, palindrome, automorphic, neon, spy, Niven, duck, buzz, Disarium, special and pronic numbers – with definitions, examples and one complete Java program that checks them all.

Math Library Methods in Java for ICSE Class 9 (with Outputs)
ICSE Class 9 Computer Applications notes on the Java Math class: sqrt, cbrt, pow, abs, max, min, ceil, floor, round, rint and random, with return types, tricky negative-number outputs and Java expressions for formulas.