Skip to main content

Posts

Showing posts from July, 2021

Python program to print Even numbers in between x and y

 Python program to print Even numbers in between x and y. Even numbers: The numbers which are  divisible by 2 or the numbers whose remainder is  0 are called even numbers. Input : enter x value 1 enter y value 7 Output :  even numbers between 1 and 7 are 2 4 6 Python program to display even numbers in range x , y #program to print even numbers in range x and y x=int(input("enter x ")) y=int(input("enter y ")) print(f"even numbers in between{x} and {y} are ") for i in range(x,y): if i%2==0: print(i) #use i,end=' ' to get output in single line Output : enter x 1 enter y 7 even numbers in between1 and 5 are 2 4 6

Python program to display odd numbers in range x , y

 Python program to print odd numbers in between x and y. Odd numbers: The numbers which are not divisible by 2 or the numbers whose remainder is  1 are called odd numbers. Input : enter x value  5 enter y value 10 Output :  odd numbers between 5 and 10 are 5 7 9  Python program to display odd numbers in range x , y #program to print odd numbers in range x and y x=int(input("enter x ")) y=int(input("enter y ")) print(f"odd numbers in between{x} and {y} are ") for i in range(x,y): if i%2!=0: print(i) #use i,end=' ' to get output in single line Output : enter x 5 enter y 10 odd numbers in between1 and 5 are 5 7 9

Python Program to find sum of n natural numbers

 Sum of n natural numbers Natural number : All positive integers are natural numbers. Input : enter n value 20 Output :  sum of 20 natural numbers is 210  Python Program to find sum of n natural numbers #program to find n natural numbers n=int(input("enter ")) nsum=0 if n >1: for i in range(1,n+1): nsum+=i print(f"sum of {n} natural numbers is {nsum}") else: print("enter a natural number") Output : enter 20 sum of 20 natural numbers is 210 Output : enter -3 enter a natural number

Program to find leap Year or not in Python

 What is a Leap year ? Generally  we have 28 days in the month of February and overall 365 days in a year. But a leap year have 29 days in the month of February and overall 366 days in a year. For every four years we get a leap year. Conditions to be a leap Year. Year divisible by 4 is a leap year until it is not divisible by 100 (century year{year ends with 00} is not a leap year until it is divisible by 400, if it is divisible by 400 then it is a leap year. Explanation :  For a non century year  if year divisible by 4 then it is a leap year. For a non century year  if year not divisible by 4 then it is not a leap year. If year is a century year then it is not a leap year. Century year is said to be a leap year if it is divisible by 400 . Input : enter a year 2400 Output :  2400 is a leap year  Program to find leap Year or not in Python #program to say leap year or not year=int(input("enter a year ")) if year%4==0: if year%100==0: if year%400==0: pri

Printing Multiplication table in Python

Display Multiplication table in Python Read an integer using input() function.   print(f"Multiplication table of {number} is") Use for loop to iterate multiplication for 10 times. print(number,"*",i,"=",number*i). As a result we get the output as a multiplication table of number. Input: enter any integer : 5 Output :  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 Program to display multiplication table in python.   #Multiplication table in python number=int(input("enter any integer ")) print(f"Multiplication table of {number} is") for i in range(1,11): print(f'{number}*{i}={number*i}') #---------without format specifiers---------# for i in range(1,11): print(number,"*",i,"=",number*i) Output : enter any integer 5 Multiplication table of 5 is 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

Printing first n even and odd numbers in Python using for loop

for Loop in Python for  loop  -- Executes a sequence of statements multiple times and abbreviates the code that manages the    loop  variable. even and odd numbers An  even number  is a  number  that can be divisible by 2.   Even numbers  are  2, 4, 6, 8 and so on.  An  odd number  is a  number  that cannot be divisible by 2. Odd numbers  are 1, 3, 5, 7, 9 so on. Program to print first 10 even and odd numbers using for  loop. Input : how many even and odd nums you want 5 Output : first 5 even nums are  2 4 6 8 10  first 5 odd nums are  1 3 5 7 9 # Program to print first n even and odd numbers using for loop. choice = int(input("how many even and odd nums you want ")) n=(choice*2)+1 print("first",choice,"even nums are ") for i in range(1,n): if i%2 == 0: print(i,end=" ") print("\nfirst",choice,"odd nums are ") for i in range(1,n): if i%2!=0: print(i,end=" ") Output : first 5 even nums are 2

volume of sphere and hemisphere in Python | program

 volume of sphere and hemisphere. Volume  of  hemisphere  = 2πr 3 /3, where 'r' is the radius of the  hemisphere . Volume  of  sphere  = 4πr 3 /3, where 'r' is the radius of the  sphere . Program to find volume of sphere and hemisphere Input : enter radius of sphere 4 enter radius of hemisphere 4 Output : volume of sphere is %.2f 268.08 volume of hemisphere is 134.04 # Program to find volume of sphere and hemisphere # for pi use pi value as 3.1423 or import math module from math import* #Program to find volume of sphere radius1 = float(input("enter radius of sphere ")) vol_sphere = (4*pi*radius1*radius1*radius1)/3 print("volume of sphere is %.2f",round(vol_sphere,2)) #Program to find volume of hemisphere radius2 = float(input("enter radius of hemisphere ")) vol_hemisphere = (2*pi*radius2*radius2*radius2)/3 print("volume of hemisphere is ",round(vol_hemisphere,2)) #here round(var,n) is predefined function used to get n digits afte

Program to Convert Centimeter to meter and kilometer in python

 Convert Centimeter to meter and kilometer. 1 meter  = 100 centimeters 1 kilometer = 100000 centimeters Read the length in centimeters. use formulae and get the output. check the below code . Program to Convert Centimeter to meter and kilometer Input : enter length in centimeters 100 Output : 100.0 centimeters = 1.0 meters. 100.0 centimeters = 1e-05 kilometers. # program to convert centimeter to meter and kilometer cm = float(input("enter length in centimeters ")) # centimeter to meter meter = 0.01*cm print(cm," centimeters = ",meter," meters.") # centimeter to kilometer km = 0.00001 print(cm," centimeters = ",km," kilometers.") Output : enter length in centimeters 100 100.0 centimeters = 1.0 meters. 100.0 centimeters = 1e-05 kilometers.

Ternary operator in python | voter eligibility using ternary operator in python.

 Ternary operator Ternary operator also known as conditional expression operator. Checks for the condition and then evaluates true or false. It has very simple syntax of single with (if else) conditional statements. Syntax of ternary operator : [true_statement] if [expression] else[false_statement] Voter eligibility using ternary operator Read age from the user. follow syntax  Program on voter eligibility using ternary operator in python. Input : enter your age 16 Output : You are not eligible. # program to print voter eligibility using ternary operator age=int(input("enter your age ")) print("You are eligible"if age>18 else "You are not eligible") Output : enter your age 16 You are not eligible.

Area and Perimeter of Square Program in Python

 Area and Perimeter of Square Area of square is side*side Perimeter of a square is 4*side or (side + side + side + side) Steps : Read one  side of a square. we know that in square all sides are equal. substitute the side value in above formulae to obtain area and perimeter. Input : enter a side 4 Output : Area is  16 Perimeter is  16  Program to find area and perimeter of a square in python. #program to print area and perimeter of a square. side=int(input("enter a side ")) #area of square side*side area = side*side print("Area is ",area) #perimeter of a square. perimeter = 4*side print("Perimeter is ",perimeter) Output : enter a side 4 Area is 16 Perimeter is 16

Area and Perimeter of Rectangle in python | program in python

 Area and Perimeter of a Rectangle. All  rectangles  are also parallelograms, but not all parallelograms are  rectangles . The  perimeter  P of a  rectangle  is given by the formula, P=2l+2w  or 2(l + w), where l is the length and w is the width of the  rectangle . The  area  A of a  rectangle  is given by the formula, A=l*w , where l is the length and w is the width. Input : enter length 12 enter breadth 10 Output: area is 120 perimeter is 44 Program to find area and perimeter of a rectangle. #program to print area and perimeter of rectangle. length = int(input("enter length ")) breadth = int(input("enter breadth ")) #area of rectangle = l*b area = length * breadth print("area is ",area) #perimeter of rectangle= 2(l+b) perimeter=2*(length+breadth) print("perimeter is ",perimeter) Output : enter length 12 enter breadth 10 area is 120 perimeter is 44

Sum of digits Python | 3 digit number |

Sum of the  digits. Input: enter a digit 123 Output : sum of the digits is 6. Program to print Sum of the  digits. #program to print sum of the digits digit =int(input("enter a digit ")) sum1=0 while (digit!=0): remainder = digit % 10 sum1+=remainder digit//=10 print("sum of the digits is ",sum1) Output: enter a digit 123 sum of the digits is 6.

Alphabet or not in Python | program in Python

 Alphabet or Not program in Python We can use isalpha() function which is predefined in python.  Input : enter 56 Output : 56 is not alphabet Input : enter f Output : f is alphabet Program to print alphabet or not using Python #program to print alphabet or not ch =input("enter ") #ch.isalpha() is a predefined python fuction to check alphabet or not if ch.isalpha(): print(ch,"is Alphabet.") else: print(ch,"is not Alphabet.") Output: enter 56 56 is not Alphabet. enter f f is Alphabet.

Vowel or Consonant in python | python solution |

 Vowel and Consonant Program in python The alphabet is made up of 26 letters, 5 of which are  vowels  (a, e, i, o, u) and the rest of which are  consonants . A  vowel  is a sound that is made by allowing breath to flow out of the mouth, without closing any part of the mouth or throat. Input : enter an alphabet D Output : D is a consonant Program to print vowel or consonant in python. #program to print vowel or consonant char = input("enter any alphabet ") check = char.lower() #char.lower() function converts char to lower letter if (check=="a" or check=="e" or check=="i" or check=="o" or check=='u'): print(char," is vowel.") else: print(char," is consonant.") Output : enter any alphabet D D is consonant.

Largest and smallest of 3 numbers in python | solution | program

 Largest  among 3 numbers Read three integers a, b and c. use control structures (if , elif, else) to check the condition. if a is greater than b and a  is greater than c then print- a is greater value. if b is greater than a and b  is greater than c then print- b is greater value. if c is greater than b and c  is greater than a then print- c is greater value. Least among 3 numbers use control structures (if , elif, else) to check the condition. if a is smaller than b and a  is smaller than c then print- a is smaller value. if b is smaller than a and b  is smaller than c then print- b is smaller value. if c is smaller than b and c  is smaller than a then print- c is smaller value. Input : a =10 b=20 c=5 Output : b is greater value c is least value Program in python  #program to print largest and least value a=int(input("enter first num ")) b=int(input("enter second num ")) c=int(input("enter third num ")) # greatest or largest value if(a>b and a>c):

even or odd | solution in python | program

 Program to print even or odd. An  even number  is a  number  that can be divided into two equal groups. An  odd number  is a  number  that cannot be divided into two equal groups.  Even numbers  end in 2, 4, 6, 8 and 0 regardless of how many digits they have (we know the  number  5,917,624 is  even  because it ends in a 4!).  Odd numbers  end in 1, 3, 5, 7, 9. Input : 12 Output : even PROGRAM TO PRINT EVEN ODD #program to print even or odd. num = int(input("enter an integer ")) if num % 2 == 0: print("even") else: print("odd") Output enter an integer 23 odd

Prime number or not | python | solution

 Prime number A  prime number  is a  number  greater than 1 with only two factors – themselves and 1. A  prime number  cannot be divided by any other  numbers  without leaving a remainder. Input 11 Output prime Program to print prime or not #program to print even or odd. num = int(input("enter an integer ")) if num==1: print("neither prime nor composite") for i in range(2,num): if num%2==0: print("composite") break else: print("prime") break Output enter an integer 1 neither prime nor composite enter an integer 11  prime

Centigrade to Fahrenheit | program in python | solution

Program to convert  Centigrade to Fahrenheit How do you convert C to F easy? If you find yourself needing to quickly   convert Celsius to Fahrenheit , here is a simple trick you can use: multiply the temperature in degrees   Celsius   by 2, and then add 30 to get the (estimated) temperature in degrees   Fahrenheit . T(°F) = T(°C) × 9/5 + 32 Input :  Temperature in centigrade 32 Output : Fahrenheit is 89.6 PROGRAM IN PYTHON #program Here centigrade = int(input("Temperature in centigrade ")) #formula fahrenheit = (1.8*centigrade) + 32 fahrenheit = (1.8*centigrade) + 32 print("fahrenheit is ",fahrenheit) Output Temperature in centigrade 32 fahrenheit is 89.6

sum of marks of 6 subjects | average | percentage | sum | python solution

Program to calculate sum of the marks of six subjects and find average and percentage Explanation : Read  Marks obtained in six subjects using int(input()) Average=sum of the marks obtained / total number of marks Percentage = (marks obtained / total)*100 Sample Input 1: 50 60 65 80 55 85 Sample Output 1: average = 65.833333333 percentage = 65.8333333 sum = 395 PROGRAM IN PYTHON sanskrit = int(input("enter marks ")) python = int(input("enter marks ")) english = int(input("enter marks ")) maths = int(input("enter marks ")) chemistry = int(input("enter marks ")) physics = int(input("enter marks ")) total=600 #total marks obtained marks_obtained = (sanskrit+ python+ english+ maths+ chemistry+ physics) print("sum of marks is ",marks_obtained) #Average average = (sanskrit+python+english+maths+chemistry+physics)/6 print("average is ",average) # percentage percent = (marks_obtained / total)*100 print("per

Swaping two numbers | program | python solution

Program for swapping of two numbers Swapping refers to the exchange of two or more things. Example: a=5 , b=10 After swapping a becomes 10 and b becomes 5. Explanation : Read a and b value. Use any of the below methods and get output Method1: We have very simple method in python i.e., a , b  =  b , a Method2: Using third variable or temporary variable , a = a + b  b = a - b a = a- b Method3: Using Bitwise XOR (^),  a = a ^ b b = a ^ b a = a ^ b Input : a =  5 b = 10  Output : a = 10 b = 5 PROGRAM IN PYTHON #swapping of two numbers a = 5 b = 10 a , b = b , a #after swapping print("a is",a,"b is ",b) #method 2 a = a + b b = a - b a = a - b print("a is",a,"b is ",b) #method 2 --- using xor a = a ^ b b = a ^ b a = a ^ b print("a is",a,"b is ",b) Output a is 10 b is 5 a is 5 b is 10 a is 10 b is 5

Simple intrest | program | solution in python

Program to find the simple interest from given principle, rate of interest & time Simple Interest = P x T x R ÷ 100, where P = Principal, R = Rate of Interest and T = Time Period of the Loan/Deposit in years Explanation: Read Principle value.  Read  Rate of Interest. Read Time Period. Substitute the values in the below formula  simple interest =  P x T x R ÷ 100. SIMPLE INTEREST PROGRAM IN PYTHON principle = float(input("principle ")) rate = float(input("rate of interest ")) time = int(input("time ")) pi = 3.1423 #simple interest formula simple_interest = (principle*rate*time) / 100 print("simple interest is ",simple_interest) Output principle 500.0 rate of interest 5.0 time 2 simple interest is 50.0

Area and circumference of circle | program | python |

Program to find area and circumference of circle using radius Circumference of circle = 2πr Area of circle = πr^2 Program to find area and circumference of circle using radius radius = float(input("enter radius ")) pi = 3.1423 #circumference of circle circum = 2*pi*radius print("circumference of circle ",circum) #area of circle area = pi*radius*radius print("area of circle ",area) #USING INBUILT FUNCTIONS from math import pi area1 = pi*radius*radius circum1 = 2*pi*radius print("area is ",area1) print("circumference ",circum1) Output enter radius 4 circumference of circle 25.1384 area of circle 50.2768 50.26548245743669 25.132741228718345

Sum of Two Integers | solution in C | solution in python

Program to find sum of given two integer numbers. Explanation :   Read two integers a and b using int(input()) Assign C variable to a+b c=a+b print the value of c. Sample Input 1: 5 6 Sample Output 1: 11 PROGRAM IN C LANGUAGE PROGRAM IN C LANGUAGE #include int main(){ int num1,num2,sum_of_two; scanf("%d %d",&num1,&num2); sum_of_two=num1+num2; printf("sum of two nums is %d",sum_of_two); return 0; } Output : sum of two nums is 11 PROGRAM IN PYTHON LANGUAGE num1=int(input()) num2=int(input()) sum_of_two = num1+num2 print("sum of two nums is ",sum_of_two) Output : sum of two nums is 11