0% found this document useful (0 votes)
107 views28 pages

Class 11 Computer Science Tasks

Uploaded by

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

Class 11 Computer Science Tasks

Uploaded by

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

COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL

TEACHER- SURENDERYADAV

Print all Integers that Aren’t Divisible by


Either 2 or 3 and Lie between 1 and 50.

Input
for i in range(0,51):
if(i%2!=0&i%3!=0):
print(i)

Output

Case 1:
1
5
7
11
13
17
19
23
25
29
31
35
37
41
43
47
49

Page 1 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Print Odd Numbers Within a Given Range

Input

lower=int(input("Enter the lower limit for the range:"))


upper=int(input("Enter the upper limit for the range:"))
for i in range(lower,upper+1):
if(i%2!=0):
print(i)

Output
Enter the lower limit
for the range:1
Enter the upper limit
for the range:16
1
3
5
7
9
11
13
15

Page 2 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Find the Sum of Digits in a Number

Input
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)

Output
Case 1:
Enter a number:1892
The total sum of digits is: 20

Page 3 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Find the Smallest Divisor of an Integer

Input
n=int(input("Enter an integer:"))
a=[]
for i in range(2,n+1):
if(n%i==0):
a.append(i)
a.sort()
print("Smallest divisor is:",a[0])

Output

Enter an integer:75
Smallest divisor is: 3

Page 4 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Count the Number of Digits in a Number

Input
n=int(input("Enter number:"))
count=0
while(n>0):
count=count+1
n=n//10
print("The number of digits in the number are:",count)

Output
Enter number:123
The number of digits in the
number are: 3

Page 5 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Check Whether a Given Year is a Leap Year

Input
year=int(input("Enter year to be checked:"))
if(year%4==0 and year%100!=0 or year%400==0):
print("The year is a leap year!)
else:
print("The year isn't a leap year!)

Output
Case 1:
Enter year to be checked:2016
The year is a leap year!

Case 2:
Enter year to be checked:2005
The year isn't a leap year!

Page 6 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Read Height in Centimeters and then


Convert the Height to Feet and Inches

Input
cm=int(input("Enter the height in centimeters:"))
inches=0.394*cm
feet=0.0328*cm
print("The length in inches",round(inches,2))
print("The length in feet",round(feet,2))

Output
Case 1:
Enter the height in centimeters:50
The length in inches 19.7
The length in feet 1.64

Case 2:
Enter the height in centimeters:153
The length in inches 60.28
The length in feet 5.02

Page 7 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Find the Smallest Divisor of an Integer


Input

n=int(input("Enter an integer:"))
a=[]
for i in range(2,n+1):
if(n%i==0):
a.append(i)
a.sort()
print("Smallest divisor is:",a[0])

Output
Case 1:
Enter an integer:75
Smallest divisor is: 3

Case 2:
Enter an integer:64
Smallest divisor is: 2

Page 8 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Generate all the Divisors of an Integer

Input
n=int(input("Enter an integer:"))
print("The divisors of the number are:")
for i in range(1,n+1):
if(n%i==0):
print(i)

Output
Case 1:
Enter an integer:25
The divisors of the number are:
1
5
25

Case 2:
Enter an integer:20
The divisors of the number are:
1
2
4
5
10
20

Page 9 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Find the LCM of Two Numbers

Input
a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))
if(a>b):
min1=a
else:
min1=b
while(1):
if(min1%a==0 and min1%b==0):
print("LCM is:",min1)
break
min1=min1+1

Output
Case 1:
Enter the first number:5
Enter the second number:3
LCM is: 15

Case 2:
Enter the first number:15
Enter the second number:20
LCM is: 60

Page 10 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Find the Sum of First N Natural Numbers

Input
n=int(input("Enter a number: "))
sum1 = 0
while(n > 0):
sum1=sum1+n
n=n-1
print("The sum of first n natural numbers is",sum1)

Output
Case 1:
Enter a number: 18
The sum of first n natural numbers is 171

Case 2:
Enter a number: 167
The sum of first n natural numbers is
1402

Page 11 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Find all Numbers in a Range which are


Perfect Squares and Sum of all Digits in the
Number is Less than 10

Input
l=int(input("Enter lower range: "))
u=int(input("Enter upper range: "))
a=[]
a=[x for x in range(l,u+1) if (int(x**0.5))**2==x and
sum(list(map(int,str(x))))<10]
print(a)

Output
Case 1:
Enter lower range: 1
Enter upper range: 40
[1, 4, 9, 16, 25, 36]

Case 2:
Enter lower range: 50
Enter upper range: 100
[81, 100]
Page 12 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Determine Whether a Given Number is


Even or Odd Recursively

Input
def check(n):
if (n < 2):
return (n % 2 == 0)
return (check(n - 2))
n=int(input("Enter number:"))
if(check(n)==True):
print("Number is even!")
else:
print("Number is odd!"

Output
Case 1:
Enter number:124
Number is even!

Case 2:
Enter number:567
Number is odd! Page 13 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Find the Binary Equivalent of a Number


Recursively

Input
l=[]
def convert(b):
if(b==0):
return l
dig=b%2
l.append(dig)
convert(b//2)
a=int(input("Enter a number: "))
convert(a)
l.reverse()
print("Binary equivalent:")
for i in l:
print i,

Output
Page 14 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Case 1:
Enter a number: 20
Binary equivalent:
1 0 1 0 0

Case 2:
Enter a number: 7
Binary equivalent:
1 1 1

Find the Sum of the Digits of the Number


Recursively

Input
l=[]
def sum_digits(b):
if(b==0):
return l
dig=b%10
l.append(dig)
sum_digits(b//10)
n=int(input("Enter a number: "))
sum_digits(n)
print(sum(l))

Output

Page 15 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Case 1:
Enter a number: 135
9

Case 2:
Enter a number: 546
15

Program to find the factorial of a number


without recursion

Input
n=int(input("Enter number:"))
fact=1
while(n>0):
fact=fact*n
n=n-1
print("Factorial of the number is: ")
print(fact)

Output
Case 1:
Enter number:5 Page 16 of 28
NAME- DHRUVofANAND
Factorial the number is:
CLASS-11TH B
120
ROOL NO- 07
Case 2:
Enter number:4
Factorial of the number is:
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Find the Sum of Digits in a Number without


Recursion

Input
l=[]
b=int(input("Enter a number: "))
while(b>0):
dig=b%10
l.append(dig)
b=b//10
print("Sum is:")
print(sum(l))

Page 17 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Output

Case 1:
Enter a number: 123
Sum is:
6

Case 2:
Enter a number: 567
Sum is:
18

Read a Number n and Print the Natural


Numbers Summation Pattern

Input
n=int(input("Enter a number: "))
for j in range(1,n+1):
a=[]
for i in range(1,j+1):
print(i,sep=" ",end=" ")
if(i<j):
print("+",sep=" ",end=" ")
a.append(i)
Page 18 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV
print("=",sum(a))

print()

Output
Case 1:
Enter a number: 4
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10

Case 2:
Enter a number: 5
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15

Read the Contents of a File in Reverse


Order

Input
filename=input("Enter file name: ")
for line in reversed(list(open(filename))):
print(line.rstrip())

Page 19 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Output
Case 1:
Contents of file:
hello world
hello

Output:
Enter file name: read.txt
hello
hello word

Case 2:
Contents of file:
line1
line2
line3

Output:
Enter file name: read2.txt
line3
line2
line1

Find the Area of a Rectangle Using Classes

Input
class rectangle():
def __init__(self,breadth,length):
self.breadth=breadth
self.length=length
def area(self):
return self.breadth*self.length
Page 20 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV
a=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=rectangle(a,b)
print("Area of rectangle:",obj.area())

print()

Output
Case 1:
Enter length of rectangle: 4
Enter breadth of rectangle: 5
Area of rectangle: 20

Case 2:
Enter length of rectangle: 15
Enter breadth of rectangle: 13
Area of rectangle: 195

Page 21 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Page 22 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Page 23 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Page 24 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Page 25 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Page 26 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Page 27 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07
COMPUTER SCIENCE NALANDA MODERN PUBLIC SCHOOL
TEACHER- SURENDERYADAV

Page 28 of 28
NAME- DHRUV ANAND
CLASS-11TH B
ROOL NO- 07

You might also like