Practical Lab Manual For C Programming
Practical Lab Manual For C Programming
Practical Lab Manual For C Programming
LAB MANUAL
I, II SEMESTER
www.uptudunia.com
E-Mail:- [email protected]
List of C Programs
1)C hello world program :- c programming language code to print hello world. This program prints
hello world, printf function is used to display text on screen, '\n' places cursor on the beginning of next line, stdio.h header
file contains declaration of printf function. The code will work on all operating systems may be it's Linux, Mac or any other
and compilers. To learn a programming language you must start writing programs in it and may be your first c code while
learning programming.
int main()
{
char string[] = "Hello World";
printf("%s\n", string);
return 0;
}
C programming code
#include <stdio.h>
int main()
{
int a;
printf("Enter an integer\n");
scanf("%d", &a);
return 0;
}
Output of program:
C programming code
#include<stdio.h>
main()
{
int a, b, c;
c = a + b;
return 0;
}
Output of program
main()
{
int a = 1, b = 2;
a = a + b;
return 0;
}
main()
{
int a, b,
c; char
ch;
while(1)
{
printf("Enter values of a and
b\n"); scanf("%d%d",&a,&b);
c = a + b;
if ( ch == 'y' || ch ==
'Y' ) continue;
else
break;
}
return 0;
}
#include<stdio.h>
main()
{
long first, second, sum;
scanf("%ld%ld", &first,
return 0;
}
return result;
}
We can use bitwise AND (&) operator to check odd or even, as an example consider binary of 7 (0111) when we perform 7 & 1 the result will
be one and you may observe that the least significant bit of every odd number is 1, so ( odd_number & 1 ) will be one always and also (
even_number & 1 ) is zero.
In c programming language when we divide two integers we get an integer result, For example the result of 7/3 will be 2.So we can take
advantage of this and may use it to find whether the number is odd or even. Consider an integer n we can first divide by 2 and then multiply it
by 2 if the result is the original number then the number is even otherwise the number is odd. For example 11/2 = 5, 5*2 = 10 ( which is not
equal to
eleven), now consider 12/2 = 6 and 6 *2 = 12 ( same as original number). These are some logic which may help you in finding if a number
is odd or not.
main()
{
int n;
printf("Enter an
integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even\n" );
else printf("Odd\n"
);
return 0;
}
main()
{
int n;
printf("Enter an
integer\n");
scanf("%d",&n);
if ( n & 1 == 1 )
printf("Odd\n" );
else printf("Even\n"
);
return 0;
}
main()
{
int n;
printf("Enter an
integer\n");
scanf("%d",&n);
if ( (n/2)*2 == n )
printf("Even\n" );
else printf("Odd\n"
);
return 0;
}
main()
{
int n;
printf("Enter an
integer\n");
scanf("%d",&n);
return 0;
}
C programming code
#include <stdio.h>
int main()
{
int first, second, add, subtract,
multiply; float divide;
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
In c language when we divide two integers we get integer result for example 5/2 evaluates to 2. As a general rule integer/integer
= integer and float/integer = float or integer/float = float. So we convert denominator to float in our program, you may also
write float in numerator. This explicit conversion is known as typecasting.
Arithmetic operations.
Output of program:
C programming code
#include <stdio.h>
main()
char ch;
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')
else
return 0;
}
Output:
<stdio.h> main()
char ch;
printf("Enter a haracter\n");
scanf("%c", &ch);
switch(ch)
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
break;
default:
return 0;
}
return 1;
return 0;
C programming code
#include <stdio.h>
int main()
{
int 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
printf("%d is not a leap year.\n", year);
return 0;
}
Compiler
used: GCC
Output of program:
8) Add digits of number in c
C program to add digits of a number: Here we are using modulus operator(%) to extract individual digits of number and
adding them.
C programming code
#include <stdio.h>
main()
{
int n, sum = 0, remainder;
printf("Enter an
integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n %
10; sum = sum +
remainder; n = n /
10;
}
return 0;
}
For example if the input is 98, sum(variable) is 0 initially
98%10 = 8 (% is modulus operator which gives us remainder when 98 is divided
by 10). sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in c whenever we divide integer by another integer we get
an integer. 9%10 = 9
sum = 8(previous value)
+ 9 sum = 17
9/10 = 0.
So finally n = 0, loop ends we get the required sum.
Output of program:
Add digits using recursion
#include <stdio.h>
int add_digits(int);
result = add_digits(n);
printf("%d\n", result);
return 0;
}
int add_digits(int
n) { static int
sum = 0;
if (n ==
0) {
return
0;
}
return sum;
9) Factorial program in c
Factorial program in c: c code to find and print factorial of a number, three methods are given, first one uses a for loop,
second uses a function to find factorial and third using recursion. Factorial is represented using !, so five factorial will be
written as 5!, n factorial as n!. Also n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0!=1.
#include<stdio.h>
Void main()
{
int i,f,n;
f=1;
printf(“enter a number”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf(“Factorial is
%d”,f); getch();
}
Factorial
Output of code:
#include <stdio.h>
long factorial(int);
int main()
{
int number; long fact = 1;
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
return result;
}
long factorial(int);
int main()
{
int num; long f;
if (num < 0)
printf("Negative numbers are not allowed.\n"); else
{
f = factorial(num); printf("%d! = %ld\n", num, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0) return 1; else
return(n * factorial(n-1));
}
10) C program to add n numbers
This c program add n numbers which will be entered by the user. Firstly user will enter a number
indicating how many numbers user wishes to add and then user will enter n numbers. In the first c program
to add numbers we are not using an array, and using array in the second code.
C programming code
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter %d integers\n",n);
return 0;
}
Output of program:
C programming code
#include <stdio.h>
main()
{
int n, reverse = 0;
printf("Enter a number to
reverse\n"); scanf("%d",&n);
while (n != 0)
{
reverse = reverse *
10; reverse =
reverse + n%10; n
= n/10;
}
return 0;
}
Output of program:
main()
{
int n, reverse = 0, temp;
temp = n;
while( temp != 0 )
{
reverse = reverse *
10; reverse = reverse
+ temp%10; temp =
temp/10;
}
if ( n == reverse ) printf("%d
is a palindrome
number.\n", n); else
printf("%d is not a palindrome number.\n", n);
return 0;
}
*
***
*****
***
*
C programming code
#include <stdio.h>
int main()
{
int n, c, k, space = 1;
space = n - 1;
space--;
printf("\n");
}
space = 1;
space++;
printf("*");
printf("\n");
}
return 0;
}
main()
{
int n, c = 2;
return 0;
}
int check_prime(int);
main()
{
int n, result;
result = check_prime(n);
if ( result == 1 )
printf("%d is prime.\n", n); else
printf("%d is not prime.\n", n);
return 0;
}
int check_prime(int a)
{
int c;
return 1;
}
C code
#include<stdio.h>
#include<conio.h>
main()
{
int r;
long number = 0, c, sum = 0, temp;
printf("Enter the maximum range upto which you want to find armstrong
numbers "); scanf("%ld",&number);
main()
{
int n, first = 0, second = 1, next, c;
return 0;
}
Output of program:
17) Linear search in c
Linear search in c programming: The following code implements linear search ( Searching algorithm ) which is used to find
whether a given number is present in an array and if it is present then at what location it occurs.It is also known as sequential
search. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired
element is found or list ends. Linear search in c language for multiple occurrences and using function.
int main()
{
int array[100], search, c, n;
return 0;
}
main()
{
int array[100], search, c, n, position;
if ( position == -1 )
printf("%d is not present in array.\n", search); else
printf("%d is present at location %d.\n", search,
position+1);
return 0;
}
return -1;
}
18) C program for binary search
C program for binary search: This code implements binary search in c language. It can only be used for sorted arrays, but it's
fast as compared to linear search. If you wish to use binary search on an array which is not sorted then you must sort it using
some sorting technique say merge sort and then use binary search algorithm to find the desired element in the list. If the
element to be searched is found then its position is printed.
The code below assumes that the input numbers are in ascending order.
main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of
elements\n"); scanf("%d",&n);
for ( c = 0 ; c < n ;
c++ )
scanf("%d",&arra
y[c]);
printf("Enter value to
find\n");
scanf("%d",&search);
first =
0; last
=n-
1;
middle = (first+last)/2;
return 0;
}
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of
elements\n"); scanf("%d", &n);
d--;
}
}
return 0;
}
-
1
then output of the program ( sum of First and Second matrix ) will
be 5 7 2 9
C programming code
#include <stdio.h>
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
for ( c = 0 ; c < m ;
c++ ) for ( d = 0 ; d
< n ; d++ )
scanf("%d", &first[c][d]);
for ( c = 0 ; c < m ;
c++ ) for ( d = 0 ; d
< n ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ;
c++ ) for ( d = 0 ; d
< n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
printf("\n");
}
return 0;
}
C programming code
#include<stdio.h>
main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
main()
{
char a[100], b[100];
if( strcmp(a,b) == 0 )
printf("Entered strings are
equal.\n"); else
printf("Entered strings are not equal.\n");
return 0;
}
#include<stdio.h>
main()
{
char first[100], second[100], result;
printf("Enter first
string\n"); gets(first);
printf("Enter second
string\n"); gets(second);
if ( result == 0 )
printf("Both strings are
same.\n"); else
printf("Entered strings are not equal.\n");
return 0;
}
first++;
second++;
}
if( *first == '\0' && *second
== '\0' ) return 0;
else
return -1;
}
C code
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100];
printf("Enter the first
string\n"); gets(a);
strcat(a,b);
String concatenation.
Output:
main()
{
char original[100], add[100];
printf("Enter source
string\n"); gets(original);
printf("Enter string to
concatenate\n"); gets(add);
concatenate_string(original, add);
return 0;
}
while(*add)
{
*original =
*add;
add++;
original++;
}
*original = '\0';
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
if( fp == NULL )
{
perror("Error while opening the
file.\n"); exit(EXIT_FAILURE);
}
fclose(fp);
return 0;
}
Output of program:
25) C program for copy a file
C program to copy files: This program copies a file, firstly you will specify the file to copy and
then you will enter the name of target file, You will have to mention the extension of file also.
We will open the file that we wish to copy in read mode and target file in write mode.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, source_file[20],
target_file[20]; FILE *source, *target;
fclose(source);
fclose(target);
return 0;
}
Output:-