0% found this document useful (0 votes)
47 views

Assignment No.1

The document contains 11 programming assignments focused on loops and iteration in Python. The assignments cover printing patterns using while and for loops, finding factors of numbers, calculating factorials and sums, checking for palindromes, and printing symmetrical patterns using nested loops. Sample code and outputs are provided for each assignment.

Uploaded by

kwer
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Assignment No.1

The document contains 11 programming assignments focused on loops and iteration in Python. The assignments cover printing patterns using while and for loops, finding factors of numbers, calculating factorials and sums, checking for palindromes, and printing symmetrical patterns using nested loops. Sample code and outputs are provided for each assignment.

Uploaded by

kwer
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment 1: Loops and Iteration

1. Write a program to print the first 10 natural numbers using a while loop.

->

i=1

while(i <= 10):

print(i, end=" ")

i += 1

Output:

1 2 3 4 5 6 7 8 9 10

2. Write a program print the following pattern using while loop.

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

3. Write a program to print the same no 2 pattern using a for loop.

->

for i in range(1, 6):

for j in range(1, i + 1):

print(j, end=" ")

print()

Output:

12

123

1234

12345

4. Write a program to print multiplication table of a given number by the user

->

num = int(input("Enter the number: "))

print("Multiplication Table of", num)

for i in range(1, 11):

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

5. Write a program to print the Fibonacci series.

->

n = int(input("Enter the value of 'n': "))

a=0

b=1

sum = 0

count = 1

print("Fibonacci Series: ", end = " ")

while(count <= n):

print(sum, end = " ")

count += 1

a=b

b = sum

sum = a + b

count += 1
a=b

b = sum

sum = a + b

Output:

Enter the value of 'n': 16

Fibonacci Series: 0 1 3 8 21 55 144 377

6. Write a program to check whether a given user's number is prime or not.

->

number = int(input("Enter any number: "))

if number > 1:

for i in range(2, number):

if (number % i) == 0:

print(number, "is not a prime number")

break

else:

print(number, "is a prime number")

else:

print(number, "is not a prime number")

Output:

Enter a number: 13

13 is a prime number

Enter a number: 10

10 is not a prime number

7. Write a program to find the factorial of a number.

->
def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n-1)

num = int(input("Enter any number: "))

print("Factorial of", num, "is", factorial(num))

Output:

Enter a number: 5

The factorial of 5 is 120

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.

->

num = int(input("Enter any natural number : "))

if num >= 0 :

summ = 0

for i in range(0,num + 1) :

summ = summ + i

print("The sum is {}" .format(summ))

else:

print("Kindly enter a Natural number")

Output:

Enter a number: 16

The sum is 136


9. Write a program to find the sum of all even numbers between 1 to n.

->

n=int(input('Enter a number:'))

sum=0

for x in range(2, n+1, 2):

sum =sum+x

print ('Sum of all even numbers between 1 and ', n, ' is :', sum)

Output:

Enter the number: 10

Sum of all even numbers between 1 and 10 is 30

10.Write a program to check whether a given number is a palindrome or not.

->

def is_palindrome(num):

return str(num) == str(num)[::-1]

num=int(input('Enter a number:'))

if is_palindrome(num):

print(num, "is a palindrome")

else:

print(num, "is not a palindrome")

Output:

Enter a number: 121

121 is a palindrome number

11.Write a program to print the following pattern:

**
***

****

*****

****

***

**

->

n=5

for i in range(1, n+1):

print("* " * i)

for i in range(n-1, 0, -1):

print("* " * i)

Output:

**

***

****

*****

****

***

**

You might also like