Cs3271 Programming in C Lab Manual
Cs3271 Programming in C Lab Manual
Cs3271 Programming in C Lab Manual
LABORATORY MANUAL
Course Objectives:
List of Programs
Platform Needed:
TOTAL: 60 PERIODS
Table of Contents
Ex.
Name of the Experiment
No.
3c Write a C program to calculate the factorial of a given number using for loop
8b Pointers to Pointers
9a Nested structure
9b An array of structures
9d Union in c
10 File handling
Ex: No 1a Write a C program to use various IO statements in C
Aim:
To write C programs to demonstrate the uses of various formatted and unformatted input
and output functions, expressions and functional blocks.
Algorithm:
#include <stdio.h>
#include<conio.h>
void main()
{
// using printf() and scanf() for multiple inputs
char gender;
int age;
int c;
char str[100];
clrscr();
// Taking and display character using getchar() and putchar()
printf("\nEnter a character");
c = getchar();
putchar(c);
printf("\nEnter your age and then gender(M, F or O): ");
scanf("%d %c", &age, &gender);
printf("\nYou entered: %d and %c", age, gender);
getch();
}
Sample Output:
Enter a Character G
G
Enter your age and then gender(M,F,O) 21 F
You entered 21 and F
Result:
Aim:
To write C programs to demonstrate the functions of operators and expressions
Algorithm:
#include <stdio.h>
#include<conio.h>
void main()
{
int a=40,b=20;
int Total=0,i;
int m=40,n=20;
int x=1, y;
clrscr();
//ARITHMETIC OPERATORS
printf("ARITHMETIC OPERATORS\n");
printf("Addition of a, b is : %d\n", a+b);
printf("Subtraction of a, b is : %d\n", a-b);
printf("Multiplication of a, b is : %d\n", a*b);
printf("Division of a, b is : %d\n", a/b);
printf("Modulus of a, b is : %d\n", a%b);
//ASSIGNMENT OPERATORS
printf("ASSIGNMENT OPERATORS\n");
for(i=0;i<=10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("\nThe sum of first 10 numbers are = %d", Total);
//RELATIONAL OPERATORS
printf("\nRELATIONAL OPERATORS\n");
if (!(m>n && m !=0))
{
printf("!(%d>%d && %d !=0) is true",m,n,m);
}
else
{
printf("!(%d>%d && %d !=0) is false",m,n,m);
}
//CONDITIONAL OPERATORS
printf("\nCONDITIONAL OPERATORS\n");
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
getch();
}
Output:
ARITHMETIC OPERATORS
Addition of a,b is :60
Subtraction of a,b is :60
Multiplication of a,b is :60
Division of a,b is :60
Modulus of a,b is :60
ASSIGNMENT OPERATORS
The sum of first 10 numbers are 55
RELATIONAL OPERATORS
!(40>20 && 40!=0) is false
CONDITIONAL OPERATORS
Xvalue is 1
Y value is 2
Result:
Aim:
To write C programs to use the decision making constructs if else and goto.
Algorithm:
Program:
include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}
Output:
Enter a number 4
4 is even number
(ii ) goto statements
Algorithm:
Program:
void printNumbers()
{
int n = 1;
label:
printf("%d ",n);
n++;
if (n <= 10)
goto label;
}
// Driver program to test above function
int main() {
printNumbers();
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Result:
Thus a C program to use the decision making constructs if else and goto is written
Aim:
To write C programs to use the decision making constructs switch statements
(i)switch statements
Algorithm:
Algorithm:
Result:
Thus C programs to use the decision making constructs switch statements and break
statement is executed successfully.
3 .a) Write a program in C to display n terms of natural number and their sum.
Aim:
To write C programs to to display n terms of natural number and their sum.
Algorithm:
Output:
Enter a number: 222
Sum of digits of 222 is 6
Result:
Thus C programs to display n terms of natural number and their sum using while
loop was executed successfully.
3. b) Write a C program to print table for the given number using do while loop
Aim:
To write C programs to print table for the given number using do while loop.
Algorithm:
#include<stdio.h>
int main(){
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d \n",(number*i));
i++;
}while(i<=10);
return 0;
}
Output:
Enter a number: 5
5
10
15
20
25
30
35
40
45
50
Result:
Thus C programs to print table for the given number using do while loop was executed
successfully.
3. c) Write a C program to calculate the factorial of a given number using for loop
Aim:
To write C programs to calculate the factorial of a given number using for loop.
Algorithm:
#include <stdio.h>
void main(){
int i,f=1,num;
printf("Input the number : ");
scanf("%d",&num);
for(i=1;i<=num;i++)
f=f*i;
printf("The Factorial of %d is: %d\n",num,f);
}
Output:
Input the number : 5
The Factorial of 5 is: 120
Result:
Thus C programs to calculate the factorial of a given number using for loop was executed
successfully.
4.a) Write a C program to find out the average of 4 integers using array.
Aim:
To write a C program to find out the average of 4 integers using array.
Algorithm:
#include <stdio.h>
int main()
{
int avg = 0;
int sum =0;
int x=0;
int num[4];
avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}
Output:
Enter number 1
10
Enter number 2
10
Enter number 3
20
Enter number 4
40
Average of entered number is: 20
Result:
Thus a C program to find out the average of 4 integers using array was executed successfully.
4.b) Write a C program to Storing elements in a matrix and printing it using 2d array
Aim:
To write a C program to storing elements in a matrix and printing it using 2d array
Algorithm:
STEP 1:Start the program.
STEP 2:Declare a 2d array.
STEP 3:Use for loop to get the elements of the array
STEP 4:Use for loop to print the elements of the array.
STEP 5:Run the program to get the desired output.
STEP 6:Stop the program.
Program:
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}
Output:
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34
Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78
Result:
Thus a C program to storing elements in a matrix and printing it using 2d array was executed
successfully.
4.c) writing a program in C to perform traverse operation on an array
Aim:
To writing a program in C to perform traverse operation on an array
Algorithm:
STEP 1:Start
STEP 2: [Initialize counter variable. ] Set i = LB.
STEP 3:Repeat for i = LB to UB.
STEP 4:Apply process to arr[i].
STEP 5: [End of loop. ]
STEP 6:Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, size;
int arr[]={1, -9, 17, 4, -3};
size=sizeof(arr)/sizeof(arr[0]); //sizeof(arr) will give 20 and sizeof(arr[0]) will give 4
printf("The array elements are: ");
for(i=0;i<size;i++)
printf("\narr[%d]= %d", i, arr[i]);
}
Output:
The array elements are:
arr[0]= 1
arr[1]= -9
arr[2]= 17
arr[3]= 4
arr[4]= -3
Result:
Thus a C program to perform traverse operation on an array was executed successfully.
5) String operations in C Programming
Aim:
Algorithm:
STEP 1:Start
STEP 2: Declare variables
STEP 3: Read the text.
STEP 4: Display the menu options
STEP 5: Compare each character with tab char ‘\t’ or space char ‘ ‘ to count no of words
STEP 6: Find the first word of each sentence to capitalize by checks to see if a character is a
punctuation mark used to denote the end of a sentence. (! . ?)
STEP 7: Replace the word in the text by user specific word if match.
STEP 8: Display the output of the calculations
STEP 9: Repeat the step 4 till choose the option stop.
STEP 10:Stop
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void replace (char *, char *, char *);
int main()
{
char choice.str[200];
int i, words;
char s_string[200], r_string[200];
/* Input text from user */
printf("Enter any text:\n ");
gets(str);
do
{
printf("\n1. Find the total number of words \n");
printf("2. Capitalize the first word of each sentence \n");
printf("3. Replace a given word with another word \n");
printf("4. Stop\n");
printf("Enter your choice : ");
choice=getchar();
switch(choice)
{
case '1' :
i = 0;
words = 1;
/* Runs a loop till end of text */
while(str[i] != '\0')
{
/* If the current character(str[i]) is white space */
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
words++;
}
i++;
}
printf("\nTotal number of words = %d", words);
break;
case '2' :
i = 0;
/* Runs a loop till end of text */
while(str[i] != '\0')
{
if(str[i]=='!' || str[i]=='.' || str[i]=='?')
{
i++;
while(str[i]!=' ' || str[i]!='\n' || str[i]!='\t || str[i] != '\0'’)
{
putchar (toupper(str[++i]));
i++;
}
}
else
putchar (str[i]);
i++;
}
break;
case '3' :
printf("\nPlease enter the string to search: ");
fflush(stdin);
gets(s_string);
printf("\nPlease enter the replace string ");
fflush(stdin);
gets(r_string);
replace(str, s_string, r_string);
puts(str);
break;
case '4' :
exit(0);
}
printf("\nPress any key to continue....");
getch();
}while(choice!=’4’);
return 0;
}
void replace(char * str, char * s_string, char * r_string)
{
char buffer[200];
char * ch;
if(!(ch = strstr(str, s_string)))
return;
strncpy(buffer, str, ch-str);
buffer[ch-str] = 0;
sprintf(buffer+(ch -str), "%s%s", r_string, ch + strlen(s_string));
str[0] = 0;
strcpy(str, buffer);
return replace(str, s_string, r_string);
}
Output:
Result:
Thus a C program to perform string operations was executed successfully.
6 .Sorting using pass by reference
Aim:
#include <stdio.h>
#include <conio.h>
void main()
{
int n,a[100],i;
void sortarray(int*,int);
clrscr();
printf("\nEnter the Number of Elements in an array : "); scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting....\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void sortarray(int* arr,int num)
{
int i,j,temp;
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i] = arr[j];
arr[j] = temp; } }
Output:
Enter the Number of Elements in an array : 5
Enter the Array elements 33
67
21
45
11
After Sorting....
11
21
33
45
67
Result:
Thus a C Program Sorting using pass by reference was executed and the output was obtained.
7. Recursive function
Aim:
To write C programs to find the Fibonacci series of a given number using recursive
function.
Algorithm:
Program:
#include <stdio.h>
int fibonacci(int i)
{
if(i == 0)
{
return 0;
}
if(i == 1)
{
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}
int main() {
int i;
for (i = 0; i < 10; i++) {
printf("%d\t\n", fibonacci(i));
}
return 0;
}
Output:
0
1
1
2
3
5
8
13
21
34
Result:
Thus a C Program to find Fibonacci series was executed and the output was obtained.
8.a) Swapping two numbers using Pointers
Aim:
To write C programs to swap two numbers using pointers.
Algorithm:
Program:
#include <stdio.h>
void swapnum(int *num1, int *num2)
{
int tempnum;
tempnum = *num1;
*num1 = *num2;
*num2 = tempnum;
}
int main( )
{
int v1 = 11, v2 = 77 ;
printf("Before swapping:");
printf("\nValue of v1 is: %d", v1);
printf("\nValue of v2 is: %d", v2);
printf("\nAfter swapping:");
printf("\nValue of v1 is: %d", v1);
printf("\nValue of v2 is: %d", v2);
}
Output:
Before swapping:
Value of v1 is: 11
Value of v2 is: 77
After swapping:
Value of v1 is: 77
Value of v2 is: 11
Result:
Thus a C Program to swap two numbers using pointers was executed and the output was
obtained.
8.b) Pointers to Pointers
Aim:
To write C programs to print a variable using double pointer.
Algorithm:
Program:
#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a; // pointer p is pointing to the address of a
pp = &p; // pointer pp is a double pointer pointing to the address of pointer p
printf("address of a: %x\n",p); // Address of a will be printed
printf("address of p: %x\n",pp); // Address of p will be printed
printf("value stored at p: %d\n",*p); //value stoted at the address contained by p i.e.10 will be printed
printf("value stored at pp: %d\n",**pp);
//value stored at the address contained by the pointer stoyred at pp
}
Output:
address of a: d26a8734
address of p: d26a8738
value stored at p: 10
value stored at pp: 10
Result:
Thus a C Program to print a variable using double pointer was executed and the output
was obtained.
8.c) Array of pointers to character to store a list of strings
Aim:
To write C programs to store a list of strings using Array of pointers to character
Algorithm:
Program:
#include <stdio.h>
const int MAX = 4;
int main ()
{
char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali"
};
int i = 0;
for ( i = 0; i < MAX; i++) {
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
Output:
Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali
Result:
Thus a C Program to store a list of strings using Array of pointers to character was
executed and the output was obtained.
9.a) Nested structure
Aim:
To write C programs to nest two structures.
Algorithm:
Program:
include <stdio.h>
struct complex {
int imag;
float real;
};
struct number {
struct complex comp;
int integer;
} num1;
int main() {
return 0;
}
Output:
Imaginary Part: 11
Real Part: 5.25
Integer: 6
Result:
Thus a C Program to nest two structures was executed and the output was obtained.
9.b) An array of structures
Aim:
To write C programs that stores information of 5 students and prints it using An array of
structures.
Algorithm:
Program:
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Output:
Enter Records of 5 students
Enter Rollno:1
Enter Name:Sonoo
Enter Rollno:2
Enter Name:Ratan
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Sarfraz
Student Information List:
Rollno:1, Name:Sonoo
Rollno:2, Name:Ratan
Rollno:3, Name:Vimal
Rollno:4, Name:James
Rollno:5, Name:Sarfraz
Result:
Thus a C Program that stores information of 5 students and prints it using an array of
structures was executed and the output was obtained.
9.c) Functioning of pointers to structures.
Aim:
To write C programs to store person information using pointer as a reference variable to
structures.
Algorithm:
Program:
#include<stdio.h>
struct person{
int age;
float weight;
};
int main(){
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Enter weight: ");
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
Output:
Let us run the above program that will produce the following result −
Enter age: 45
Enter weight: 60
Displaying:
Age: 45
weight: 60.000000
Result:
Thus a C Program to store person information using pointer as a reference variable to
structures was executed and the output was obtained.
9.d) Union in C
Aim:
To write C programs to store employee details using union.
Algorithm:
Program:
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 12.3;
Output:
Salary = 0.0
Number of workers = 100
Result:
Thus a C Program to store to store employee details using union was executed and the
output was obtained.
10. File handling
Aim:
To write a C program to read name and marks of n number of students from and store them in a
file. If the file previously exits, add the information to the file.
Algorithm:
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
char name[50];
int marks, i, num;
FILE *fptr;
fclose(fptr);
return 0;
}
Output:
Enter number of students 2
For student1
Enter name:pravi
Enter marks:100
For Student 2
Enter name: Theeran
Enter marks:100
Student.txt
Name: pravi
Marks=100
Name: Theeran
Marks=100
Result:
Thus a C Program to store to read name and marks of n number of students from and store them
in a file. If the file previously exits, add the information to the file was executed and the output was
obtained.