Computer Science, asked by netaijana686, 1 year ago

Write a program to find the four digit numbers, which are perfect squares, and all the digits in that number are even.

Answers

Answered by rakeshchennupati143
53

--You haven't told me in which program i have to write so i wrote in java,python java code can also work in c or c++ with changing the out statements

Program-in-java:

public class Main{

  public static void main(String args[]){

     int rem;

     int[] arr = new int[4];

     for(int i = 1000 ; i < 10000 ; i++){

        for(int j = 32 ; j<101 ; j++ ){

           if( i == (j*j)){

              int k = 0,temp = i;

              while(temp>0){

                 arr[k] = temp % 10;

                 temp = temp / 10;

                 k++;

              }

              if(arr[0]%2==0&&arr[1]%2==0&&arr[2]%2==0&&arr[3]%2==0){

                 System.out.print(i+"\t");

              }

           }

        }

     }

  }

}

Output:

4624    6084    6400    8464

Program-in-python:

for i in range(1000,10000,1):

   for j in range(32,100,1):

       if i == j*j:

           string = str(i)

           if int(string[0])%2 == 0 and int(string[1])%2 == 0 and int(string[2])%2 == 0 and int(string[3])%2 == 0:

               print(i)

Output:

4624

6084

6400

8464

Explanation:

  • if you clearly see i used a outer for loop for getting all 4 digit numbers
  • and i used inner loop to check which of the 3 digit number have 4 digit number as its square so if you can see i took 32 from starting because
  • 31x31 = 961 which is not a 4 digit number
  • 32x32 = 1024 so a perfect square of a four digit number will be above than 32
  • and i took range 100 because 99x99 = 9801 which is last a 4 digit number have a perfect square.
  • so i checked i with j*j and if that is true then i took each digit and checked if that is even or not if all the digits are even is true the i printed the number
  • or else the loop iterates until all the numbers are printed and conditions breaks;

----Hope you liked my programs, if you liked it mark as brainliest, it would really help.   :)

Answered by qwvilla
2

A program to find the four-digit numbers, which are perfect squares, and all the digits in that number are even:

  • #include <iostream>

        using namespace std;

        bool isEven(int n){

        int r,c=0;

        while(n>0){

        r = n%10;

        if(r%2==0){

           c++;

        }

        n = n/10;

     }

       if(c==4){

       return true;

      }

     else{

          return false;

        }

    }

     bool isPerfectSquare(int n)

    {

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

             if ((n % i == 0) && (n / i == i)) {

                return true;

         }

     }

        return false;

  }

   int main()

   {

        int i;

        for(i=1000; i<10000; i++){

         if(isPerfectSquare(i) && isEven(i)){

              cout<<i<<endl;

         }    

     }

       return 0;

    }

What are perfect squares?

  • Perfect squares are numbers that give a result of the product when we perform multiplication to itself that number. These numbers or integers can be negative, positive, or zero.
  • The perfect square of 0 is 0, the perfect square of 1 is 1, the perfect square of 2 is 4. This we get by multiplying the number with itself like 0×0=0, 1×1=1, 2×2=4.
Similar questions