C lab Manual print
C lab Manual print
C lab Manual print
EX
NO DATE EXPERIMENT LIST PAGE SIGNATURE
NO
1.a. Write a C program to use various IO statements
in C
7 Recursive Function
9.d. Union in C
Aim:
To write C programs to demonstrate the use of various formatted and unformatted input and
output functions, expressions and functional blocks.
Algorithm:
STEP 1: Start the program.
STEP 2: Declare all required variable and initialize them.
STEP3: Get input values from the user for arithmetic operation.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char gender;
int age;
int c;
char str[100];
clrscr();
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:%dand %c”,age,gender);
getch();
}
Sample Output:
Enter a Character G
G
Enter your age and then gender(M,F,O)21F
You entered 21 and F
Result:
Aim:
To write a C program to demonstrate the functions of operators and expressions.
Algorithm:
STEP 1: Start the program
STEP 2: Declare all required variable and initialize then.
STEP 3: Performs the various operations using the corresponding C operators.
STEP 4: Obtain the desired output.
Program:
#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();
printf(“ASSIGNMENT OPERATORS\n”);
for(i=0;i<=10;i++)
{
total+=i;
}
printf(“\nThe sum of first 10 numbers are=%d”,Total);
printf(“\nREALTIONAL 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);
}
printf(“\nCONDITIONAL OPERATORS\n”);
y=(x==1 ? 2:0);
printf(“x values is %d\n”,x);
printf(“y values is %d\n”,y);
getch();
}
Output:
ARITHMETIC OPERATORS
Addition of a,b is: 60
RELATIONAL OPERATORS
!(40>20&&40!=0) is false
CONDITIONAL OPERATORS
X value is 1
Y value is 2
Result:
Thus the C program to demonstrate the functions of operators and expressions was written and
executed successfully.
DATE: 2a) Decision making constructs: if-else and goto
Aim:
To write a C program to use the Decision making constructs: if-else and goto.
Algorithm:
STEP 1: Start the program
STEP 2: Declare all required variable and get the input from user.
STEP 3: Check the given input is even or not.
STEP 4: Obtain the desired output.
Program:
#include<stdio.h>
#include<conio.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
Program:
void printnumbers()
{
int n=1;
label:
printf(“%d”,n);
n++;
if(n<=10)
goto label;
}
int main()
{
printnumbers();
return 0;
}
OUTPUT:
1 2 3 4 5 6 7 8 9 10
Result:
Thus the C program to use the Decision making constructs: if-else and goto was written and
executed successfully.
DATE: 2b) Decision making constructs: switch statements, break-continue
Aim:
To write a C program to use the Decision-making constructs switch statements, break-continue.
Algorithm:
STEP 1: Start the program
STEP 2: Declare two variables.
STEP 3: Declare switch statements and use arithmetic operators in each case.
STEP 4: Obtain the desired output.
Program:
#include<stdio.h>
#includes<conio.h.>
int main()
{
for(int i=1;i<=10;i++)
{
if(i==5)
{
break;
}
printf(“%d”,i);
}
return 0;
}
OUTPUT:
1234
b) Program:
#include<stdio.h>
int main()
{
for(int i=1;i<=10;i++)
{
if(i==4)
{
continue;
}
printf(“%d”,i);
}
}
return 0;
}
OUTPUT:
1 2 3 4 5 6 7 8 9 10
Result:
Thus the C program to use the Decision making constructs switch statements, break-continue
was written and executed successfully.
DATE: 3a) Write a c program to display n terms of natural number and their sum.
Aim:
To write a C program to display n terms of natural number and their sum.
Algorithm:
STEP 1: Start the program
STEP 2: Get a natural number as input from the user.
STEP 3: Use while loop to find the sum of the digits.
STEP 4: Obtain the desired output.
Program:
#include<stdio.h>
#include<conio.h.>
int main()
{
remainder=n%10;
sum+=remainder;
n/=10;
}
printf(“sum of digits of %d is %d”,num,sum);
return 0;
}
OUTPUT:
Enter a number: 222
Sum of digits of 222 is 6.
Result:
Thus, the C program to display n terms of natural number and their sum using while loop was
written and executed successfully.
DATE: 3b) Write a c program to print table for the given number using do while loop
Aim:
To write a C program to print for the given number using do while loop.
Algorithm:
STEP 1: Start the program
STEP 2: Get a number as input from the user.
STEP 3: Use do while loop to print the table of the given number.
STEP 4: Run the program to get the desired output.
Program:
#include<stdio.h>
int main()
{
int i=1, number=0;
printf(“Enter a number”);
scanf(“%d”,&n);
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, the C program to print table for the given number using do while loop was executed
successfully.
DATE: 3c) Write a c program to calculate the factorial of a given number using for loop
Aim:
To write a C program to calculate the factorial of a given number using for loop.
Algorithm:
STEP 1: Start the program
STEP 2: Get a number as input from the user.
STEP 3: Use do while loop to print the table of the given number.
STEP 4: Run the program to get the desired output.
STEP 5: Stop the program.
Program:
#include<stdio.h>
void main()
{
int i,if=1,num;
printf(“Inputthe number”);
scanf(“%d”,&num);
for(i=1;i<=num;i++)
f=f*i;
printf(“The factorial of %d is:%d\n’,num,f);
}
OUTPUT:
Enter a number: 5
The factorial of 5 is: 120
Result:
Thus, the C program to calculate the factorial of a given number using for loop was executed
successfully.
DATE: 4a) Write a c program to find out the average of 4 integer using array.
Aim:
To write a C program to find out the average of 4 integer using array.
Algorithm:
STEP 1: Start the program
STEP 2: Declare an array with size 4.
STEP 3: Use the loop to get the elements of the array.
STEP 4: Use the loop to perform the average of the array.
Program:
#include<stdio.h>
int main()
{
int avg=0;
int sum=0;
int x=0;
int num[4];
for(x=0; x<4;x++)
{
OUTPUT:
Enter a number: 1
10
Enter a number: 1
20
Enter a number: 1
30
Enter a number: 1
40
Result:
Thus, the C program to find out the average of 4 integer using arraywas executed successfully.
DATE: 4b) Write a C program to Storing elements in a matrix and printing it using 2d
array.
Aim:
To write a C program to store elements in a matrix and printing it using 2d array.
Algorithm:
STEP 1: Start the program
Program:
#include<stdio.h>
void main()
{
int arr[3][3],I,j;
for(i=0; i<3;i++)
{
for(i=0; i<3;i++)
{
printf(“Enter a[ %d][%d]:”,i,j);
scanf(“%d”,&arr[i][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
Printing the elements….
56 10 30
34 21 34
45 56 78
Result:
Thus, the C program to Storing elements in a matrix and printing it using 2d array was executed
successfully.
DATE: 4c) Write a C program to perform traverse operation on an array.
Aim:
To write a C program to perform traverse operation on an array.
Algorithm:
STEP 1: Start the program
STEP 2: Initialize counter variable. Set i=LB.
STEP 3: Repeat for i=LB to UB.
STEP 4: Apply process to arr [i].
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]);
printf(“The array elements are:”);
for(i=0; i<size;i++)
printf(“\n arr[%d] a,arr[i]);
}
OUTPUT:
The array elements are:
a[0]=1
a[1]=-9
a[2]=17
a[3]=4
a[4]=-3
Result:
Thus a C program to perform traverse operation on an array was executed successfully.
DATE: 5) String operation in C programming
Aim:
To write a C program to perform String operations.
Algorithm:
STEP 1: Start the program
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 text by user specific word if match.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void replace(char*,char*);
int main()
{
char choice.str[200];
int i, words;
char s_string[200],r_string[200];
printf(“Enter any text:\n”);
gets(str);
do
{
switch(choice)
{
case ‘1’:
i=0;
words=1;
while(str[i]!=’\0’)
{
if(str[i]==’ ‘||str[i]==’\t’)
{
words++;
}
i++;
}
printf(“\n Total number of words=%d”,words);
break;
case ‘2’:
i=0;
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(“\n please enter the string to search:”);
flush(stdin);
gets(s_string);
printf(“\n Please enter the replace string”);
flush(stdin);
gets(r_string);
replace(str,s_string,r_string);
puts(str);
break;
case ‘4’:
exit(0);
}
strncpy(buffer,str,ch_str);
buffer[ch-str]=0;
sprint(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:
Enter any text:
I like C and C++ programming!
Result:
Thus a C program to perform string operations was executed successfully.
DATE: 6. SORTING Using Pass by reference
Aim:
Algorithm:
STEP 1: Start
STEP 3: Read the input for number of elements and each element.
STEP 5: Compare the elements in each pass till all the elements are sorted.
STEP 7:Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
int n,a[100],i;
void sortarray(int*,int);
clrscr();
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
sortarray(a,n);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
getch();
int i,j,temp;
for(i=0;i<num;i++)
for(j=j+1;j<num;j++)
if(arr[i]>arr[j])
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
Output:
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.
DATE: 7). Recursion function
Aim:
To write a C program to the Fibonacci series of a given numbers using Recursion function.
Algorithm:
STEP 1: Start
STEP 3: Use recursive function to find the Fibonacci series of the given number.
STEP 5: Stop
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()
for(i=0;i<10;i++)
printf(“%d\t\n”,fibonacci(i));
return 0;
Output:
21
34
Result:
Thus a C program to find Fibonacci series was executed and the output was obtained.
DATE: 8. a) Swapping of two numbers using Pointers
Aim:
Algorithm:
STEP 3: Write swap function and pass reference of the declared variables to it.
Program:
#include<stdio.h>
int tempnum;
tempnum=*num1;
*num1=*num2;
*num2=tempnum;
int main()
int v1=11,v2=77;
swapnum(&v1,&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 find swap two numbers using pointers was executed and the output was
obtained.
DATE: 8. b) Pointers to pointers
Aim:
Algorithm:
STEP 3: Use the printf function to print double variable pointer variable.
Program:
#include<stdio.h>
void main()
int a=10;
int *p;
int **pp;
p=&a;
pp=&p;
printf(“address of a:%x\n”,p);
printf(“address of p:%x\n”,pp);
}
OUTPUT:
address of a: d26a8734
address of p:d26a8738
RESULT:
Thus a C program to print a variable using double Pointers was executed and the output was obtained.
DATE: 8. c) Array of Pointers to character to store lists of strings
Aim:
Algorithm:
Program:
#include<stdio.h>
int main()
int i=0;
for(i=0;i<MAX;i++)
printf(“values of names[%d]=%s\n”,names[i]);
return 0;
}
OUTPUT:
RESULT:
Thus a C program to store a list of strings using array of pointers to character was executed and the
output was obtained.
DATE: 9. a) Nested Structure
Aim:
Algorithm:
Program:
#include<stdio.h>
struct complex
int imag;
float real;
}:
struct number
int integer;
num1;
int main()
{
num1.comp.imag=11;
num1.comp.real=5.25;
num1.integer=6;
printf(“Imaginary Part:%d\n”,num1.comp.imag);
printf(“Real Part:%2f\n”,num1.comp.real);
printf(“Integer:%d”,num1.integer);
return 0;
OUTPUT:
Imaginary Part: 11
Integer: 6
RESULT:
Thus a C program to nest two structures executed and the output was obtained.
DATE: 9. b) An array of structures
Aim:
To write C programs that stores information of 5 students and prints it’s using an array of structures
Algorithm:
Program:
#include<stdio.h>
#include<string.h>
struct student
int rollno;
char name[10];
};
int main()
int i;
scanf(“%d”,&st[i].rollno);
scanf(“%d”,&st[i].name);
for(i=0;i<5;i++)
printf(“\n Rollno:%d,Name:%s”,st[i].rollno,st[i].name);
return 0;
}
OUTPUT:
Enter Rollo: 1
Enter Rollo: 2
Enter Rollo: 3
Enter Rollo: 4
Enter Rollo: 5
Result:
Thus a C program that stores information of 5 students and prints its using an array of structures was
executed and the output was obtained.
DATE: 9. c) Functioning of pointers to structures
Aim:
Algorithm:
Program:
#include<stdio.h>
#include<string.h>
struct person
int age;
float weight;
};
int main()
personptr=&person1;
printf(“Enter age:”);
scanf(“%f”,&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 person information using pointer as a reference variable to structures was
executed and the output was obtained.
DATE: 9. d) Union in C
Aim:
Algorithm:
Program:
#include<stdio.h>
union Job
float salary;
int workerNo;
}j;
int main()
j.workerNo=100;
printf(“Salary=%1f\n”,j.salary);
printf(“Number of workers=%d”,j.workerNo);
return 0:
}
Output:
Salary=0.0
Result:
Thus a C program to store employee details using union was executed successfully.
DATE: 10.File handling
Aim:
To write a C programs to read name and marks of a n number of students from and store them in a
file. If the file previously exists, add the information to the file.
Algorithm:
STEP 3: Use the FILE operation like fopen to open the file.
STEP 4: Add the students name and marks by using for loop.
Program:
#include<stdio.h>
#include<conio.h>
int main()
char name[50];
int marks,i,num;
FILE *fptr;
scanf(“%d”,&num);
fptr=(fopen(“C:\\student.txt”,”a”));
if(fptr==NULL)
{
printf(“Error!”);
exit(1);
for(i=0;i<num;++i)
scanf(“%d”,name).;
scanf(“%d”,marks);
fclose(fptr);
return 0;
}
Output:
For student 1
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 a n number of students from and store them in a
file. If the file previously exists, add the information to the file was executed and the output was
obtained.