You are on page 1of 30

DELHI TECHNOLOGICAL

UNIVERSITY

Programming Fundamentals 101

Name : Aditya Khitauliya


Roll Number : 2K22/B8/67
PROGRAM 1

Aim: Write a C program to print “Hello World!”.


Theory: Print function is used to display messages as well as value on
the standard output device (screen).

Algorithm: Just pass the string you want to print on the screen within
inverted commas (““).

Code:

Output: Hello World! is printed on the screen.

PROGRAM 2
Aim: Write a C program to take input two integers and print their sum.

Theory: Arithmetic operators are used for performing basic


mathematical operations on operands. “+” gives addition of two operants

Algorithm: Adding two numbers and storing the value of their addition
in variable “sum” and using print function to finally display its value.

Code:

Output: sum of a and b is printed on the screen.


PROGRAM 3
Aim: Write a C program to find Simple Interest

Theory: Simple Interest is calculated using three parameters Principal


amount, Rate of return and Duration of loan (in years). These three
parameters will be taken as input from the user and the Simple Interest
will be calculated using the standard formula.

Simple Interest = (P*R*T)/ 100

Where P is Principal amount, R is rate of return and T is Duration (in


years)

Algorithm: Declaring integer variable P and float variables SI, R, T.


Taking input from the user for variables R, T and P. Perform the
required operation in the formula and stores its value in SI. Using print
function print SI to display the calculated Simple Interest.

Code:
Output: Simple Interest is displayed on the screen for the value entered
by the user.
PROGRAM 4

Aim: Write a C program to take 5 integers as input and find the greatest
of them and print it.

Theory: The program takes the input one by one from the user and at
each turn compares the new input with the old input to see which is
greater. If the new input is found greater, the old input is discarded and
replaced with the new input and the program proceeds in a similar
fashion to accept another input. If the old input is found greater, it is
retained, and the new input is discarded away and the program proceeds
to accept the next input.

Algorithm: Declare integer variable ‘max’, ‘new’, and ‘i’. Input five
integer values from the user using for loop. After each entry by the user
check the greatest of available numbers. Then use the print function to
display the greatest of those 5 integer values.

Code:
Output:
PROGRAM 5

Aim: Write a C program to perform Logical AND and OR operations on


two expressions.

Theory:

Algorithm:

Code:
Output:
PROGRAM 6

Aim: Write a C program to perform bitwise AND and OR operations on


two variables.

Theory: The bitwise operators are the operators used to perform the
operations on the data at the bit-level. When we perform the bitwise
operations, then it is also known as bit-level programming. It consists of
two digits, either 0 or It is mainly used in numerical computations to
make the calculations faster.
Bitwise AND operator is denoted by the single ampersand sign (&).
Two integer operands are written on both sides of the (&) operator. If
the corresponding bits of both the operands are 1, then the output of the
bitwise AND operation is 1; otherwise, the output would be 0.
The bitwise OR operator is represented by a single vertical sign (|). Two
integer operands are written on both sides of the (|) symbol. If the bit
value of any of the operand is 1, then the output would be 1, otherwise 0.

Algorithm: We have two variables a and b.


a =6;
b=4;
The binary representation of the above two variables are
given below.
a = 0110
b = 0100
When we apply the bitwise AND operation in the above two
variables, i.e., a&b, the output would be:
Result = 0100
We consider two variables,
a = 23;
b = 10;
The binary representation of the above two variables would be:
a = 0001 0111
b = 0000 1010
When we apply the bitwise OR operator in the above two variables,
i.e., a|b , then the output would be:
Result = 0001 1111

Code:
Output:

PROGRAM 7

Aim: Write a C program to perform relational operations on two


variables.
Theory: Relational operators are a type of binary operator. It is used for
two operands to draw a comparison between them. Some of the
relational operators are ‘>’, ‘< ‘, ‘=< ‘, ‘=>’, ‘==’.

Algorithm: Declare two integer type variables a and b. Using scanf


function input the values for those variables from user. Using relational
operators in a printf function print their outputs.

Code:

Output: Using relational operators prints the greater value between


them.
PROGRAM 8

Aim: Write a C program to find whether the entered number is prime.

Theory: The program will read a value from the user and check for it
being a prime number or not. Starting from 2, the program will divide
the input number untill it is divided with each number till half of what
number was entered by the user. If the remainder in each case remains
not equal to zero, the program declares the input number as a prime
number. Otherwise, the number entered is a non-prime number.
Algorithm: Declare an integer variable n and by using scanf function
take input from the user. Divide n by each number till n/2 by using for
loop. If remainder is zero in any case print x is not a prime number.
Using else statement print n is a prime number

Code:
Output: Displaying if a number entered by the user is prime or not.

PROGRAM 9

Aim: Write a C program to find sum of a 5-digit number.

Theory: The program accepts a value from the user and calculates the
sum of each digit of 5-digit number entered by the user. The program
stores the last digit of the number in integer type variable and then omits
that last digit from the original entered number.

Algorithm: The program accepts the value from the user and divides it
into 10 and stores the remainder and then the number in its integer form
is reduced by the factor of 10 and the process is repeated. This is carried
out as long as the number is reduced by the of 10 is greater than 0 after
reduction.
Code:

Output: Displaying the sum of digits of a 5-digit number entered by the


user.
PROGRAM 10

Aim: Write a C program to reverse a 5-digit number.

Theory: This program reverses the 5-digit number entered by the user
and then prints the reversed number on the screen. For example, if user
enters a number 12345 as input, then 54321 is printed as output. In our
program we use (%) operator to obtain the last digit of the number. In
this way we will be able to invert the number and display it from right to
left.

Algorithm: Declare an integer variable x, rev. Using while loop


compute these equations
rem = x%10
[rev = rev*10 + rem and x = x/10]
Print rev to get the reverse of the entered number.

Code:
Output: Displaying the reverse of the 5-digit number input by the user.

PROGRAM 13
Aim: Write a C program to find the area and perimeter of a circle,
rectangle, square and triangle using functions.

Theory: This program is used to find the perimeter of a circle, rectangle


and triangle. The formula used in this program are:

perimeter of rectangle: 2 * (a + b)

Area of rectangle: a*b

perimeter of triangle: a + b + c

area of triangle: (s(s-a) (s-b) (s-c))1/2

perimeter of circle: 2 * pi * r area of circle: 3.14*r*r.

Algorithm: Create functions for area and perimeter of different figures


of type float with required variables.
Declare variables length, breadth, radius, area, perimeter, a, b, c of type
float.
Take input regarding the figure.
Using switch statement, calculate the area and perimeter of the required
figure by passing the values to the functions.
Display the values of area and perimeter.
Code:
#include<stdio.h>
#include<math.h>
int main(){
int choice;
printf("Enter 1 for Triangle\n2 for Square\n3 for Reactagle\n4
for Circle\n");
scanf("%d",&choice);
switch(choice) {
case 1: {
int a,b,c;
float s,area,perimeter;
printf("Enter sides of triangle\n");
scanf("%d%d %d",&a,&b,&c);
s=(float)(a+b+c)/2;
area=(float)(sqrt(s*(s-a)(s-b)(s-c)));
printf("Area of Triangle is %f\n",area);
perimeter = a+b+c;
printf("Perimeter of the triangle is : %f\n",perimeter);
break;
}
case 2 : {
int side;
printf("Enter side of the square\n");
scanf("%d",& side);
printf("The area of the square is %d\n",side * side);
printf("The perimeter of the square is %d\n",side*4);
break;
}
case 3 : {
int length,breadth;
printf("Enter length of the Rectangle\n");
scanf("%d",& length);
printf("Enter breadth of the Rectangle\n");
scanf("%d",& breadth);
printf("The area of the Reactangle is %d\n",length *
breadth);
printf("The perimeter of the square is %d\
n",2*(length+breadth));
break;
}
case 4 : {
int radius;
printf("Enter Radius of the Circle\n");
scanf("%d",& radius);
printf("The area of the Circle is %f\n",3.14*radius*radius);
printf("The perimeter of the square is %f\n",2*3.14*radius);
break;
}
default:{
printf("Invalid Choice\n");
break;
}
}
}

Output:
PROGRAM 14

Aim: Write a C program to compute the factorial using recursion.

Theory: The product of positive integers from 1 to N is called factorial


N. It can also be defined as the factorial of a number N as the product of
the first n natural numbers. Factorial N is denoted by N!
Were,

N! = 1. 2. 3 ……...(N-2). (N-1). N

This function is defined for all the positive integers including 0. The
factorial of factional and negative numbers is not defined.
Algorithm: Declare a recursive function factorial ( ). Take input from
the user n. Using conditional statements If n = 0 or 1 then return 1. Else
return n*factorial(n-1). Print the value of factorial N.

Code:

Output:
PROGRAM 15

Aim: Write a C program to generate the Fibonacci sequence using


recursion.

Theory: Fibonacci series is defined as a sequence of numbers in which


the first two numbers are 1 and 1 and each subsequent number is the
sum of the previous two. So, in this series, the nth term is the sum of (n-
1)th term and (n-2)th term.

Mathematically, the nth term of the Fibonacci series can be represented


as:
tn = tn-1 + tn-2
The Fibonacci sequence upto certain term can be represented as: 1, 1, 2,
3, 5, 8, 13, 21, 34...

Algorithm: Declare variables i, n, fib, t1, t2. Initialise the variables t1 =


0, t2 = 1 and fib = 1. Enter the number of terms of fibonacci series to be
printed. Use a for loop to print the numbers.
Code:

Output:

You might also like