Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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.
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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Program: 4
program to find the average of two number no.*/
Algorithm:
9
#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
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
OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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]);
}
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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#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)
{
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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{
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
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
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
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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
first = second;
second = next;
}
printf("%d\n",next);
}
getch();
return 0;
}
OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
34
35