Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
LIST OF EXPERIMENTS
1. Write a Program to print ASCII value of a character in C.
2. Write a Program using if-else statement.
3. Write a program using Switch Statement.
4. Write a Program to find the average of n (n < 10) numbers using
arrays.
5. Write a program using for, while and do-while loop in C.
6. Write a Program to implement Call by Value and Call by Reference.
7. Write a program for calculating factorial of a number using Recursion.
8. Write a program to perform String Manipulation by using library
functions.
9. Write a Program to implement Pointer in C.
10. Write a program for additionof two matrices in C.
11. Write a Program to create and store information in file in C.
12. Write a Program to store and display student information using
Structure.
1
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO . 1
OBJECT : Write a Program to print ASCII value of a
character in C.
Program:
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
// Reads character input from the user
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}
Output
Enter a character: G
ASCII value of G = 71
2
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO. 2
OBJECT: Write a Program using if-else
statement.
Program:
// Program to check whether an integer entered by the user is odd
or even
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
Output
Enter an integer: 7
7 is an odd integer.
3
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO: 3
OBJECT: Write a program using Switch Statement.
Program:
int main()
{
int i=2;
switch (i)
{
case 1:
printf("Case1 ");
break;
case 2:
printf("Case2 ");
break;
case 3:
printf("Case3 ");
break;
case 4:
printf("Case4 ");
break;
default:
printf("Default ");
}
return 0;
}
Output:
Case 2
4
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO: 4
OBJECT: Write a Program to find the average of n (n <
10) numbers using arrays.
Program:
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average marks = %d", average);
return 0;
}
Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
5
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO : 5
OBJECT: Write a program using for, while and do-while
loop in C.
Program:
for loop
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when n is less than count
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
return 0;
}
Output
Enter a positive integer: 10
Sum = 55
6
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
while loop:
// Program to find factorial of a number
// For a positive integer n, factorial = 1*2*3...n
#include <stdio.h>
int main()
{
int number;
long long factorial;
printf("Enter an integer: ");
scanf("%d",&number);
factorial = 1;
// loop terminates when number is less than or equal to 0
while (number > 0)
{
factorial *= number;
// factorial = factorial*number;
--number;
}
printf("Factorial= %lld", factorial);
return 0;
}
Output
Enter an integer: 5
Factorial = 120
do-while loop:
//Program to print table for the given number using //do while loop
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,number=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);
7
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
do{
printf("%d \n",(number*i));
i++;
}while(i<=10);
getch();
}
Output
Enter a number: 5
5
10
15
20
25
30
35
40
45
50
8
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO : 6
OBJECT: Write a Program to implement Call by Value
and Call by Reference.
Program:
Call by Value:
#include <stdio.h>
void swapByValue(int, int); /* Prototype */
int main() /* Main function */
{
int n1 = 10, n2 = 20;
/* actual arguments will be as it is */
swapByValue(n1, n2);
printf("n1: %d, n2: %d\n", n1, n2);
}
void swapByValue(int a, int b)
int t;
t = a; a = b; b = t;
}
OUTPUT
n1: 10, n2: 20
9
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
Call by Reference:
#include <stdio.h>
void swapByReference(int*, int*); /* Prototype */
int main() /* Main function */
{
int n1 = 10, n2 = 20;
/* actual arguments will be altered */
swapByReference(&n1, &n2);
printf("n1: %d, n2: %d\n", n1, n2);
}
void swapByReference(int *a, int *b)
{
int t;
t = *a; *a = *b; *b = t;
}
OUTPUT
n1: 20, n2: 10
10
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO : 7
OBJECT : Write a program for calculating factorial of a
number using Recursion.
Program:
/*
* C Program to find factorial of a given number using recursion
*/
#include <stdio.h>
int factorial(int); //prototype
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{return 1;
}
else
{ return(num * factorial(num - 1));
}}
OUTPUT:
Enter a number to find it's Factorial: 6
The Factorial of 6 is 720.
11
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO : 8
OBJECT: Write a program to perform String
Manipulation by using library functions.
Program:
strlen:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "BeginnersBook";
printf("Length of string str1: %d", strlen(str1));
return 0;
}
Output:
Length of string str1: 13
strcmp:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "BeginnersBook";
char s2[20] = "BeginnersBook.COM";
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}
12
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
Output:
string 1 and 2 are different
strcat:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}
Output:
Output string after concatenation: HelloWorld
strcpy:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
/* this function has copied s2 into s1*/
strcpy(s1,s2);
printf("String s1 is: %s", s1);
return 0;
}
Output:
String s1 is: string 2: I’m gonna copied into s1
13
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO : 9
OBJECT: Write a Program to implement
Pointer in C.
Program:
/* Source code to demonstrate, handling of pointers in C program
*/
#include <stdio.h>
int main(){
int* pc;
int c;
c=22;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
return 0;
}
Output
Address of c: 2686784
Value of c: 22
Address of pointer pc: 2686784
Content of pointer pc: 22
Address of pointer pc: 2686784
Content of pointer pc: 11
Address of c: 2686784
Value of c: 2
14
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO : 10
OBJECT: Write a program for addition of two matrices
in C.
Program:
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-\n");
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;
}
15
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
Output
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
1 2
3 4
Enter the elements of second marix
5 6
2 1
Sum of entered matrices:-
6 8
5 5
16
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO : 12
OBJECT: Write a Program to store and display student
information using Structure.
Program:
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
puts(s.name);
printf("Roll number: %d\n",s.roll);
printf("Marks: %.1f\n", s.marks)
return 0;
}
Output
Enter information:
Enter name: Jack
Enter roll number: 23
Enter marks: 34.5
Displaying Information:
Name: Jack
Roll number: 23
Marks: 34.5
18
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
19