[go: up one dir, main page]

0% found this document useful (0 votes)
2 views22 pages

Pseudocodes On Number DataTypes

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

numbers.

py 05/04/2023 09:49 PM

1.Perform all Arithmetic Operations

a=10
b=20
print('addition = ', a+b)
print(f'subtraction = ' ,a-b)

2.Print integer in Positive Format

num=20
if(num<0):
num=num*-1
print(num)

3.Check 2 integers are equal or not

num1=20
num2=30
if(num1==num2):
print('numbers are equal')
else:
print('numbers are not equal')

4.check user entered integer is Digit(1 number) or Number(2numbers)

n=20
if(n>=-9 & n<=9):
print(n,'is a digit')
else:
print(n,'is two digit')

5.take input from user and check its 2 digit or single digit number

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


if(n>=10 and n<=99 or n<=-10 and n>=-99):
print(f'{n} is two digit')
else:

Page 1 of 22
numbers.py 05/04/2023 09:49 PM

print(f'{n} is not two digit')

6.Check user entered number is multiple of 3 &5 or not

n=int(input('enter a number : '))


if(n%3==0 and n%5==0):
print('number is divisible by 3 and 5')
else:
print('number is not divisible by both 3 and 5 ')

7.Read month number from user & print it is valid or not

m=int(input('enter a month number : '))


if(m>=1 and m<=12):
print('is a valid month number')
else:
print('is not a valid month number ')

8.Read month number from user & print Corresponding How many Days in that month

month = int(input('enter a month number : '))

if(month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):


print("Number of days is 31")
elif(month==4 or month==6 or month==9 or month==11):
print("Number of days is 30")
elif(month == 2):
print(" of days is 28")
else:
print("Number of days is 30")

9.print results like Distinction ,1st class,2nd class,pass or Fail

print("Enter Subjects marks: ")


p = int(input('p : '))
c = int(input('c : '))

Page 2 of 22
numbers.py 05/04/2023 09:49 PM

m = int(input('m : '))
b = int(input('b : '))
per = ((p + c + m + b) / 4)

if(p<35 or c<35 or m<35 or b<35):


print(" Student is Fail")
else:
if(per >= 85):
print("Distinction")
elif(per >= 60):
print("First Class")
elif(per >= 50):
print("Second Class")
else:
print("Pass")

10.Check number is even or not


a=24
if(a%2==0):
print('even')
else:
print('odd')

11.Basics of loop print from 1 to n

n=10
i=1
while(i<=n):
print(i)
i=i+1

for i in range(5):
print(i,end=" ")

12.print from n to 1
n=10
for i in range(n,0,-1):

Page 3 of 22
numbers.py 05/04/2023 09:49 PM

print (i , end=" ")

i =10
while (i >= 1):
print(i, end=' ')
i = i - 1

13.multiplication table
num=12
for i in range(1, 11):
print(num, 'x', i, '=', num*i)

14.print even numbers

for i in range(1,20):
if i%2==0:
print(i,end=" ")

15.sum of even
result = 0
for x in range(1, 10):
if (x % 2 == 0):
result = result + x

print("Sum of even number is ", result)

16.count number of digits in a nmber


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)

Page 4 of 22
numbers.py 05/04/2023 09:49 PM

17.add 2 numbers using a function


def addNumbers(x,y):
sum = x + y
return sum

output = addNumbers(12,9)
print(output)

18.swap using funtion


def SwapTwoNumbers(a,b):
print("Before Swap: ",a,b)
a = a+b
b = a-b
a = a-b
return a,b

a,b = SwapTwoNumbers(17,24)
print("After Swap: ",a,b)

//or int temp = a;


a = b;
b = temp;

19.Read amount from user & print it in the Indian Currency Dimenssion
amt=int(input("Enter the numerical amount : "))
print("2000rs --> ", int(amt/2000))
amt=amt%2000
print("500rs --> ", int(amt/500) )
amt=amt%500
print("200rs --> ", int(amt/200) )
amt=amt%200
print("100rs --> ", int(amt/100) )
amt=amt%100
print("50rs --> ", int(amt/50) )
amt=amt%50
print("20rs --> ", int(amt/20) )
amt=amt%20
print("10rs --> ", int(amt/10) )

Page 5 of 22
numbers.py 05/04/2023 09:49 PM

amt=amt%10
print("5rs --> ", int(amt/5) )
amt=amt%5
print("2rs --> ", int(amt/2) )
amt=amt%2
print("1rs --> ", int(amt/1) )
amt=amt%1

20.factorial
def factorial(n):
fact = 1
while(n!=0):
fact = fact*n
n = n-1
print("The factorial is",fact)

inputNumber = int(input("Enter the number: "))


factorial(inputNumber)

21.Function to get sum of digits


def getSum(n):

sum = 0
while (n != 0):
sum = sum + (n % 10)
n = n//10
return sum

n = 12345
print(getSum(n))

22.check number is pallindrome or not


n=int(input("Enter number:"))
temp=n
rev=0
while(n!=0):
dig=n%10
rev=rev*10+dig

Page 6 of 22
numbers.py 05/04/2023 09:49 PM

n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

23.count even and odd numbers present in the list

list1 = [10, 21, 4, 45, 66, 93, 1]

even_count, odd_count = 0, 0

for num in list1:


if num % 2 == 0:
even_count += 1
else:
odd_count += 1

print("Even numbers in the list: ", even_count)


print("Odd numbers in the list: ", odd_count)

24.Python Program to check whether the given input is alphabet, number or special character

ch = input("Please Enter Any Character : ")

if((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')):
print("The Given Character ", ch, "is an Alphabet")
elif(ch >= '0' and ch <= '9'):
print("The Given Character ", ch, "is a Digit")
else:
print("The Given Character ", ch, "is a Special Character")

25.check str is vowel or consonant


l = input("Enter the character: ")
if l.lower() in ('a', 'e', 'i', 'o', 'u'):
print("Vowel")
elif l.upper() in ('A', 'E', 'I', 'O', 'U'):

Page 7 of 22
numbers.py 05/04/2023 09:49 PM

print("Vowel")
else:
print("Consonant")

26.count howmany capital ,lower,vowels,consonant,numbers in a string


name=str(input("enter the string"))
count=0
for i in name:
if i.isupper():
count=count+1
print("The number of capital letters found in the string is:-",count)

27.Find out How many 1 and 0 in a Given Number

gvn_num = 111000101
ones_cnt = 0
zeros_cnt = 0

while gvn_num:
remindr = gvn_num % 10 # Calculate the value of the given number modulus 10 (which gives the last digit of the given Number).
gvn_num = int(gvn_num/10) # Calculate the value of the given number divided by 10 (which removes the last digit of the given n

if remindr == 1:
ones_cnt = ones_cnt+1

if remindr == 0:
zeros_cnt = zeros_cnt+1

print("The Count of 0's in a given number = ", zeros_cnt)


print("The Count of 1's in a given number = ", ones_cnt)

output:
The Count of 0's in a given number = 4
The Count of 1's in a given number = 5

28.Find the Sum of Natural Numbers

Page 8 of 22
numbers.py 05/04/2023 09:49 PM

* n=5
sum=0
for i in range(1,n+1):
sum=sum+i
print(sum)

* while(n!= 0):
sum = sum+n
n = n-1
print("Sum of natural numbers", "=", sum)

* sum = (number * (number+1))//2 #Using mathematical formula

29.print multiplication tables upto n


n=5
for j in range(1,n+1):
for i in range(1,11):
print(j , 'x', i ,'=', (j*i))
print()

30.print multiplication table of a given number


n=5
for i in range(1, 11):
print(n, 'x', i, '=', (n * i))

number = 8
m = 1
while(m <= 10):
print(number, "*", m, "=", number*m)
m = m+1

31.Check Prime Number


A prime number is a positive integer greater than 1 that has no other variables except 1 and the number itself.
Since they have no other variables, the numbers 2, 3, 5, 7, and so on are prime numbers.

Page 9 of 22
numbers.py 05/04/2023 09:49 PM

number = 5
temp = False
# We will check if the number is greater than 1 or not
# since prime numbers starts from 2
if number > 1:

for i in range(2, number//2): # checking the divisors of the number


if (number % i) == 0:
temp = True # if any divisor is found then set temp to true since it is not prime number
break # Break the loop if it is not prime
if(temp):
print("The given number", number, "is not prime number")
else:
print("The given number", number, "is prime number")

32.Find the Factors of a Number and count number of factors


number = 10
count=0
print("printing the factors of given number : ")
for i in range(1,number+1):
if(number%i==0):
print(i) #1 2 5 10
count=count+1
print("Total Number of divisors are = ",count) #4

33.Finding Factorial of a Number


num = 7
res = 1

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


res = res*i
print(i,'! =',res)

print("Factorial of", num, "=", res) # Factorial of 7 = 5040

output:
1 ! = 1

Page 10 of 22
numbers.py 05/04/2023 09:49 PM

2 ! = 2
3 ! = 6
4 ! = 24
5 ! = 120
6 ! = 720
7 ! = 5040

34.Count the Number of Digits Present In a Number

* given_number = 27482
digits_count = 0
while (given_number > 0):
given_number = given_number//10
digits_count = digits_count + 1
print("The total digits present in the given number=", digits_count)

* given_number = 27482
print("The total digits present in the given number=", len(str(given_number)))

35.Reverse the given Number


given_num = 12345
reverse_number = 0
while (given_num > 0):
remainder = given_num % 10 # getting the last digit
reverse_number = (reverse_number * 10) + remainder
given_num = given_num // 10

print("The reversed number =", reverse_number)

36.Find the Greatest Digit in a Number.

gvn_num = 154
str_numbr = str(gvn_num)
lst = list(str_numbr)
maxim_digit = max(lst)
print("The maximum digit in given number {", gvn_num, "} = ", maxim_digit)

Page 11 of 22
numbers.py 05/04/2023 09:49 PM

37.Print all Numbers in a Range Divisible by a Given Number


lower_limit = 20
upper_limit = 60
numb = 4
print("The numbers which are divisible by", numb, "from", lower_limit, "to", upper_limit, "are:")
for val in range(lower_limit, upper_limit+numb):
if(val % numb == 0):
print(val, end=" ")

output: The numbers which are divisible by 4 from 20 to 60 are: 20 24 28 32 36 40 44 48 52 56 60

38.Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given Range of Numbers

lowerlimit = 10
upperlimit = 200
print("The number which is divisible by 7 and multiple of 5 :")
for numb in range(lowerlimit, upperlimit+1):
if(numb % 7 == 0 and numb % 5 == 0):
print('Number =',numb)

output:
The number which is divisible by 7 and multiple of 5 :
Number = 35
Number = 70
Number = 105
Number = 140
Number = 175

39.Print Odd Numbers Within a Given Range


lowLimitRange = 1
uppLimitRange = 30
print('Odd numbers from', lowLimitRange, 'to', uppLimitRange, 'are :')
for iterval in range(lowLimitRange, uppLimitRange+1):
if(iterval % 2 != 0):
print(iterval, end=" ")

Page 12 of 22
numbers.py 05/04/2023 09:49 PM

40.Accept Three Digits and Print all Possible Combinations from the Digits

firstDigit = 1
secondDigit = 9
thirdDigit = 2

digitsList = []

digitsList.append(firstDigit)
digitsList.append(secondDigit)
digitsList.append(thirdDigit)
# Using nested loops
for i in range(3):
for j in range(3):
for k in range(3):
if(i != j & j != k & k != i):
print(digitsList[i], digitsList[j], digitsList[k])
Output:
1 9 2
1 2 9
9 1 2
9 2 1
2 1 9
2 9 1

41.Find Even Digits Sum and Odd Digits Sum Divisible by 4 and 3 Respectively

numb = 123452
stringnum = str(numb)

digtslst = list(map(int, stringnum)) # Create a list of digits say "digtslst" using map(),list(),int functions.
evn_sum = 0
od_sum = 0

for itr in range(len(digtslst)):


if(digtslst[itr] % 2 == 0):
evn_sum += digtslst[itr]

Page 13 of 22
numbers.py 05/04/2023 09:49 PM

else:
od_sum += digtslst[itr]

if(evn_sum % 4 == 0 and od_sum % 3 == 0):


print("yes, the even digits sum and odd digits sum of a given number{", numb, "} is divisible by 4 and 3 respectively.")
else:
print("No, the even digits sum and odd digits sum of a given number{", numb, "} is not divisible by 4 and 3 respectively.")

Output: yes, the even digits sum and odd digits sum of a given number{ 123452 } is divisible by 4 and 3 respectively.

42.Count the Number of Odd and Even Digits of a Number at Even and Odd places

numb = 1787725620
stringnum = str(numb)

digtslst = list(map(int, stringnum))

evn_sum = 0
od_sum = 0

for itr in range(len(digtslst)):


if(itr % 2 == 0):
evn_sum += digtslst[itr]
else:
od_sum += digtslst[itr]

print("The sum of all digits at even places in a given number{", numb, "} =", evn_sum)
print("The sum of all digits at odd places in a given number {", numb, "} =", od_sum)

Output:
The sum of all digits at even places in a given number{ 1787725620 } = 23
The sum of all digits at odd places in a given number { 1787725620 } = 22

43.Count the Number of Odd and Even Digits

numb = 1237891
stringnum = str(numb)

digtslst = list(map(int, stringnum))

Page 14 of 22
numbers.py 05/04/2023 09:49 PM

evn_count = 0
od_count = 0

for itr in range(len(digtslst)):


if(digtslst[itr] % 2 == 0):
evn_count += 1
else:
od_count += 1

print("The count of even digits in a given number{", numb, "} =", evn_count)
print("The count of odd digits in a given number{", numb, "} =", od_count)

Output:
The count of even digits in a given number{ 1237891 } = 2
The count of odd digits in a given number{ 1237891 } = 5

44.Print Palindrome Numbers in a Range in Python

gvn_lowrlmt = 50
gvn_upprlmt = 130
print("The palindrome numbers between", gvn_lowrlmt, "and", gvn_upprlmt, "are:")

for m in range(gvn_lowrlmt, gvn_upprlmt+1):


given_num = m
duplicate_num = given_num

reverse_number = 0

while (given_num > 0):


remainder = given_num % 10
reverse_number = (reverse_number * 10) + remainder
given_num = given_num // 10

if(duplicate_num == reverse_number):
print(duplicate_num, end=" ")

Output: The palindrome numbers between 50 and 130 are: 55 66 77 88 99 101 111 121

Page 15 of 22
numbers.py 05/04/2023 09:49 PM

45.Program to Find the Sum of Series 9+99+999…..+N

gvn_numb = 6
resltsum = 0

k = 9
for itr in range(1, gvn_numb+1):
resltsum += k
k = (k*10)+9

print("The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output: The total sum of the series till the given number { 6 } = 1111104

46.Print Series 1, 2, 4, 8, 16, 32…n

gvn_numb = int(input("Enter some Random Number = "))


itr = 1

print("The above series till the given number{", gvn_numb, "} is :")
while itr <= gvn_numb:
print(itr, end=" ")
itr *= 2

Output:
Enter some Random Number = 50
The above series till the given number{ 50 } is :1 2 4 8 16 32

47.Print Series 1, 22, 333, 4444…n

gvn_numb = 5
print("The above series till the given number{", gvn_numb, "} is :")

for itr in range(gvn_numb+1):


for m in range(itr):
print(itr, end="")

Page 16 of 22
numbers.py 05/04/2023 09:49 PM

print(end=" ")

Output: The above series till the given number{ 5 } is : 1 22 333 4444 55555

48.Disarium Number

num = 135
ans = 0

digits = len(str(num))
dup_number = num
while (dup_number != 0):
remainder = dup_number % 10
ans = ans + remainder**digits
digits = digits - 1
dup_number = dup_number//10

if(num == ans):
print(num, "is disarium number")
else:
print(num, "is not disarium number")

Output: 135 is disarium number

49.Armstrong Number in Python

num = 153
ans = 0
digits = len(str(num))
dup_number = num
while (dup_number != 0):
remainder = dup_number % 10
ans = ans + remainder**digits
dup_number = dup_number//10

if(num == ans):
print(num, "is Armstrong number")
else:

Page 17 of 22
numbers.py 05/04/2023 09:49 PM

print(num, "is not Armstrong number")

output: 153 is Armstrong number


Explanation: Here 1^3 + 5^3 + 3^3 = 153 so it is Armstrong Number

50.Check Whether the given Number is Perfect Number or Not


appropriate divisors of 6 are 1, 2, 3.
Because the sum of these divisors equals 6 (1+2+3=6), 6 is considered a perfect number.
When we consider another number, such as 12, the proper divisors of 12 are 1, 2, 3, 4, and 6.
Now, because the sum of these divisors does not equal 12, 12 is not a perfect number.

def checkPerfectNumb(givenNumb):
totalSum = 1

for i in range(2, givenNumb//2 + 1):


if givenNumb % i == 0:
totalSum += i

if(totalSum == givenNumb):
return True

return False

given_numb = 6
if(checkPerfectNumb(given_numb)):
print("The given number", given_numb, "is perfect number")
else:
print("The given number", given_numb, "is not a perfect number")

Generate Perfect Numbers in an Interval

def checkPerfectNumbr(givenNumb):
totalSum = 1

for i in range(2, givenNumb):


if givenNumb % i == 0:

Page 18 of 22
numbers.py 05/04/2023 09:49 PM

totalSum += i

if(totalSum == givenNumb):
return True

return False

lowlimrange = 1
upplimrange = 1000
print('The Perfect numbers in the given range', lowlimrange, 'and', upplimrange, 'are:')

for itrvalue in range(lowlimrange, upplimrange+1):


if(checkPerfectNumbr(itrvalue)):
print(itrvalue, end=' ')

Output: The Perfect numbers in the given range 1 and 1000 are: 1 6 28 496

51.Check Whether the given Number is Strong Number or Not

def checkStrongNumb(givenNumb):
totalSum = 0
tempNum = givenNumb

while(givenNumb): # using while to extract digit by digit of the given number


s = 1
factNum = 1
remainder = givenNumb % 10

while(s <= remainder): # calculating the factorial of the digit(extracted by remainder variable)
factNum = factNum * s
s = s + 1

totalSum = totalSum + factNum


givenNumb = givenNumb//10

if(totalSum == tempNum):
return True

Page 19 of 22
numbers.py 05/04/2023 09:49 PM

return False

given_numb = 145
if(checkStrongNumb(given_numb)):
print("The given number", given_numb, "is strong number")
else:
print("The given number", given_numb, "is not a strong number")

Ex: 145 the sum of factorial of digits = 1 ! + 4 ! +5 ! = 1 + 24 +125

Check If A Number Is A Happy Number


Given Number = 320
Square of the digits = 32 + 22 + 02 = 13
Square of the digits = 12 + 32 = 10
Square of the digits = 12 + 02 = 1

def digitSquareSum(resltnumber):

strnumbe = str(resltnumber)
numbrlistdigits = list(map(int, strnumbe))

sumsquaredigits = 0
for digitvalu in numbrlistdigits:
sumsquaredigits = sumsquaredigits+(digitvalu**2)

return sumsquaredigits

numbr = 100
while(reslt != 1 and reslt != 4):
reslt = digitSquareSum(reslt)

if(reslt == 1):
print('The given number [', numbr, '] is a happy number')
else:
print('The given number [', numbr, '] is not a happy number')

Page 20 of 22
numbers.py 05/04/2023 09:49 PM

Print all Happy numbers within a given range

def digitSquareSum(resltnumber):
strnumbe = str(resltnumber)
numbrlistdigits = list(map(int, strnumbe))
sumsquaredigits = 0

for digitvalu in numbrlistdigits:


sumsquaredigits = sumsquaredigits+(digitvalu**2)

return sumsquaredigits

def checkhapppynumb(numb):
rest = numb

while(rest != 1 and rest != 4):


rest = digitSquareSum(rest)

if(rest == 1):
return True
else:
return False

lowlimrange = 19
upplimrange = 145
print('The happy numbers in the given range', lowlimrange, 'and', upplimrange, 'are:')

for l in range(lowlimrange, upplimrange+1):


if(checkhapppynumb(l)):
print(l, end=' ')

Output:
The happy numbers in the given range 19 and 145 are:
19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 103 109 129 130 133 139

Print all the Prime Numbers within a Given Range

Page 21 of 22
numbers.py 05/04/2023 09:49 PM

givenNumbr = 95
print('The prime numbers from 2 to upper limit [', givenNumbr, '] :')

for valu in range(2, givenNumbr+1):


cntvar = 0
for divi in range(2, valu//2+1):
if(valu % divi == 0):
cntvar = cntvar+1

if(cntvar <= 0):


print(valu, end=' ')

Output:
The prime numbers from 2 to upper limit [ 95 ] :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89

Page 22 of 22

You might also like