0% found this document useful (0 votes)
19 views10 pages

Samples - October.Students - Week.2.HW 2

Here is a Python program to find the sum of digits of a three digit number: PROGRAM SumOfDigits: READ num sum = 0 first_digit = num//100 sum = sum + first_digit num = num%100 second_digit = num//10 sum = sum + second_digit third_digit = num%10 sum = sum + third_digit PRINT sum END This program first reads a three digit number as input. It then separates the hundreds, tens and ones place digits using floor division and modulo operators. It calculates the sum of these individual digits and prints the final sum.

Uploaded by

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

Samples - October.Students - Week.2.HW 2

Here is a Python program to find the sum of digits of a three digit number: PROGRAM SumOfDigits: READ num sum = 0 first_digit = num//100 sum = sum + first_digit num = num%100 second_digit = num//10 sum = sum + second_digit third_digit = num%10 sum = sum + third_digit PRINT sum END This program first reads a three digit number as input. It then separates the hundreds, tens and ones place digits using floor division and modulo operators. It calculates the sum of these individual digits and prints the final sum.

Uploaded by

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

Samples

OPERATORS

Divide the first operand


/ Division a/b
by the second operand
Raise the first operand by
** Power a ** b the power of the second
operand
Divide the first operand
by the second operand and
% Modulo a%b
yield the remainder
portion

you use the double slash // operator to perform floor division.


This // operator divides the first number by the second number and rounds the
result down to the nearest integer (or whole number)
Whereas, the regular division of 12 by 5 would be equal to 2.4.
That is, 2 remainder 4:

PROGRAM RegularDevision:
num2 = 5
num3 = num1 / num2
PRINT normal division of num1 by num2 = num3
END.

# Output: normal division of 12 by 5 = 2.4


num2 = 5
num3 = num1 / num2
print("normal division of", num1, "by", num2, "=", num3)
# Output: normal division of 12 by 5 = 2.4
Examples of Floor Division
In the example below, the floor division of 12 by 5 resulted in 2:

PROGRAM FloorDivision:
num1 = 12 num2 = 5
num3 = num1 // num2
PRINT floor division of num1 by num2 = num3
END.
# Output: floor division of 12 by 5 = 2

num1 = 12 num2 = 5
num3 = num1 // num2
print("floor division of", num1, "by", num2, "=", num3)
# Output: floor division of 12 by 5 = 2
This shows you that the // operator rounds down the result of the division of two numbers to the nearest whole number.
Even if the decimal point is 9, the // operator would still round the result down to the nearest whole number.

PROGRAM RoundNumber:
num1 = 29
num2 = 10
num3 = num1 / num2
num4 = num1 // num2
PRINT normal division of num1 by num2 = num3)
PRINT but floor division of num1 by num2 = num4)
END.

""" Output: normal division of 29 by 10 = 2.9 but floor division of 29 by 10 = 2


"""
num1 = 29
num2 = 10
num3 = num1 / num2
num4 = num1 // num2
print("normal division of", num1, "by", num2, "=", num3)
print("but floor division of", num1, "by", num2, "=", num4)
""" Output: normal division of 29 by 10 = 2.9 but floor division of 29 by 10 = 2 """
code to Demonstrate the
Exponential Operactor
a =2
b =5

# using double asterisk operator


c = a**b
print(c)

# using double asterisk operator


z = 2 * (4 ** 2) + 3 * (4 ** 2 - 10)
print(z)
Modulus
•x=5
• y=2

• print(x % y)

• Output: 1
Q.1: four operands output

# Write a program which outputs 4


Arithmetic Operators. +,-,* and /
Q.1: four operands output
PROGRAM FourOperands: PROGRAM FourOperands:
a = 9; READ a;
b = 4; READ b;
add = a + b; add = a + b;
sub = a – b; sub = a – b;
mul = a * b; mul = a * b;
div = a / b; vs div = a / b;
PRINT add; PRINT add;
PRINT sub; PRINT sub;
PRINT mul; PRINT mul;
PRINT div; PRINT div; Examples of Arithmetic Operator
a =9

END. END.
b =4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
Q.2: Sum of digits -- HW
• Given a three digit number. Find sum of numbers.
• Sample number: 179
• Output: 17

You might also like