Assignment No.1
Assignment No.1
Assignment No.1
1. Write a program to print the first 10 natural numbers using a while loop.
->
i=1
i += 1
Output:
1 2 3 4 5 6 7 8 9 10
12
123
1234
12345
->
i=1
while i<=5:
j=1
while j <=i:
print(j,end=" ")
j+=1
print("\n")
i+=1
Output:
1
12
123
1234
12345
->
print()
Output:
12
123
1234
12345
->
print(num,"X",i,"=",num * i)
Output:
Enter a number: 9
9x1=9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
->
a=0
b=1
sum = 0
count = 1
count += 1
a=b
b = sum
sum = a + b
count += 1
a=b
b = sum
sum = a + b
Output:
->
if number > 1:
if (number % i) == 0:
break
else:
else:
Output:
Enter a number: 13
13 is a prime number
Enter a number: 10
->
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
Output:
Enter a number: 5
Enter a number: 0
The factorial of 0 is 1
8. Write a program to find the sum of natural numbers entered by the user.
->
if num >= 0 :
summ = 0
for i in range(0,num + 1) :
summ = summ + i
else:
Output:
Enter a number: 16
->
n=int(input('Enter a number:'))
sum=0
sum =sum+x
print ('Sum of all even numbers between 1 and ', n, ' is :', sum)
Output:
->
def is_palindrome(num):
num=int(input('Enter a number:'))
if is_palindrome(num):
else:
Output:
**
***
****
*****
****
***
**
->
n=5
print("* " * i)
print("* " * i)
Output:
**
***
****
*****
****
***
**