Thursday, April 29, 2021

BlueJ Program On Tech Number Checking


A 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. Write a program to generate and print all four digit tech numbers. 

Example :
Consider the number 3025
Square of sum of the halves of 3025 = (30+25)2
= (55)2
= 3025 is a tech number.


import java.util.*;
class A2
{
int a,b,c=0,digit=0,i;
Scanner sc=new Scanner(System.in);
void take()
{
    System.out.print("\nEnter the number: ");
    a=sc.nextInt();
    for(i=a;i>0;i=i/10)
    {
        digit++;
    }
     digit=digit/2;
     for(i=a;i>0;i=i/10)
     {
       c=c*10+i%10;
       digit--;
       if(digit==0)
       {
           i=i/10;
       break;
    }
    }
    b=i;
    c=rev(c);
        if(a==Math.pow((b+c),2))
    
    System.out.print(a + " is a tech number.");
    else
    System.out.print(a + " is not a tech number.");
}
 private int rev(int n)
 {
     int r=0;
     while(n>0)
     {
          r=r*10+n%10;
          n=n/10;
        }
        return r;
    }
    
 public static void main(String args[])
     {
        A2 ob=new A2();
        ob.take();
       
    }
}


Same program using another technique.

  
import java.util.*;
class A2
{
int b,c=0,digit;
String str1,s1,s2;
Scanner sc=new Scanner(System.in);
void take()
{
    System.out.print("\nEnter the number: ");
    str1=sc.nextLine();
    digit=str1.length();
     digit=digit/2;
     s2=str1.substring(digit);
     s1=str1.substring(0,digit);
     
    b=Integer.parseInt(s1);
    c=Integer.parseInt(s2);
    
    if(Integer.parseInt(str1)==Math.pow((b+c),2))
    
    System.out.print(str1 + " is a tech number.");
    else
    System.out.print(str1 + " is not a tech number.");
}
    
 public static void main(String args[])
     {
        A2 ob=new A2();
        ob.take();
       
    }
}

Other Programs On Numbers: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner