Document Imp
Document Imp
In this formula, the term b2 - 4ac is called the discriminant. If b2 - 4ac = 0, then the equation
has two equal roots. If b2 - 4ac > 0, the equation has two real roots. If b2 - 4ac < 0, the
equation has two complex roots. Write a program that prompts the user to input the value
of a (the coefficient of x2 ), b (the coefficient of x), and c (the constant term) and outputs the
roots of the quadratic equation.
OUTPUT:
2) Write a program that prompts the user to input a year and determine whether the year is
a leap year or not. Leap Years are any year that can be evenly divided by 4. A year that is
evenly divisible by 100 is a leap year only if it is also evenly divisible by 400. Example : 1992
Leap Year 2000 Leap Year 1900 NOT a Leap Year.
OUTPUT:
3) Write a program that prompts the user to input a character and determine the character
is vowel or consonant.
OUTPUT:
4) Write a program that prompts the user to input a number and prints its factorial. The
factorial of an integer n is defined as n! = 1 x 2 x 3 x ... x n; if n > 0 = 1; if n = 0 For instance, 6!
can be calculated as 1 x 2 x 3 x 4 x 5 x 6.
Ans: num = int(input("Enter a positive number: "))
fact=1
i=num
while i>1:
fact=fact*i
i=i-1
print("Factorial of",num,"is",fact)
OUTPUT:
5) Write a program that prompts the user to input a number and reverse its digits. For
example, the reverse of 12345 is 54321; reverse of 56 is 65.
Ans: num = int(input("Enter a positive integer: "))
temp = num
rev=0
while temp>0:
digit = temp%10
rev= digit + rev*10
temp= int(temp/10)
print("Reverse of",num,"is",rev)
OUTPUT:
6) Write a program that asks the user to input a positive integer. Your program should find
and display the sum of digits of number. For example, sum of digits of number 32518 is
3+2+5+1+8 = 19.
Ans: num = int(input("Enter a positive integer: "))
temp = num
sum=0
while temp>0:
digit = temp%10
sum=sum+digit
temp= int(temp/10)
print("Sum of digits of",num,"is",sum)
OUTPUT:
7) A palindromic number is a number that remains the same when its digits are reversed. For
example, 16461. Write a program that prompts the user to input a number and determine
whether the number is palindrome or not.
Ans: num = int(input("Enter a positive integer: "))
temp = num
rev=0
while temp>0:
digit = temp%10
rev= digit + rev*10
temp= int(temp/10)
if num==rev:
print(num,"is a palindrome")
else:
print(num,"is not a palindrome")
OUTPUT:
8) Write a program that prompts the user to input a positive integer. It should then output a
message indicating whether the number is a prime number
Ans: num=int(input("enter a number:"))
lim=int(num/2)+1
for i in range(2,lim):
rem=num%i
if rem==0:
print(num,"is not prime")
break
else:
print(num,"is a prime number")
OUTPUT:
9) Write a program that prompts the user to input two numbers and display its HCF.
Ans : x=int(input("enter a number:"))
y=int(input("enter a number:"))
if x>y:
small=y
else:
small=x
for i in range(1,small+1):
if ((x%i==0) and (y%i==0)):
hcf=i
print("The HCF of", x,"and",y,"is",hcf)
OUTUT:
10) An Armstrong number of three digits is an integer such that the sum of the cubes of its
digits is equal to the number itself. For example, 371 is an Armstrong number since 33 + 73 +
13 = 371. Write a program to find all Armstrong number in the range of 0 and 999
Ans:
number=sum=c=0
n=int(input("enter a limit"))
for i in range(0,n+1):
number=i
sum=0
while number>0:
digit = number%10
sum=sum+pow(digit,3)
number= int(number/10)
if sum == i:
c+=1
print(i)
print("there are " ,c," armstrong numbers between o and ",n)
OUTPUT:
11) Write a program to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence
the sum of two successive terms gives the third term. Following are the first few terms of the
Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34 55 89...
Ans:
n1, n2 = 0, 1
count = 0
if nterms <= 0:
elif nterms == 1:
print(n1)
else:
print("Fibonacci sequence:")
print(n1,end=” “)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
OUTPUT:
12) Floyd's triangle is a right-angled triangular array of natural numbers as shown below.
Write a program to print the Floyd’s triangle.
Ans:
k=1
n=int(input("enter the number of rows"))
for i in range(1,n+1):
for j in range(1,i+1):
print(k,end=' ')
k=k+1
print()
OUTPUT:
13) Write a program that reads a string from keyboard and display:
* The number of uppercase letters in the string
* The number of lowercase letters in the string
* The number of digits in the string
* The number of whitespace characters in the string.
Ans:
text = input('Enter a string: ')
lower = upper = digit = space = 0
for ch in text:
if ch.isupper():
upper += 1
elif ch.islower():
lower += 1
elif ch.isdigit():
digit += 1
elif ch == ' ':
space += 1
Output:
14) A palindrome is a string that reads the same backward as forward. For example, the
words dad, madam and radar are all palindromes. Write a program that determines whether
the string is a palindrome. Note: Do not use reverse() method
Ans:
string=input("enter a string: ")
flag=True
for i in range(0, len(string)//2):
if(string[i] != string[len(string)-i-1]):
flag = False
break
if(flag):
print("Given string is palindrome");
else:
print("Given string is not a palindrome");
OUTPUT:
15) Write a python program to enter a string and a substring. It should then display the
number of occurrences of the given substring in the line.
Example: Enter the string: Your passion will be shown by the passion of desire Enter the
substring: passion The frequency of substring in string is 2
Ans:
string=input("enter a sentence or word ")
sub=input("enter a word or alphabet ")
count=string.count(sub)
print(sub," is present " , count ," times ")
OUTPUT: