Write a program to Check the automorphic number.
Automorphic Number: An automorphic number is a natural number in a given number base whose square "ends" in the same digits as the number itself.
Example:
Input: num = 5
Output: Number is Automorphic
Approach:
Java
public class AutomorphicNumber {public static void main(String[] args) {int num = 5;if (checkAutomorphicNumber(num)) {System.out.println("Number is Automorphic");} else {System.out.println("Number is not Automorphic");}}// Method to check number is automorphicprivate static boolean checkAutomorphicNumber(int num) {// squre of given numberint squre = num * num;// length of given numberint length = (int) (Math.log10(num) + 1);// find mode of given number based on digit lengthint mod = (int) Math.pow(10, length);// get last digit based on modint lastNum = squre % mod;// check number and last digitif (num == lastNum) {return true;}return false;}}
C++
#include <bits/stdc++.h>using namespace std;//function to check the//automorphic numberbool checkAutomorphicNumber(int num){//square of given numberint squre=num*num;//length of given numberint length=log10(num)+1;//find mod of given number based on no//of digitsint mod=pow(10,length);//get last digits based on mod;int lastNum=squre%mod;//check number with last lastNumif(lastNum==num)return true;elsereturn false;}int main(){int num=5;if(checkAutomorphicNumber(num))cout<<"Number is automorphic\n";elsecout<<"Number is not automorphic\n";return 0;}
C
#include <stdio.h>#include <stdbool.h>#include <math.h>//function to check the//automorphic numberbool checkAutomorphicNumber(int num){//square of given numberint squre = num * num;//length of given numberint length = log10(num) + 1;//find mod of given number based on no//of digitsint mod = pow(10, length);//get last digits based on mod;int lastNum = squre % mod;//check number with last lastNumif (lastNum == num)return true;elsereturn false;}int main(){int num = 5;if (checkAutomorphicNumber(num)){printf("Number is automorphic\n");}else{printf("Number is not automorphic\n");}return 0;}
No comments:
Post a Comment