0% found this document useful (0 votes)
10 views21 pages

Program List 3

The document contains a list of programming exercises focused on basic concepts in computer science, particularly using loops in Python. Each question provides a task, input requirements, and example code for both while and for loops to achieve the desired output. The exercises cover topics such as printing natural numbers, calculating sums, checking for prime numbers, and generating patterns.

Uploaded by

aishwaryath903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

Program List 3

The document contains a list of programming exercises focused on basic concepts in computer science, particularly using loops in Python. Each question provides a task, input requirements, and example code for both while and for loops to achieve the desired output. The exercises cover topics such as printing natural numbers, calculating sums, checking for prime numbers, and generating patterns.

Uploaded by

aishwaryath903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Computer Science File

Program List 3

Aishwarya Thakur
Q1) Write a program to print all natural numbers fro, 1 to n using:
(a)​ While loop
Input:
n=int(input("Enter Number"))
i=1
while(i<=n):
print(i)
i=i+1

Output:

(b)​ for loop


Input:
n=int(input("Enter the limit"))
for i in range(1,n+1):
print(i)

Output:
Q2) Write a program to print all natural numbers from n to 1 in reverse
order using:
(a)​ while loop
Input:
n=int(input("Limit"))
while(n>0):
print(n)
n=n-1

Output:

(b)​ for loop


Input:
n=int(input("Enter the limit"))
for i in range(n,0,-1):
print(i)
​ Output:

Q3) Write a program to print all even numbers between 1 to n using:


(a)​ while loop
Input:
n=int(input("Limit"))
i=0
while(i<=n):
print(i)
i=i+2

Output:

(b)​ for loop


Input:
n=int(input("Limit"))
for i in range(0,n+1,2):
print(i)
Output:

Q4) Write a program to print all odd numbers between 1 to n using:


(a)​ while loop
Input:
n=int(input("Limit"))
i=1
while(i<=n):
print(i)
i=i+2

Output:

(b)​ for loop


Input:
n=int(input("Limit"))
for i in range(1,n+1,2):
print(i)

Output:
Q5) Write a program to find sum of all natural number between 1 to n
using:
(a)​ while loop
Input:
n=int(input("Enter number"))
c=0
i=1
while(i<=n):
c=i+c
i=i+1
print(c)

Output:

(b)​ for loop


Input:
n=int(input("Enter number"))
s=0
for i in range(1,n+1):
s=s+i
print(s)

Output:

Q6) Write a program to find sum of all even numbers between 1 to n


using:
(a)​ while loop:
Input:
n=int(input("Limit:"))
c=0
i=0
while(i<=n):
c=c+i
i=i+2
print(c)

Output:

(b)​ for loop


Input:
n=int(input("Limit"))
c=0
for i in range(0,n+1,2):
c=c+i
print(c)

Output:

Q7) Write a program to find sum of all even numbers between 1 to n


using:
(c)​ while loop:
Input:
n=int(input("Limit:"))
c=0
i=1
while(i<=n):
c=c+i
i=i+2
print(c)

Output:

(d)​ for loop


Input:
n=int(input("Limit"))
c=0
for i in range(1,n+1,2):
c=c+i
print(c)

Output:

Q8) Write a program to print multiplication table of any number using:


(a)​ while loop
Input:
n=int(input("Enter a number:"))
i=1
while (i<= 10):
c=n*i
print(c)
i=i+1

Output:

(b)​ for loop


Input:
n=int(input("Number:"))
for i in range(1,11):
c=n*i
print(c)

Output:
Q9) Write a program to count the number of digits in a number using
while loop.

Input:
n=int(input("Enter a number:"))
c=0
while(n>0):
n=n//10
c=c+1
print("The number of digits is:",c)

Output:

Q10) Write a program to calculate the sum of digits of a number using


while loop:
Input:
n=int(input("Enter a number: "))
s=0
while n>0:
r=n%10
s=s+r
n=n//10
print("Sum of digits:",s)

Output:
Q11) Write a program to calculate the product of digits of a number
using while loop.
Input:
n=int(input("Enter a number: "))
s=1
while(n>0):
a=n%10
n=n//10
s=s*a
print("Product of digits:",s)

Output:

Q12) Write a program to accept 10 numbers from the user and display
their average.
Input:
t=0
c=10
for i in range(c):
n=int(input("Enter number: "))
t=t+n
a=t/c
print("Average is: ",a)

Output:
Q13) Write a program to check whether the three digit number is an
armstrong number or not using a while loop.
Input:
n=int(input("Enter a number: "))
s=0
y=n
while(n>0):
a=n%10
n=n//10
c=a**3
s=s+c
if(y==s):
print("Armstrong number")
else:
print("Not an Armstrong number")

Output:

Q14) Write a program to find the factorial of any number using:


(a)​ while loop
Input:
n=int(input("Enter a number: "))
f=1
i=1
while(i<=n):
f=f*i
i=i+1
print("Factorial of",n,"is",f)

Output:

(b)​ for loop


Input:
n=int(input("Enter a number: "))
f=1
for i in range(1,n+1):
f=f*i
i=i+1
print("Factorial of",n,"is",f)

Output:

Q15) Write a program to enter a number and print its reverse using while
loop.
Input:
n=int(input("Enter a number: "))
o=n
r=0
while(n>0):
d=n%10
r=r*10+d
n=n//10
print("The reverse of",o,"is",r)

Output:

Q16) Write a program to check whether a number is a palindrome or not


using while loop.
Input:
n=int(input("Enter a number: "))
o=n
r=0
while(n>0):
d=n%10
r=r*10+d
n=n//10
if(o==r):
print("The number is a palindrome")
else:
print("The number is not a palindrome")

Output:

Q17) Write a program to print fibonacci series up to n terms using while


loop.
Input:
n=int(input("Enter the number of terms:"))
a=0
b=1
c=0
while(c<n):
print(a, end="")
d=a+b
a=b
b=d
c=c+1
if(c<n):
print(",",end="")

Output:

Q18) Write a program to print the following patterns using for and while
loop.
(a)
*
**
***
****
(i) for loop:
Input:
for i in range(1,5):
print("*" * i)

Output:

(ii) while loop


Input:
n = 4
i = 1
while i<=n:
print("*" * i)
i=i+1
Output:

(b)
1
12
123
1234
(i) for loop:
Input:
for i in range(1,5):
for j in range(1,i+1):
print(j, end="")
print()

Output:
(ii) while loop
Input:
i=1
while i <= 4:
j = 1
while j <= i:
print(j, end="")
j=j+1
print()
i=i+1
Output:

(c)
2
44
666
8888
(i) for loop:
Input:
n = 2
for i in range(1, 5):
for j in range(i):
print(n, end="")
print()
n = n + 2

Output:

(ii) while loop


Input:
n = 2
i = 1
while i <= 4:
j = 1
while j <= i:
print(n, end="")
j += 1
print()
n=n+2
i=i+1

Output:

(d)
1
21
321
4321
(i) for loop:
Input:
n = 2
for i in range(1, 5):
for j in range(i):
print(n, end="")
print()
n = n + 2

Output:

(ii) while loop


Input:
i = 1
while i <= 4:
j = i
while j >= 1:
print(j, end="")
j = j - 1
print()
i = i + 1

Output:

(e)
1234
123
12
1
(i) for loop:
Input:
for i in range(4, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()

Output:

(ii) while loop


Input:
i = 4
while i >= 1:
j = 1
while j <= i:
print(j, end="")
j = j + 1
print()
i = i - 1
Output:

(f)
A
BC
DEF
GHIJ
(i) for loop:
Input:
ch = 65
for i in range(1, 5):
for j in range(i):
print(chr(ch), end="")
ch = ch + 1
print()

Output:

(ii) while loop


Input:
i = 1
ch = 65
while i <= 4:
j = 1
while j <= i:
print(chr(ch), end="")
ch = ch + 1
j = j + 1
print()
i = i + 1

Output:
(g)
A
BB
CCC
DDDD
(i) for loop:
Input:
ch = 65
for i in range(1, 5):
for j in range(i):
print(chr(ch), end="")
print()
ch = ch + 1

Output:

(ii) while loop


Input:
i = 1
ch = 65
while i <= 4:
j = 1
while j <= i:
print(chr(ch), end="")
j = j + 1
print()
ch = ch + 1
i = i + 1

Output:
(h)
A
AB
ABC
ABCD
(i) for loop:
Input:
for i in range(1, 5):
for j in range(65, 65 + i):
print(chr(j), end="")
print()

Output:

(ii) while loop


Input:
i = 1
while i <= 4:
j = 65
while j < 65 + i:
print(chr(j), end="")
j = j + 1
print()
i = i + 1

Output:
Q19) Write a program to print the following series using while loop- 1 4
7 10……
Input:
n = int(input("Enter the number of terms: "))
i = 1
c = 1
while c <= n:
print(i, end=" ")
i = i + 3
c = c + 1

Output:

Q20) Write a program to check whether a given number is prime or not.


Input:
n = int(input("Enter a number: "))
if n <= 1:
print("Not prime")
else:
i = 2
flag = 0
while i < n:
if n % i == 0:
flag = 1
break
i = i + 1
if flag == 0:
print("Prime")
else:
print("Not prime")

Output:

Q21) Write a program to generate numbers from 2 to n.


Input:
n = int(input("Enter a number: "))
i = 2
while i <= n:
print(i, end=" ")
i = i + 1

Output:

You might also like