Programming in c Rt
Programming in c Rt
LIST OF EXPERIMENTS
1
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO . 1
Program:
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
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
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
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
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;
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;
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:
#include <stdio.h>
int main()
{
int number;
long long factorial;
factorial = 1;
// loop terminates when number is less than or equal to 0
while (number > 0)
{
factorial *= number;
// factorial = factorial*number;
--number;
}
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
Program:
Call by Value:
#include <stdio.h>
swapByValue(n1, n2);
}
void swapByValue(int a, int b)
int t;
t = a; a = b; b = t;
}
OUTPUT
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 */
swapByReference(&n1, &n2);
}
void swapByReference(int *a, int *b)
{
int t;
t = *a; *a = *b; *b = t;
}
OUTPUT
10
Programming in C Lab, Branch- Information Technology, II sem, Subject Code-201,
Developed by- Kripa Singh,,Kalaniketan Polytechnic College, Jabalpur
-------------------------------------------------------------------------------------------------------------------------------------------------
EXPERIMENT NO : 7
Program:
/*
* C Program to find factorial of a given number using recursion
*/
#include <stdio.h>
int main()
{
int num;
int result;
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:
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:
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
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 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];
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
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("Name: ");
puts(s.name);
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