Sunday 22 September 2024

Practical - 74 - Write a Python program to receive numbers from user through keyboard until user inputs 0 to end the input process, then the program calculates and displays the sum of given odd numbers and even numbers respectively.

Practical - 74 - Write a Python program to receive numbers from user through keyboard until user inputs 0 to end the input process, then the program calculates and displays the sum of given odd numbers and even numbers respectively.


Ans:


sum_odd = 0

sum_even = 0

while True:

    num = int(input("Enter a number (0 to stop): "))

    if num == 0:

        break

    if num % 2 == 0:

        sum_even += num

    else:

        sum_odd += num

print("Sum of odd numbers: ",sum_odd)

print("Sum of even numbers: ",sum_even)

 

Output:

Enter a number (0 to stop): 3

Enter a number (0 to stop): 7

Enter a number (0 to stop): 8

Enter a number (0 to stop): 9

Enter a number (0 to stop): 6

Enter a number (0 to stop): 4

Enter a number (0 to stop): 0

Sum of odd numbers:  19

Sum of even numbers:  18


Saturday 21 September 2024

Practical - 73 - Write a Python program to print the sum of first n odd natural numbers where the value of n is taken as input from the user.

Practical - 73 - Write a Python program to print the sum of first n odd natural numbers where the value of n is taken as input from the user.


Ans:

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

sum=0

for i in range(1, 2*n, 2):

    sum+=i

print("The sum of the first",n,"odd natural numbers is:",sum)

 

Output:

Enter the value of n: 3

The sum of the first 3 odd natural numbers is: 9


OR


Enter the value of n: 5

The sum of the first 5 odd natural numbers is: 25

Practical - 72 - Write a Python program that accepts the roll number and marks of the student in two subjects and displays the grade of the student

Practical - 72 - Write a Python program that accepts the roll number and marks of the student in two subjects and displays the grade of the student as per the following criteria

Percentage>60 : Grade A,

Percentage<=60 and Percentage>=33: Grade B

Percentage<33: Reappear 


Ans:

rn=int(input("Enter the roll number of student: "))

marks1=int(input("Enter marks in Subject 1: "))

marks2=int(input("Enter marks in Subject 2: "))

total_marks=200

percentage=(marks1+marks2)*100/total_marks

print("Roll Number of student: ", rn)

if percentage > 60:

    print("Grade A")

elif 33 <= percentage <= 60:

    print("Grade B")

else:

    print("Reappear")

 

Output:

Enter the roll number of student: 1

Enter marks in Subject 1: 75

Enter marks in Subject 2: 82

Roll Number of student:  1

Grade A


OR


Enter the roll number of student: 2

Enter marks in Subject 1: 35

Enter marks in Subject 2: 46

Roll Number of student:  2

Grade B


OR


Enter the roll number of student: 3

Enter marks in Subject 1: 16

Enter marks in Subject 2: 28

Roll Number of student:  3

Reappear

Thursday 19 September 2024

Practical - 71 - Write a program in python to print all the numbers between 1 to 100 which are either divisible by 3 or 5.

Practical - 71 - Write a program in python to print all the numbers between 1 to 100 which are either divisible by 3 or 5.


Ans:

for i in range(1,101):

    if i%3==0 or i%5==0:

        print(i,end=" ")

 

Output:

3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51 54 55 57 60 63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99 100 


Practical - 70 - Write a program in python to generate the sequence: -1, 2, -3, 4, -5, …n, where n is an integer and its value should be taken as n input from the user.

Practical - 70 - Write a program in python to generate the sequence: -1, 2, -3, 4, -5, …n, where n is an integer and its value should be taken as n input from the user.

Ans:

n=int(input("Enter Number of terms in sequence = "))

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

    a=i*(-1)**i

    print(a,end=",")

 

Output:

Enter Number of terms in sequence = 8

-1,2,-3,4,-5,6,-7,8,


Practical - 69 - Write a program in python to input a number and calculate factorial of a number.

Practical - 69 - Write a program in python to input a number and calculate factorial of a number.


Ans:

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

fact=1

if(n<0):

    print("Factorial can not be find of a negative number")

elif(n==0):

    print("Factorial of 0 is 1")

else:

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

        fact=fact*i

    print("Factorial of",n,"is",fact)

 

Output:

Enter a number: -2

Factorial can not be find of a negative number


OR


Enter a number: 0

Factorial of 0 is 1


OR


Enter a number: 6

Factorial of 6 is 720

Practical - 68 - Write a program in python to print the tables all all numbers from 2 to 10

Practical - 68 - Write a program in python to print the tables all all numbers from 2 to 10


Ans:

print("Table form 2 to 10: ")

for n in range(2,11):

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

        print(i,end=" ")

    print()

 

Output:

Table form 2 to 10: 

2 4 6 8 10 12 14 16 18 20 

3 6 9 12 15 18 21 24 27 30 

4 8 12 16 20 24 28 32 36 40 

5 10 15 20 25 30 35 40 45 50 

6 12 18 24 30 36 42 48 54 60 

7 14 21 28 35 42 49 56 63 70 

8 16 24 32 40 48 56 64 72 80 

9 18 27 36 45 54 63 72 81 90 

10 20 30 40 50 60 70 80 90 100


Practical - 67 - Write a program in Python to input a number and print the table of that number.

 Practical - 67 - Write a program in Python to input a number and print the table of that number.


Ans:

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

for i in range(1,11):

    print(n,"*",i,"=",n*i)

 

Output:

Enter a number: 5

5 * 1 = 5

5 * 2 = 10

5 * 3 = 15

5 * 4 = 20

5 * 5 = 25

5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

5 * 10 = 50


Practical - 66 - Write a program in python to input number of seconds and print it in the form: hrs:mins:sec. e.g. 4815 seconds are printed as 1 hrs:20 mins:15 secs

Practical - 66 - Write a program in python to input number of seconds and print it in the form: hrs:mins:sec. e.g. 4815 seconds are printed as 1 hrs:20 mins:15 secs


Ans:

n=int(input("Enter number of seconds = "))

hrs=n//3600

n=n-hrs*3600

mins=n//60

sec=n-mins*60

print(hrs,"hrs:",mins,"mins:",sec,"sec")

 

Output:

Enter number of seconds = 4815

1 hrs: 20 mins: 15 sec


Practical - 65 - Write a Python program to take sides of a triangle as input and print its area using Heron's Formula.

Practical - 65 - Write a Python program to take sides of a triangle as input and print its area using Heron's Formula.


Ans:

a=int(input("Enter First side of triange: "))

b=int(input("Enter Second side of triange: "))

c=int(input("Enter Third side of triange: "))

s=(a+b+c)/2

area=(s*(s-a)*(s-b)*(s-c))**0.5

print("Area of triangle=",area)

 

Output:

Enter First side of triange: 3

Enter Second side of triange: 4

Enter Third side of triange: 5

Area of triangle= 6.0