Program 23 - CSU1128 - CSE 2026 - Shoolini University

Program to print all numbers which four digit perfect square and where its first two digit and last two digits are also a perfect square. Example 1681

Explanation

The program prints all four-digit numbers which are perfect squares and where the first two digits and the last two digits are also perfect squares.

Header

The code begins with including the standard input-output header files using #include <stdio.h> and #include <math.h> for mathematical functions.

Program Description

The program prints a description message for the user to understand its functionality.

Perfect Square Calculation

The program finds and prints all special four-digit perfect square numbers using a for loop:

  • for (d = 1000; d <= 9999; d++): Loops through each number from 1000 to 9999.
  • int m = sqrt(d);: Calculates the square root of the current number d.
  • if (m * m == d): Checks if the square of the square root is equal to the original number, indicating a perfect square.
  • int j = d / 100;: Extracts the first two digits of the number d.
  • int o = d % 100;: Extracts the last two digits of the number d.
  • int n = sqrt(j);: Calculates the square root of the first two digits.
  • int e = sqrt(o);: Calculates the square root of the last two digits.
  • if (n * n == j && e * e == o): Checks if the first two digits and the last two digits are perfect squares.
  • printf("\t %d \t", d);: Prints the current number if it meets all the criteria.

Program End

The program ends with printf("\n"); to print a new line and return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
#include <math.h>
int main()
{
    int d;
    printf("\n\n Program to print all numbers which four digit perfect square and where");
    printf("its first two digit and last two digits are also a perfect square. Example 1681. \n\n");

    printf("All four digit special perfect square numbers between 1111 and 9999 are:\n");
    for (d = 1000; d <= 9999; d++)
    {
        int m = sqrt(d); // Take root of i
        if (m * m == d)
        {
            int j = d / 100;
            int o = d % 100;
            int n = sqrt(j);
            int e = sqrt(o);

            if (n * n == j && e * e == o)
            {
                printf("\t %d \t", d);
            }
        }
    }
    printf("\n");
    return 0;
}                    
                

Output

Program to print all numbers which four digit perfect square and where its first two digit and last two digits are also a perfect square. Example 1681.

All four digit special perfect square numbers between 1111 and 9999 are:
1600 1681 2500 3600 4900 6400 8100