DSL810 Assignment3: "C" & "Python" Coding

PROGRAMMING BASICS


Brief: This is to learn the basics of programming using C language and Python and implement simple logics to understand the syntax and working.
C is based on Procedural programming while Python is based in Object Oriented programming. Syntax is easier on Python.


Learning:
●Header files include the standard function declaration and definition like printf, scanf etc.
●Different ways to read the input from user and display output to user.
●Using mathematical operations of set of numbers.
●Tackling syntax and logical errors.

Link for Online Python Interpreter

Program 1:

Write a program to take two integers "num1" and "num2" as inputs from the user and print their sum.

Program in "C"

#include
int main()
{
/* creating variables num1, num2, sum */
int num1, num2;
int sum;
printf("Please enter first number:\n");
/* read first number from user*/
scanf("%d", &num1);
printf("Please enter second number:\n");
/* read second number from user*/
scanf("%d", &num2);
/* add both numbers and print the result*/
printf("The sum of the two numbers is %d", sum = num1 + num2);
return 0;
}


Program in "Python"

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



Program 2:

Write a program to take an integer "n" as input and print if it is "odd" or "even".

Program in "C"

#include
int main()
{
int n;
printf("Please enter a number:\n");
/* read the number from user*/
scanf("%d", &n);
/* use modulus operator to to get the remainder when divided by 2.
The remainder would be zero if the number is even, remainder would be 1 if the number is odd*/
if(n%2==0)
{
printf("The entered number is even");
else
{
printf("The entered number is odd");
}
return 0;
}


Program in "Python"

num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
print("This is an odd number.")
else:
print("This is an even number.")



Program 3:

Write a program to take an integer "n" as input from the user and print its factorial.

Program in "C"

#include
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}
return 0;
}


Program in "Python"

Python program to find the factorial of a number using recursion
def recur_factorial(n):
"""Function to return the factorial of a number using recursion"""
if n == 1:
return n
else:
return n*recur_factorial(n-1)
# Change this value for a different result
num = 7
# uncomment to take input from the user
#num = int(input("Enter a number: "))
# check is the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))


THANK YOU! :)