Você está na página 1de 35

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEM

DEPARTMENT OF COMPUTER SCIENCE


JAZAN UNIVERSITY, JAZAN

ALGORITHM AND PROGRAMMING LABORATORY (281 CSC-3)

LAB M AN U AL
(REVISED)
ACADEMIC YEAR (2014 2015)

PREPARED BY
Abu Salim
For
College Of Science, Department of Mathematics
Lecturer
DEPARTMENT OF COMPUTER SCIENCE
COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEM
1

JAZAN UNIVERSITY, KSA


Lab Programs List
1. Write a program in C to Print "hello".
2. Write a Program in C that take first input an integer number and then print it. Input is
done using scanf function and number is printed on screen using printf.
3. Write a Program in C to perform the basic arithmetic operation of addition on two
numbers and then print the sum on the screen.
For example if the user entered two numbers as 5, 6 then 11 will be printed on the screen.
4. Write a Program in C to find the average of two numbers.
5. Write a Program in C to change a numeric grade to letter Grade.
6. Write a program in c to check whether a number entered by user is even or odd.
7. Write a program in C to implement selection sort.
8. Write a program in C to implement bubble sort.
9. Write a program in C to implement insertion sort.
10. Write a program in C to implement Linear Search.
11. Write a program in C to implement Binary search.
12. Write a program in C to calculate factorial of a given number using iteration.
2

13. Write a program in C to calculate factorial of a given number using recursion.

ADDITIONAL PROGRAM:
14: Write a Program in C to check alphabet is a vowel or not.
15: Write a Program in C check leap year.
16: Write a Program in C to reverse a number
17: Write a Program in C to print Prime number.
18: Write a Program in C to print Fibonacci series.

What is C?
3

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
maximum control and efficiency to the Programmer.
Following are C programming language features:

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.

Writing, Compilation and Execution of C Program


1. Creating & Editing C Program
2. Saving C program
3. Compiling C program
4. Linking C program
5. Loading C program
6. Executing C program
4

1: Creating & Executing C Program: - The first step is to create and edit the source
program in C. We will use visual studio 2008.
2: Saving C program: - After writing or editing the source program, it is saved on the disk
as text file with an extension ".c".
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
source code into the machine code.
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
executable file with extension EXE is created.
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.

Program 1:
This C Program will print "hello".
5

#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("Hello");
getch();
return 0;
}

OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program 2:
This c program first inputs an integer and then prints it. Input is done using scanf
function and number is printed on screen using printf.
6

#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
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Program 3:

This c program perform the basic arithmetic operation of addition on two numbers
and then prints the sum on the screen. For example if the user entered two numbers
as 5, 6 then 11 will be printed on the screen.
Algorithm:
step1 : Input two numbers, A, B
step 2 : Add the numbers, A+B and Store the

result in S

step 3 : Output the Sum, S


step 4 : End.
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int a, b, c;
clrscr();
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of entered numbers is%d",c);
getch();
return 0;
}
OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------8

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program: 4
program to find the average of two number no.*/
Algorithm:
9

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
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Program: 5:
program to check whether a number entered by user is even or odd.
#include <stdio.h>
10

#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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Program 6:
Write an algorithm to change a numeric grade to a letter grade.
Algorithm:
11

Input: One number


1. if (the number is between 90 and 100, inclusive)
then
1.1 Set the grade to A
End if
2. if (the number is between 80 and 89, inclusive)
then
2.1 Set the grade to B
End if
3. if (the number is between 70 and 79, inclusive)
then
3.1 Set the grade to C
End if
4. if (the number is between 60 and 69, inclusive)
then
4.1 Set the grade to D
End if
5. If (the number is less than 60)
then
5.1 Set the grade to F
End if
6.
Return the grade
End

Program:
#include <stdio.h>
12

#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)
printf("\nYour grade is D");
else
printf("\nInvalid Input...");
getch();
13

printf("\n\nPress any key to terminate...");


getch();
return 0;
}

OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program 7: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.
14

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]);
}

for(i=1; i<=N-1; i++)


for(j=i+1; j<=N;j++)
if(A[i]>A[j])
{
Temp = A[i];
A[i] = A[j];
A[j] = Temp;
}
15

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
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------}
Program 8: 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.
3. Move the wall one element ahead.
4. Repeat step 2 and 3 until all numbers are Sorted.
16

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++)
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]);
17

getch();
return 0;
}

OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program 9: 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.
3. Move the wall one element ahead.
4. Repeat step 2 and 3 until all the elements are sorted.
Program:
#include <stdio.h>
18

#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++)
{
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++)
19

printf("\n\t\t\t%d",A[i]);
getch();
return 0;
}

OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Program 10: Write a program to implement linear Search (sequential search).
Steps of Algorithm:
1. Input all elements into List.
2. Start searching from the beginning of the list
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>
20

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");
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;
}
21

OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program 11: Write a program to implement Binary Search Algorithm.


Steps of Algorithm:
1. Sort the list and check the target with Middle element.
2. If found then stop, else see the Target is in the first half or second half.
3. If Target not found in one half , no need to check that half.
4. Repeat all steps until the target is found.
Binary search is suitable for sorted lists and used for large amount of data
Program:
#include <stdio.h>
#include<conio.h>
int main()
22

{
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++)
{
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
23

low = mid + 1;
}
if(flag == 1)
{
printf("Key element is found");
}
else
{
printf("Key element not found");
}
getch();
return 0;
}

OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

24

Program 12: 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");
25

scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
getch();
return 0;
}
OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Program 13: Write a program in C to calculate factorial of a given number using
recursion.
Algorithm:
Input: A positive integer num
1. if (num is equal to 0)
then
1.1 return 1
else
1.2 return num x Factorial (num 1)
End if
End
Program:
#include <stdio.h>
#include<conio.h>
long factorial(int);
int main()
{
int n;
long f;
clrscr();
26

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);
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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

27

ADDITIONAL PROGRAMS
Program: 14: Write a Program in C to check alphabet is a vowel or not.
#include <stdio.h>
#include<conio.h>
int main()
{
char ch;
clrscr();
printf("Enter a character\n");
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' ||
ch == 'u' || ch
== 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);
28

getch();
return 0;
}

OUTPUT
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Program 15: Write a Program in C check leap year.
#include <stdio.h>
#include<conio.h>
int main()
{
int year;
clrscr();
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
29

printf("%d is not a leap year.\n", year);


getch();
return 0;
}
OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Program 16: Write a Program in C to reverse a number.
#include <stdio.h>
#include<conio.h>
int main()
{
int n, reverse = 0;
clrscr();
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
30

getch();
return 0;
}
OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Program 17: Write a Program in C to print Prime number.
#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();

int n, i = 3, count, c;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
for ( count = 2 ; count <= n ; )
31

{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
getch();
return 0;
}

OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------32

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program 18: Write a Program in C to print Fibonacci series.


#include<stdio.h>
#include<conio.h>
int main()
{
int n, first = 0, second = 1, next, c;
clrscr();
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
33

first = second;
second = next;
}
printf("%d\n",next);
}
getch();
return 0;
}
OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

34

35

Você também pode gostar