ASSIGNMENT NO. 3

PROGRAMMING


Coding in C and Python

In this assignment I learned to do programming in C language and python. Practised by making basic programs. I already used C and C++ in microcontrollers. Python was new to me. Python is more simpler in terms of syntax compared to C and C++. I coded programs which generated patterns using 'for loop'. This exercise helped me to develop strong logic which helped me to understand programs of Sorting numbers, characters based on some rule. Using 'Switch statement' I able to code for calculators.

1 : Addition of two numbers. Program asks for entering first number and second number then show the sum.

w3schools

PYTHON CODE (Copy as it is to run)

num1 = input('Enter first number: ') # enter first number, asks the user for it
num2 = input('Enter second number: ') # enter second number, asks the user for it
sum = float(num1) + float(num2) # Add two numbers
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) # Display the sum

2 : Code for identifying if the number entered is even or odd.

codeedit

PYTHON CODE (Copy as it is to run)

num = int(input("Enter a number: ")) # asks for the number to be checked
mod = num % 2 #dividing the number with two and storing the remainder in the mod
if mod > 0:
     print("This is an odd number.")
else:
     print("This is an even number.")

3 : Code for finding factorial. Ask for a number and calculates the factorial.

userpageiit

PYTHON CODE (Copy as it is to run)

num = int(input("Enter a number: ")) # Asks for a number to be entered
factorial = 1
if num < 0: # check if the number is negative, positive or zero
    print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
    print("The factorial of 0 is 1")
else:
    for i in range(1,num + 1):
       factorial = factorial*i
    print("The factorial of",num,"is",factorial)

END

Assignment no. 1 Assignment no. 2 Assignment no. 3 Assignment no. 4 Assignment no. 5 Assignment no. 6 Assignment no. 7