write a python program to print the numbers
from 1 to n
where n is obtained as input from
the
user. while printing the numbers, the prime
numbers should be replaced with square of it .
For example if input n=25 then out put should
be
1 4 9 425.
Answers
Answered by
2
The given code is written in Python.
def checkPrime(n):
c=0
for i in range(1,n+1):
if n%i==0:
c+=1
if c==2:
return True
return False
n=int(input("Enter n - "))
for i in range(1,n+1):
if checkPrime(i):
print(i * i,end=" ")
else:
print(i,end=" ")
The checkPrime(n) function checks whether a number is prime or not. The value of n is taken as input. Inside the loop, this function is called. If the number is prime, square of the number is printed or else, the original number is printed.
See the attachment for output.
Attachments:
Similar questions