ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 9
Programming for school students ( upper to middle/secondary school)
A tech number has even number of digits. If the number is split in two equal halves, then the square of the sum of these halves is equal to the number itself. Write a program to generate and print all four digits tech numbers.
JAVA CODE
public class ISCJava{
public static void main(String[] args) {
final int j = 2;
int counter;
int lastTwoDigit;
int firstTwoDigit;
int temp;
for( int i = 1000 ; i < 10000 ; i++) { // looping through all 4-digits
counter = 0;
lastTwoDigit = 0;
firstTwoDigit = 0;
temp = i;
// extracting the last two digit
while ( counter < j) {
lastTwoDigit += (temp%10)*(int)Math.pow(10,counter);
counter++;
temp /= 10;
}
// extracting the first two digit
counter = 0;
while ( counter < j) {
firstTwoDigit += (temp%10)*(int)Math.pow(10,counter);
counter++;
temp /= 10;
}
if( i == (int)Math.pow(firstTwoDigit+lastTwoDigit,2))
System.out.println(i+" is a tech number.");
}
}
}
PYHTON CODE
#PROGRAM TO PRINT ALL 4 DIGITS TECH NUMBER
j = 2
for i in range(9000): #total number of 4 digits number are 9000
counter = 0
lastTwoDigit = 0
firstTwoDigit = 0
temp = i+1000
numb = temp
#extracting the last two digit
while counter < j:
lastTwoDigit += int(temp%10*int((10**counter)))
temp /= 10
temp = int(temp)
counter +=1
#extracting the last two digit
counter = 0
while counter < j:
firstTwoDigit += int(temp%10*int((10**counter)))
temp /= 10
temp = int(temp)
counter +=1
#print(firstTwoDigit,"last",lastTwoDigit,numb)
if numb == int((firstTwoDigit+lastTwoDigit)**2):
print(numb," is a tech number.")
No comments:
Post a Comment