خوارزميات عملي ١
خوارزميات عملي ١
خوارزميات عملي ١
LAB MANUAL
(REVISED-4)
PREPARED BY
Abu Salim
For
Lecturer
2
Program 19: Write a program in C to find average of the numbers entered by the user.
Program 20: Write an algorithm to change a numeric grade to a letter grade.
Program 21: Write a Program in c to implement Selection Sort Algorithm?
Program 22: Write a program to implement Bubble sort Algorithm.
Program 23: Write a program to implement Insertion sort Algorithm.
Program 24: Write a program to implement linear Search (sequential search).
Program 25: Write a program to implement Binary Search Algorithm.
Program 26: Write a program in C to calculate factorial of a given number using iteration
Program 27: Write a program in C to calculate factorial of a given number using recursion.
3
What is C?
The C programming language is a popular and widely used programming language for
creating computer programs. Programmers around the world embrace C because it gives
Fixed number of keywords, including a set of control primitives, such as if, for,
while, switch and do while.
Multiple logical and mathematical operators, including bit manipulators.
Multiple assignments may be applied in a single statement.
Function return values are not always required and may be ignored if unneeded.
Typing is static. All data has type but may be implicitly converted.
Basic form of modularity, as files may be separately compiled and linked.
Control of function and object visibility to other files via extern and static attributes.
2. Saving C program
3. Compiling C program
4. Linking C program
5. Loading C program
4
6. Executing C program
1: Creating & Executing C Program: - The first step is to create and edit the source
2: Saving C program: - After writing or editing the source program, it is saved on the
3: Compiling C Program: - In this step, the source program is compiled. The source
program is converted into machine code. The C compiler is used to translate the C program
4: Linking C Program: - In this step, the Linker links object file produced by the
compiler to many other library files. After linking the object code to the libraries, an
5: Loading C program :-In this step the loader loads the executable file from disk into
memory for execution. The program must be loaded into memory for execution.
6: Executing C Program: - In this step, the program is executed on the computer. The
CPU fetches instructions of program from memory one by one and takes action on them.
5
Program 1:
Write a program in C to print "hello" on the screen.
#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("Hello");
getch();
return 0;
}
OUTPUT
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
6
Program 2:
Write a program in C to take input from user using scanf and then print on the
screen using printf.
#include <stdio.h>
#include<conio.h>
int main()
{
int a;
clrscr();
printf("Enter an integer\n");
scanf("%d", &a);
printf("Integer that you have entered is %d\n", a);
getch();
return 0;
}
OUTPUT
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
7
Program 3:
Write a C Program to perform addition, subtraction, division, multiplication, and
modulus.
#include <stdio.h>
#include<conio.h>
int main()
{
float a=10, b=5, c,d,e,f;
int x=8,y=5,z;
clrscr();
c=a+b;
pprintf("\nAddition =\t %f",c);
d=a-b;
printf("\nSubtraction =\t %f",d);
e=a*b;
printf("\nProduct =\t %f",e);
f=a/b;
printf("\nDivision =\t %f",f);
z=x%y;
printf("\nModulous =\t %d",z);
getch();
return 0;
}
OUTPUT
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
8
Program 4:
Write a C Program to perform addition, subtraction, division, multiplication
modulous, use scanf( ) function for input.
#include <stdio.h>
#include<conio.h>
int main()
{
float a, b, c,d,e,f;
int x,y,z;
clrscr();
printf("\nEnter the value of a and b");
scanf("%f%f",&a,&b);
c=a+b;
printf("\nAddition =\t %f",c);
d=a-b;
printf("\nSubtraction =\t %f",d);
e=a*b;
printf("\nProduct =\t %f",e);
f=a/b;
printf("\nDivision =\t %f",f);
printf("\nEnter the value of x and y");
scanf("%d%d",&x,&y);
z=x%y;
printf("\nModulous =\t %d",z);
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
9
Program: 5
Write a program in C to find the average of two numbers.
Algorithm:
Input : Two Numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result of step 2
4: End
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
double a,b,avg ;
clrscr();
printf("Enter the numbers");
scanf("%lf%lf",&a,&b);
avg = (a+b) / 2;
printf("Average of numbers%f",avg);
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
10
Program 6:
Write a Program in C to check whether a number entered by user is even or odd.
#include <stdio.h>
#include<conio.h>
int main()
{
int num;
clrscr();
printf("Enter an integer you want to check: ");
scanf("%d",&num);
if((num%2)==0)
/* Checking whether remainder is 0 or not. */
printf("%d is even.",num);
else
printf("%d is odd.",num);
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
11
Program 07: Write a C Program to show use of UNARY OPERATOR (Increment,
Decrement).
#include<stdio.h>
#include<conio.h>
void main( )
{
int x, s;
clrscr();
printf(" \n input the values of x :");
scanf("%d", &x);
++x;
printf("\n x=%d",x);
++x;
printf("\n x=%d",x);
printf(" \n input the values of s :");
scanf("%d", &s);
--s;
printf("\n s=%d",s);
--s;
printf("\n s=%d",s);
getch();
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
12
Program 08: Write a C Program to show use of Logical, Assignment and Conditional
Operator.
#include<stdio.h>
#include<conio.h>
void main( )
{
int x,s,d;
clrscr();
printf("\n input x\t");
scanf("%d", &x);
printf("\n input s\t");
scanf("%d",&s);
printf("x=%d",x);
printf("\n if else\n");
if(x>4)
{
printf("\nWelcome");
}
else
{
printf("\nHello");
}
printf("\nAssignments Operators\n");
x+=5;
printf("\nAfter Increment x=%d\n",x);
s*=5;
printf("\nAfter Multiplications=%d\n",s);
13
printf("\nConditional Operator\n");
d=(x>s)? 6 : 8;
printf("Value of d=%d",d);
printf("\nBye");
getch();
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
14
Program 09: Write a C Program to show use of IF ELSE Statement.
#include<stdio.h>
#include<conio.h>
void main( )
{
int x,s,d;
clrscr();
printf("\n input x\t");
scanf("%d", &x);
printf("x=%d",x);
printf("\n if else\n");
if(x>4)
{
printf("\nWelcome");
}
else
{
printf("\nHello");
}
printf("\nBye");
getch();
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………-
15
Program 10:
Write a C Program to show use of NESTED_IF_ELSE Statement.
#include<stdio.h>
#include<conio.h>
void main( )
{
int x,s,d;
clrscr();
printf("\n input x\t");
scanf("%d", &x);
printf("\n input s\t");
scanf("%d",&s);
printf("x=%d",s);
printf("\n if else Nested\n");
if(x>4)
{
if(s>5)
{
printf("\nWelcome");
}
else
{
printf("\nGood");
}
}
else
{
printf("\nHello");
}
printf("\nBye");
getch();
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
16
Program 11: Write a C Program to show use of if else if Ladder Statement.
#include<stdio.h>
#include<conio.h>
void main( )
{
int mark;
clrscr();
printf("\nEnter value of Marks");
scanf("%d",&mark);
if(mark>=90)
printf(“\n excellent”);
else if(mark>=80)
printf(“\n very good”);
else if(mark>=70)
printf(“\n good”);
else if(mark>=60)
printf(“\n average”);
else
printf(“\n to be improved”);
printf("\nBye");
getch();
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………-
17
Program 12: Write a C Program to show use of SWITCH Statement.
#include<stdio.h>
#include<conio.h>
void main( )
{
int mark;
clrscr();
printf("\nEnter value of Marks");
scanf("%d",&mark);
switch(marks)
{
case 1:
printf("\nGood");
break;
case 2:
printf("\n Very Good");
break;
default:
printf("\nWelcome");
}
printf("\nBye");
getch();
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
18
Program 13: Write a program in C to display welcome 10 times using while.
#include<stdio.h>
#include<conio.h>
int main()
{
int n=10;
while(n > 0)
{
printf("\nWelcome");
n = n - 1;
}
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
19
Program 14: Write a Program in C to show use of do while statement.
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1;
do
{
printf(“Value of i=%d\n”,i);
++i;
}
while(i<=10);
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
20
Program 15: Write a program in C to display welcome 10 times using for.
#include<stdio.h>
#include<conio.h>
int main()
{
int I;
clrscr();
for(i=1;i<=10;++i)
{
printf(“WELCOME\n”);
}
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
21
Program 16: Write a program in C to show use of break statement.
#include<stdio.h>
#include<conio.h>
int main()
{
int I;
clrscr();
for(i=1;i<=10;++i)
{
printf(“WELCOME\n”);
if(i==4)
break;
}
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
22
Program 17: Write a program in C to show use of goto statement.
#include<conio.h>
int main()
{
int n=1,x,sum=0;
clrscr();
while(n<=10)
{
printf(“\n Enter the number x”);
scanf(“%d” ,&x);
if(x<0)
goto error;
++n;
}error:printf(“\n the number x is non negative”);
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
23
Program 18: Write a program in C to show use of Array .
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
printf(“\n Numbers Are”);
for(i=1;i<10;++i)
{
printf(“%d”,a[i]);
}
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
24
Program 19: Write a program in C to find average of the numbers entered by the
user.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
float x[100],avg=0;
clrscr();
printf(“\n Enter the noumber of values “);
scanf(“%d”,&n);
printf(“\n Input the numbers”);
for(i=1;i<=n;++i)
{
scanf(“%f”,&x[i]);
avg=avg+x[i];
}
avg=avg/n;
printf(“\n Average=%f”,avg);
getch();
return 0;
}
25
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
26
Program 20:
Write an algorithm to change a numeric grade to a letter grade.
Algorithm:
27
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
float marks=0;
clrscr();
printf("Enter grade :");
scanf("%f",&marks);
if(marks>=90 && marks<=100)
printf("\nYour grade is A");
else if(marks>=79.5 && marks <90)
printf("\nYour grade is B+");
else if(marks>=75.5 && marks <79.5)
printf("\nYour grade is B");
else if(marks>=70.5 && marks <75.5)
printf("\nYour grade is B-");
else if(marks>=65.5 && marks <70.5)
printf("\nYour grade is C+");
else if(marks>=60.5 && marks <65.5)
printf("\nYour grade is C");
else if(marks>=50.5 && marks <60.5)
printf("\nYour grade is C-");
else if(marks>=0.00 && marks <50.5)
28
printf("\nYour grade is D");
else
printf("\nInvalid Input...");
getch();
printf("\n\nPress any key to terminate...");
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
29
Program 21:Write a Program in c to implement Selection Sort Algorithm ?
1. List is divided into Sorted and Unsorted
2. Select the Smallest from Unsorted and Swap this number with the first element.
3. After each Selection and Swap, move the wall one element ahead.
4. Repeat steps 2 and 3 until all elements are taken from Unsorted.
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int A[20],N,Temp,i,j;
clrscr();
printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
scanf("%d",&N);
printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
for(i=1; i<=N; i++)
{
scanf("\n\t\t%d",&A[i]);
}
30
if(A[i]>A[j])
{
Temp = A[i];
A[i] = A[j];
A[j] = Temp;
}
printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
for(i=1; i<=N; i++)
printf("\n\t\t\t%d",A[i]);
printf("\n\n-----------------------");
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
31
…………………………………………………………………………
…………………………………………………………………………
Program 22: Write a program to implement Bubble sort Algorithm.
Algorithm:
1. List is divided into Sorted and Unsorted
2. Smallest is Bubbled from Unsorted list and moved to sorted list.
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int A[20],N,Temp,i,j;
clrscr();
printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
scanf("%d",&N);
printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
for(i=1; i<=N; i++)
{
scanf("\n\t\t%d",&A[i]);
}
for(i=1; i<=N-1; i++)
for(j=1; j<=N-i;j++)
32
if(A[j]>A[j+1])
{
Temp = A[j];
A[j] = A[j+1];
A[j+1] = Temp;
}
printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
for(i=1; i<=N; i++)
printf("\n\t\t\t%d",A[i]);
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
33
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
Program 23: Write a program to implement Insertion sort Algorithm.
1. List is divided into Sorted and Unsorted
2. First element of unsorted list is transferred to the Sorted list and Inserted in
correct position.
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int A[20],N,Temp,i,j;
clrscr();
printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
scanf("%d",&N);
printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
for(i=1; i<=N; i++)
{
scanf("\n\t\t%d",&A[i]);
}
for(i=2; i<=N; i++)
34
{
Temp = A[i];
j = i-1;
while(Temp<A[j] && j>=1)
{
A[j+1] = A[j];
j = j-1;
}
A[j+1] = Temp;
}
printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
for(i=1; i<=N; i++)
printf("\n\t\t\t%d",A[i]);
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
35
…………………………………………………………………………
…………………………………………………………………………
3. Continue Search until the target is found and return its Position
This search is suitable for unordered lists and used for small data lists
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], search, c, n;
clrscr();
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
36
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
37
…………………………………………………………………………
…………………………………………………………………………
2. If found then stop, else see the Target is in the first half or second half.
Binary search is suitable for sorted lists and used for large amount of data
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int a[25], i, n, K, flag = 0, low, high, mid;
clrscr();
printf("Enter the number of elements");
scanf("%d", &n);
printf("Enter the elements");
for(i = 0; i<n; i++)
{
38
scanf("%d",&a[i]);
}
printf("Enter the key elements to be searched ");
scanf("%d",&K);
low = 0;
high = n - 1;
while(low <= high)
{
mid = (low+high)/2;
if(a[mid] == K)
{
flag = 1;
break;
}
else if(K<a[mid])
high = mid-1;
else
low = mid + 1;
}
if(flag == 1)
{
printf("Key element is found");
}
else
39
{
printf("Key element not found");
}
getch();
return 0;
}
OUTPUT
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………
40
Program 26: Write a program in C to calculate factorial of a given number using
iteration
Algorithm:
Input: A positive integer num
1. Set FactN to 1
2. Set i to 1
3. for (i is less than or equal to num)
3.1 Set FactN to FactN x I
3.2 Increment i
End for
4. Return FactN
End
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int c, n, fact = 1;
clrscr();
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
getch();
return 0;
}
41
OUTPUT
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
Program:
#include <stdio.h>
#include<conio.h>
long factorial(int);
int main()
{
int n;
long f;
clrscr();
printf("Enter an integer to find factorial\n");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
42
printf("%d! = %ld\n", n, f);
}
getch();
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
OUTPUT
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
43