You are on page 1of 24

DELHI TECHNOLOGICAL

UNIVERSITY

COMPUTER PROGRAMMING
FUNDAMENTALS

Submitted to- Submitted by-


Dr. Akshi Kumar Varun Utsav
(Assistant Professor DTU ) BTech COE
( 2K19/B3/33 )
INDEX
S.NO TOPIC DONE ON CHECKED ON SIGNATURE
PROGRAM 1

AIM – Write a program to print “Hello World!”

THEORY - # is a preprocessor directive which indicates that the lines beginning with # will be processed by the compiler the
compilation of code. Stdio is a header file which stands for standard input output and facilities in input output. Header files contain
pre-defined functions, etc which can be directly used in the code. Void is return type of the function which indicates that the
function doesn’t return any value. Main is the function name which marks the point of beginning of compilation of program. Printf
is a pre-defined function used for displaying output on the screen. In this problem, we have to display a string on the output screen
for which printf can be used.

PROGRAM -
#include<stdio.h>
#include< conio.h>
int main()
{ printf(“Hello World”);
getch();
}

OUTPUT –

RESULT -
The given string “Hello World!” is displayed on the screen.

CONCLUSION -
This program familiarizes us with the basics structure of C programming and various basic components use in C
programming like header files, standard input output, functions etc. We can use printf() function to display output on the
screen.
PROGRAM 2

AIM – Write a program using if-else statement.

THEORY – The if-else statement in C is used to perform the operations based on some specific condition. The
operations specified in if block are executed if and only if the given condition is true. The if statement is used to check
some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in
the scenario where we need to perform the different operations for the different conditions.

PROGRAM –
#include<stdio.h>

int main()

{ int a;

printf("ENTER THE NUMBER");

scanf("%d",&a);

if(a%2==0)

{ printf("THE NUMBER IS EVEN"); }

else

printf("THE NUMBER IS ODD");

return 0; }

OUTPUT –

RESULT – The number entered by the user was checked using if-else statement and then output was displayed
accordingly.

CONCLUSION – This program familiarizes us with the use of condition statements in C programming and basic
structure of if-else statement.
PROGRAM 3

AIM – Write a program using switch case statement

THEORY – The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute
multiple operations for the different possible values of a single variable called switch variable. Here, We can define
various statements in the multiple cases for the different values of a single variable. First, the integer expression specified
in the switch statement is evaluated. This value is then matched one by one with the constant values given in the different
cases. If a match is found, then all the statements specified in that case are executed along with the all the cases present
after that case including the default statement. No two cases can have similar values. If the matched case contains a break
statement, then all the cases present after that will be skipped, and the control comes out of the switch. Otherwise, all the
cases following the matched case will be executed.

PROGRAM –
#include<stdio.h>

int main()

int x,y,temp;

printf("ENTER A NUMBER ");

scanf("%d",&x);

printf("ENTER ANOTHER NUMBER ");

scanf("%d",&y);

printf("SELECT THE OPERATION \n 1 FOR ADDITION \n 2 FOR SUBTRACTION \n 3 FOR

MULTIPLICATION \n 4 FOR DIVISION \n");

scanf("%d",&temp);

switch(temp)

case 1:

printf("%d",x+y);

break;

case 2:

printf("%d",x-y);

break;

case 3:

printf("%d",x*y);

break;

case 4:
printf("%d",x/y);

break; }

return 0;

OUTPUT –

RESULT –
The numbers entered by the user were undergone operations using switch statement according to the operation choosed
by the user and the desired output was displayed on the screen

CONCLUSION –
This program familiarizes us with the use of condition statements in C programming and basic structure of switch case
statement.
PROGRAM 4

AIM – To write a program to find the average of two numbers.

THEORY – Average of two numbers is basically the sum of both the numbers divided by two. In order to find the
average we can use basic algorithm of finding the sum of two numbers then dividing it by two. We will use float data
type for defining the variable of average, as the average may come in decimals also.

PROGRAM –
#include<stdio.h>

int main()

{ int a, b;

float avg;

printf("ETER TWO NUMBERS \n");

scanf("%d %d", &a, &b);

avg = ((a + b)/2.00);

printf("THE AVERAGE OF TWO NUMBERS IS - %f", avg);

return 0;

OUTPUT –

RESULT – The desired average was displayed on the screen.

CONCLUSION - This program familiarizes us with the use of different datatypes in C programming and basic
structure of defining an equation.
PROGRAM 5

AIM – To write a program to find simple interest.

THEORY – Simple interest of principal, rate and time is basically the product of all of them divided by hundred. In
order to find the simple interest we can use basic algorithm of finding the product of numbers and then dividing it by
hundred. We will use float data type for defining the variable of simple interest, as it may come in decimals also.

PROGRAM –

#include<stdio.h>

int main()

int p, r, t;

float si ;

printf("ENTER PRINCIPAL,RATE,TIME RESPECTIVELY \n");

scanf("%d%d%d",&p,&r,&t);

si=(p*r*t)/100.00;

printf("THE SIMPLE INTEREST IS- \n %f",si);

return 0; }

OUTPUT –

RESULT – The desired simple interest was displayed on the screen.

CONCLUSION – This program familiarizes us with the use of different datatypes in C programming and basic
structure of defining an equation.
PROGRAM 6

AIM – To write a program to find the greatest of three numbers.

THEORY – When a series of decision is required, nested if-else is used. Nesting means using one if-else construct
within another one. This type of structure is known as the else-if ladder. This chain generally looks like a ladder hence it
is also called as an else-if ladder. The test-expressions are evaluated from top to bottom. Whenever a true test-expression
if found, statement associated with it is executed. When all the n test-expressions becomes false, then the default else
statement is executed.

PROGRAM –

#include<stdio.h>

int main()

int a,b,c;

printf("ENTER THREE NUMBERS \n");

scanf("%d%d%d",&a,&b,&c);

printf("THE GREATEST OF THE THREE NUMBERS IS -");

if(a>=b)

{if(a>=c)

{ printf("%d",a); }

else

printf("%d",c);

else

if(b>=c)

{ printf("%d",b); }

else

printf("%d",c);

return 0; }
OUTPUT –

RESULT – The greatest of the three numbers was displayed on the screen.

CONCLUSION - This program familiarizes us with the use of nested if-else statement in C programming and how
to use nested if-else in conditions where more than two cases are formed.
PROGRAM 7

AIM – To write a program to find the greatest of ten numbers.

THEORY – Firstly ten values are entered then they are stored in an array of size 10 let ar[ ] be an array holding these
values. Let us consider a variable 'greatest'. At the beginning of the loop, variable 'greatest' is assigned with the value of
the first element in the array greatest=a[0]. For each value of 'i', value of a[i] is compared with value of variable
'greatest'. If any value greater than the value of 'greatest' is encountered, it would be replaced by a[i]. After completion of
'for' loop, the value of variable 'greatest' holds the greatest number in the array.

PROGRAM –
#include<stdio.h>

int main()

int ar[10],temp;

printf("ENTER TEN NUMBERS\n");

for(int i=0;i<10;i++)

scanf("%d",&ar[i]);

temp=ar[0];

for(int i=0;i<10;i++)

if(ar[i]>temp)

temp=ar[i];

} }

printf("THE GREATEST NUMBER = %d",temp);

return 0;

}
OUTPUT –

RESULT –
The greatest of the ten numbers was displayed on the screen.

CONCLUSION –
This program familiarizes us with the use of arrays in C programming and how to use array to take multiple inputs from
the user where many inputs are required.
PROGRAM 8

AIM – To write a program to print a pattern of triangle.

THEORY – Patterns can be printed in c using nested for and while loops. To print a pattern it is necessary that nested
for loops work with two variables one for manipulation of rows and other for columns. For printing different patterns
different conditions can be used.

PROGRAM –
#include <stdio.h>

int main()

{ int i, space, rows, k=0;

printf("Enter number of rows: ");

scanf("%d",&rows);

for(i=1; i<=rows; ++i, k=0)

{ for(space=1; space<=rows-i; ++space)

{ printf(" "); }

while(k != 2*i-1)

{ printf("* ");

++k; }

printf("\n"); }

return 0;

OUTPUT –

RESULT – The required pattern was displayed on the screen.

CONCLUSION – This program familiarizes us with the use of nested for loops in C programming to print different
types of patterns.
PROGRAM 9

AIM – To print Fibonacci series upto n terms.

THEORY – The Fibonacci sequence is a series where the next term is the sum of pervious two terms. The first two
terms of the Fibonacci sequence is 0 followed by 1.

PROGRAM –
#include<stdio.h>

int main()

{ int n, a=0, b=1;

long long int c;

printf("ENTER THE NUMBER UPTO WHICH YOU WANT TO PRINT FIBONACCI SERIES-\n");

scanf("%d",&n);

printf("THE FIBONACCI SERIES IS -\n %d,%d",a,b);

while(n-2>0) {

c=a+b;

printf(",%d",c);

a=b;

b=c;

n-=1; }

return 0; }

OUTPUT –

RESULT – The required Fibonacci series was displayed on the screen.

CONCLUSION – Fibonacci series can be printed by both by using loop and recursion. Here we have used looping
for printing Fibonacci series.
PROGRAM 10

AIM – To print the reverse of a number.

THEORY – This program takes an integer input from the user. Then the while loop is used until n != 0 is false. In
each iteration of while loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by times.

PROGRAM –
#include<stdio.h>

int main()

int n, t;

printf("ENTER A NUMBER-\n");

scanf("%d",&n);

printf("THE REVERSE OF THE NUMBER IS -\n");

while(n>0)

{ t=n%10;

printf("%d",t);

n=n/10;

return 0; }

OUTPUT –

RESULT – The reverse of the entered number was displayed on the screen.

CONCLUSION – While loop is used to reverse the sequence of numbers.


PROGRAM 11

AIM- To find the factorial of a number.

THEORY- This program takes a positive integer from the user and computes factorial using for loop.

PROGRAM
#include <stdio.h>

int main()

{int i,n=1,a;

printf("enter number ");

scanf("%d",&a);

if(a<0)

printf("error");

else

for(i=1;i<=a;i++)

n=n*i;

printf("factorial is>%d",n);

return(0);

OUTPUT

RESULT - The factorial of a number has been find by using loop.

CONCLUSION - This program familiarizes us with the use of loops.


PROGRAM 12

AIM-To check a leap year.

THEORY- A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year
is a leap year only if it is perfectly divisible by 400.

PROGRAM
#include <stdio.h>

int main()

{int n;

printf("enter year >");

scanf("%d",&n);

if(n%100==0)

if(n%400==0)

printf("year is leap");

else

printf("year is not leap");

else if(n%4==0)

printf("year is leap");

else

printf("year is not leap");

return(0);}

OUTPUT

RESULT - The year is verified whether it is leap or not.

CONCLUSION - This program familiarizes us with the use of if-else statement.


PROGRAM 13

AIM- To find HCF and LCM of two numbers.

THEORY - In this program, two integers entered by the user are stored in variable n1 and n2.Then, for loop is
iterated until i is less than n1 and n2. In each iteration, if both n1 and n2 are exactly divisible by i, the value of i is
assigned to gcd. When the for loop is completed, the greatest common divisor of two numbers is stored in
variable gcd.

PROGRAM
#include <stdio.h>

int main() {

int a, b, x, y, t, gcd, lcm;

printf("Enter two integers\n");

scanf("%d%d", &x, &y);

a = x;

b = y;

while (b != 0) {

t = b;

b = a % b;

a = t; }

gcd = a;

lcm = (x*y)/gcd;

printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);

printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

return0;}

OUTPUT

RESULT - The lcm and hcf have been found by using simple mathematics.
CONCLUSION - This program familiarizes us with the use of mathematical formulas with loops in a program.
PROGRAM 14

AIM- To find a number in a array using linear search.

THEORY- A linear search or sequential search is a method for finding an element within a list. It sequentially
checks each element of the list until a match is found or the whole list has been searched.

PROGRAM
#include <stdio.h>

int main()

{int a[100],i,e,n;

printf("enter number of elements of array");

scanf("%d",&n);

printf("enter elements of array");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("enter element to be found");

scanf("%d",&e);

for(i=0;i<n;i++)

{ if(a[i]==e)

{ printf("element found at %d position",i+1);

break; } }

if(i==n)

printf("element is not present there");

return(0); }
OUTPUT

RESULT - The number can be find in a array using linear search.

CONCLUSION - This program familiarizes with the memory allotment in continuous form in array.
PROGRAM 15

AIM – To check whether a string is palindrome or not.

THEORY – A string is said to be palindrome if it does not gets altered even if we reverse the original string. We
would first take a string and check it whether it is palindrome or not.

PROGRAM –
#include<stdio.h>

#include<string.h>

int main()

{ char a[100];

int n, x;

printf("ENTER A STRING\n");

scanf("%s",&a);

n=strlen(a);

for(int i=0;i<n;i++)

{ if(a[i]==a[n-i-1])

{ x=1; }

else

x=0; }

if(x==1)

{ printf("\nTHE STRING IS PALINDROME"); }

else

printf("\nTHE STRING IS NOT PALINDROME");

return 0; }

OUTPUT –
RESULT – When a palindrome string was entered then desired output was displayed on the screen.

CONCLUSION – This program familiarizes us with the concept of strings and palindrome.
PROGRAM 16

AIM – To find the sum of digits of a five digit number.

THEORY – The main idea to find sum of digits can be divided in three steps. Extract last digit of the given
number. Add the extracted last digit to sum. Remove last digit from given number. As it is processed and not
required any more.

PROGRAM –
#include<stdio.h>

int main()

{ int a, n, sum=0;

printf("ENTER A FIVE DIGIT NUMBER\n");

scanf("%d",&a);

while(a!=0)

n=a%10;

sum+=n;

a=a/10; }

printf("THESUM OF DIGITS IS - %d", sum);

return 0; }

OUTPUT –

RESULT – The desired output was displayed on screen.

CONCLUSION – This program familiarizes us with the concept of number places and while loop.
PROGRAM 17

AIM – To count the number of vowels in a given string.

THEORY – Read the entered string and store the string into the variable ‘s’ using gets(s) function. For loop
iterates through string ‘s’ with the structure for(i=0;s[i];i++), If the ASCII value of s[i] is in the range of 65 to 90 or
97 to 122 then check s[i] is equal to any one of the vowels(a,e,i,o,u).If s[i] is equal to any vowel then increase the
vowel count.

PROGRAM –

#include <stdio.h>

int main() {

char s[] = "TajMahal";

int i = 0;

int vowels = 0;

while(s[i++] != '\0')

{ if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' )

vowels++; }

printf("'%s' contains %d vowels.", s, vowels);

return 0; }

OUTPUT –

RESULT – The desired output was displayed on the screen.

CONCLUSION – This program familiarizes us with the concept of strings and if-else statements used in a
loop.

You might also like