BS3171 C-Programming Lab PDF
BS3171 C-Programming Lab PDF
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int m1, m2, m3;
int tot;
float avg;
clrscr();
scanf(“%d %d %d”, &m1, &m2, &m3);
tot=m1+m2+m3;
avg=tot/3;
printf(“%f”, avg);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,t;
clrscr();
printf("enter the value of x=");
scanf("%d",&x);
printf("enter the value of y=");
scanf("%d",&y);
t=x;
x=y;
y=t;
printf("\n after swapping the value:x=%d,y=%d",x,y);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
printf(“enter the value of x=”);
scanf(“%d”,&x);
printf(“/nenter the value of y=”);
scanf (“%d”,&y);
x=x+y;
y=x-y;
x=x-y;
printf(“/n after swapping the value: x=%d, y=%d”,x,y);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float Fahrenheit, Celsius;
celsius=24
fahrenheit=((Celsius*9)/5)+32;
printf(“/n/ntemperature in Fahrenheit is:%f”,Fahrenheit);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0&&year%100!=0&&year%4==0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf(“Enter your age:”);
scanf(“%d”,&age);
if(age>=18)
goto g; //goto label g
else
goto s; //goto label s
g:
printf(“Eligible to vote\n”);
getch();
exit();
s:
printf(“Not Eligible to vote”);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
int x,y;
float z;
clrscr();
printf(“1. Addition\n”); p
rintf(“2.Subraction\n”);
printf(“3.Multiplication\n”);
printf(“4.Division\n”);
printf(“Enter choice:”);
scanf(“%d”,&choice);
printf(“Enter the input values:”);
scanf(“%d”,&x);
scanf(“%d”,&y);
switch(choice)
{
case 1:
z=x+y;
break;
case 2:
z=x-y;
break;
case 3:
z=x*y;
break;
case 4:
z=(float)x/y;
break;
}
Printf(“The result is: %f”,z);
}
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf(“enter a positive number:\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
if(i%2==1)
{
continue;
}
sum+=i;
}
printf(“sum of even numbers from 1 to %d=%d”,n,sum);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n=10,f1=0,f2=1,f3;
clrscr();
printf(“%d\t%d”,f1,f2);
for(i=2;i<n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
printf(“%d\t”,f3);
}
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
{
int n,d=0,sum=0,temp;
clrscr();
printf(“Enter a number:\n”);
scanf(“%d”,&n);
temp=n;
while( n>0)
{
d=n%10;
sum=sum+d*d*d;
n=n/10;
}
if(sum==temp)
{
printf(“Armstrong number”);
}
else
{
printf(“Not an armstrong number”);
}
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
{
int n,i=1,f=1;
clrscr();
printf(“\n Enter the number:”);
scanf(“%d”,&n);
do
{
f=f*i;
i++;
}
while(i<=n);
printf(“\nThe factorial of %d is %d”,n,f);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
void main()
int i,a[50],sum=0,n;
float avg;
clrscr();
printf(“Enter no of elements:”);
scanf(“%d”,&n);
printf(“Enter the values:”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
for(i=0;i<n;i++)
printf(“\na[%d]=%d”,i,a[i]);
printf(\nSum=%d”,sum);
avg=sum/n;
printf(“\nMean=%d”,avg);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
int a[3][3], b[3][3], mul[3][3];
printf("Enter elements of the first matrix:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("a[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("b[%d][%d] = ", i, j);
scanf("%d", &b[i][j]); }
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
mul[i][j] = 0;
for (k = 0; k < 3; k++)
{
mul[i][j] += a[i][k] * b[k][j];
}
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d\t", mul[i][j]);
}
printf("\n");
}
getch();
}
lOMoAR cPSD| 45076656
Program:
#include <stdio.h>
#include <string.h>
void main()
{
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string: ");
scanf("%s", string1);
length = strlen(string1);
for (i = 0; i < length / 2; i++)
{
if (string1[i] != string1[length - i - 1])
{
flag = 1;
break;
}
}
if (flag)
{
printf("%s is not a palindrome\n", string1);
}
else
{
printf("%s is a palindrome\n", string1);
}
getch();
}
lOMoAR cPSD| 45076656
Program:
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char s1[] = "computer";
char s2[]=”science”;
char s[20];
int len,cmp;
clrscr();
strcat(s1, s2);
printf("concatenated string is: %s ", s1);
strcpy(s, s1);
printf(“the copied string is: %s”, s);
len = strlen(s);
printf(“ the length of the string is:%d”,len);
strrev(s2);
printf(“reversed string is : %s”,s2);
cmp=strcmp(s,s1);
printf(“compared value is : %d”,cmp);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int i,j,n;
char str[100][100],s[100];
printf("Enter number of names :");
scanf("%d",&n);
printf("Enter names in any order:");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("The sorted order of names are:”);
for(i=0;i<n;i++)
{
printf("%s",str[i]);
}
}
lOMoAR cPSD| 45076656
Program:
#include <stdio.h>
#include<conio.h>
int addition(int a, int b)
{
int sum = a + b;
return sum;
}
int subtract(int a, int b)
{
int difference = a - b;
return difference;
}
int multiply(int a, int b)
{
int product = a * b;
return product;
}
float division(float a, float b)
{
float quotient = a / b;
return quotient;
}
void main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Arithmetic operations on %d and %d:\n", num1, num2);
printf("Addition: %d\n", addition(num1, num2));
printf("Subtraction: %d\n", subtract(num1, num2));
printf("Multiplication: %d\n", multiply(num1, num2));
printf("Division: %.6f\n", division(num1, num2));
getch();
}
lOMoAR cPSD| 45076656
Program:
#include <stdio.h>
#include<conio.h>
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
printf("After swap: value of a: %d\n", x);
printf("After swap: value of b: %d\n", y);
}
void main()
{
int a = 100;
int b = 200;
printf("Before swap: value of a: %d\n", a);
printf("Before swap: value of b: %d\n", b);
swap(a, b);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include <stdio.h>
#include<conio.h>
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void main()
{
int a = 100;
int b = 200;
printf("Before swap: value of a: %d\n", a);
printf("Before swap: value of b: %d\n", b);
swap(&a, &b);
printf("After swap: value of a: %d\n", a);
printf("After swap: value of b: %d\n", b);
getch();
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int big(int [],int);
void main()
{
int a[SIZE],n,I,b;
clrscr();
printf(“Enter the size of array:”);
scanf(“%d”,&n);
printf(“\n Enter elements:/n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
b=big(a,n);
printf(“/nLargest number:%d”,b);
getch();
}
int big(int a[],int n)
{
int b,i;
b=a[0];
for(i=0;i<n;i++)
if(a[i]>b)
b=a[i];
return
b;
}
lOMoAR cPSD| 45076656
Program:
#include<stdio.h>
#include<conio.h>
int power(int base,int p);
void main()
{
int base,p,result;
clrscr();
printf(“Enter the base number:”);
scanf(“%d”,&base);
printf(“Enter power number:”);
scanf(“%d”,&p);
result=power(base,p);
printf(“%d^%d=%d”,base,p,result);
getch();
}
int power(int base,int p)
{
if(p!=0)
return(base*power(base,p-1));
else
return 1;
}
lOMoAR cPSD| 45076656
Aim:
To write a C program to find biggest of given 3 numbers using pointers
to function.
Algorithm:
STEP 1: Start the program
STEP 2: Read the three numbers
STEP 3: Pass the addresses to function
STEP 4: Find largest number by comparing the numbers
STEP 5: Print the largest number
STEP 6: Stop the program
Program:
#include<stdio.h>
#include<conio.h>
biggest(int *,int *,int *)
void main()
{
int a,b,c;
clrscr();
printf(“Enter three numbers:\n”);
scanf(“%d%d%d”,&a,&b,&c);
biggest(&a,&b,&c);
}
biggest(int *pa,int *pb,int *pc)
{
if(*pa>*pb&&*pa>*pc)
{
printf(“Biggest = %d”,*pa);
}
else if(*pb>*pc&&*pb>*pc)
{
printf(“Biggest = %d”,*pb);
}
else
{
printf(“Biggest = %d”,*pc);
}
getch();
}
lOMoAR cPSD| 45076656
Result:
Thus a C program to find the biggest of three numbers is written, executed
and output is verified.
lOMoAR cPSD| 45076656
Aim:
To write a C program to read and write array elements using pointers .
Algorithm:
STEP 1: Start the program
STEP 2: Read the number of elements
STEP 3: Read the elements in an array
STEP 4: Store the array address in a pointer
STEP 5: Print the array elements by incrementing the pointer
STEP 6: Stop the program
Program:
#include<stdio.h>
#include<conio.h> void main()
{
int *a,i,n;
int b[5];
clrscr();
printf("Enter the no of elements:");
scanf("%d",&n);
printf("Enter the array elements:");
for(i=0;i<5;i++)
scanf("%d",&b[i]);
a=b;
for(i=0;i<5;i++)
{
printf("b[%d]\t\t%d\n",i,*a);
a++;
}
getch();
}
lOMoAR cPSD| 45076656
Result:
Thus C program to read and write array elements using pointer is written,
executed and output is verified.
lOMoAR cPSD| 45076656
Aim:
To write a C program to compare strings using pointers
Algorithm:
STEP 1: Start the program
STEP 2: Initialise two strings to be compared
STEP 3: Pass the strings as pointers
STEP 4: Compare and return 1 if equal
STEP 5: Return 0 if not equal
STEP 6: Stop the program
Program:
#include <stdio.h>
#include<conio.h>
int compare(char* str1, char* str2)
{
while (*str1 == *str2)
{
if (*str1 == '\0' && *str2 == '\0')
return 1;
str1++;
str2++;
}
return 0;
}
int main()
{
char str1[] = "Hello";
char str2[] = "World";
if (compare(str1, str2))
printf("%s and %s are Equal", str1, str2);
else
printf("%s and %s are not Equal", str1, str2);
return 0;
}
lOMoAR cPSD| 45076656
Result:
Thus C program to read and write array elements using pointer is written,
executed and output is verified.
lOMoAR cPSD| 45076656
Aim:
To write a C program print values of pointer to pointer
Algorithm:
STEP 1: Start the program
STEP 2: Initialize a value to a variable
STEP 3: Store the variable to pointer variable
STEP 4: Now store the pointer variable to pointer to pointer variable
STEP 5: Print the values
STEP 6: Stop the program
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int val;
int *ptr;
int **ptr_to_ptr;
val = 10;
*ptr = &val;
**ptr_to_ptr = &ptr;
printf("Value of val: %d\n", val);
printf("Value using ptr: %d\n", *ptr);
printf("Value using ptr_to_ptr: %d\n", **ptr_to_ptr);
getch();
}
lOMoAR cPSD| 45076656
Result:
Thus C program to print values using pointer to pointers is written,
executed and output is verified
lOMoAR cPSD| 45076656
Aim:
To write a C program to write student record using structure
Algorithm:
STEP 1: Start the program
STEP 2: Define a structure for creating student record
STEP 3: Initialise a structure variable to access structure member variables
STEP 4: Obtain values for member variables
STEP 5: print the student record
STEP 6: stop the program
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
char* name;
int roll_number;
int age;
double total_marks;
}
void main()
{
int i = 0;
struct Student student[5];
student[0].roll_number = 1;
student[0].name = "Anand";
student[0].age = 12;
student[0].total_marks = 78.50;
student[1].roll_number = 2;
student[1].name = "Babu";
student[1].age = 10;
student[1].total_marks = 56.84;
student[2].roll_number = 3;
student[2].name = "Daisy";
student[2].age = 11;
student[2].total_marks = 87.94;
student[3].roll_number = 4;
student[3].name = "Ganga";
student[3].age = 12;
student[3].total_marks = 89.78;
lOMoAR cPSD| 45076656
student[4].roll_number = 5;
student[4].name = "Ishwarya";
student[4].age = 13;
student[4].total_marks = 78.55;
printf("Student Records:\n\n");
for (i = 0; i < n; i++)
{
printf("\tName = %s\n",
student[i].name);
printf("\tRoll Number = %d\n",
student[i].roll_number);
printf("\tAge = %d\n", student[i].age);
printf("\tTotal Marks = %0.2f\n\n", student[i].total_marks);
}
getch();
}
Result:
Thus C program to read and write array elements using pointer is written,
executed and output is verified.
lOMoAR cPSD| 45076656
EX NO : 10 DEFINING AN UNION
DATE :
Aim :
To write a C program to declare and initialise a Union
Algorithm:
STEP 1:Start the program
STEP 2:Define a union MyUnion
STEP 3:Define member variables and union variable
STEP 4:Initialize a value
STEP 5:Print the value
STEP 6:Stop the program
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
union MyUnion myVar;
myVar.intValue = 42;
printf("Value stored in intValue: %d\n", myVar.intValue);
getch();
}
lOMoAR cPSD| 45076656
Result :
Thus C program to declare and initialise Union is written, executed and output is
verified.
lOMoAR cPSD| 45076656
Aim:
To create a file to write and read employee records using the C
program.
Algorithm:
STEP 1: start the program
STEP 2: create a file pointer to open a file
STEP 3: open a file for write access
STEP 4: write the employee data in the file
STEP 5: close the file
STEP 6: open the file for read access
STEP 7: read the contents of the file
STEP 8: print the employee data
STEP 9: close the file
STEP 10: stop the program
Program:
#include<stdio.h>
#include<conio.h>
Void main()
{
FILE *fptr;
int i, n, empno; float bpay, allow, ded;
char name[10];
clrscr();
fptr = fopen("EMPLOYEE.DAT", "w");
printf("Enter the number of employees : ");
scanf("%d", &n); for(i = 0; i < n; i++);
{
printf("\nEnter the employee number : ");
scanf("%d", &empno);
printf("\nEnter the name : ");
scanf("%s", name);
printf("\nEnter the basic pay, allowances & deductions : ");
scanf("%f %f %f", &bpay, &allow, &ded);
fprintf(fptr, "%d %s %f %f %f \n", empno,name,bpay,allow,ded);
}
fclose(fptr);
fptr = fopen("EMPLOYEE.DAT", "r");
printf("\nEmp. No.Name\t\t Bpay\t\t Allow\t\t Ded\t\t Npay\n\n");
for(i = 0; i < n; i++)
lOMoAR cPSD| 45076656
{
fscanf(fptr,"%d%s%f%f%f\n", &empno,name,&bpay,&allow,&ded);
printf("%d \t %s \t %.2f \t %.2f \t %.2f \t %.2f \n", empno, name, bpay, allow,
ded, bpay + allow - ded);
}
fclose(fptr);
getch();
}
Result:
Thus a C program to create a file for writing and reading employee data is
written, executed and output is verified.
lOMoAR cPSD| 45076656
Aim:
To write a C program to read and print contents of a file
Algorithm:
STEP 1: Start the program
STEP 2: create a file pointer to open a file
STEP 3: open a file for read access
STEP 4: read the contents of the file
STEP 5: print the contents of file
STEP 6: close the file
STEP 7: stop the program
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
fp=fopen("Jtp.txt","r");
if(!fp)
{
printf("Error in opening file\n");
return 0;
}
//The file pointer always starts at the beginning of the file.
printf("Position of the pointer : %ld\n",ftell(fp));
char ch;
while(fread(&ch,sizeof(ch),1,fp)==1)
{
//Here, we go through the entire file and print everything we find till we get
to the finish.
printf("%c",ch);
}
printf("Position of the pointer : %ld\n",ftell(fp));
//It will return to its previous location below using the rewind() function.
rewind(fp);
printf("Position of the pointer : %ld\n",ftell(fp));
fclose(fp);
getch();
}
lOMoAR cPSD| 45076656
Result:
Thus a C program to read and print the contents of a file is written,
executed and output is verified
lOMoAR cPSD| 45076656
Aim:
To write a C program to access file contents randomly.
Algorithm:
STEP 1: start the program
STEP 2: create file pointer to open the file
STEP 3: open the file for read access
STEP 4: using fseek() move the pointer through the file contents
STEP 5: print the contents of file
STEP 6: close the file
STEP 7: stop the program
Program:
#include<stdio.h>
#include<conio. h>
void main()
{
FILE *fp;
fp= fopen("jtp.txt","r");
if(!fp)
{
printf("Error: File cannot be opened\n");
return 0;
}
//Move 3 bytes forward, so if we print all the way through, we won't see the
first 6 bytes.
fseek(fp, 8, 0);
char ch;
while(fread(&ch,sizeof(ch),1,fp)==1)
{
//Here, we go through the entire file and print everything up until the end.
printf("%c",ch);
}
fclose(fp);
getch();
}
lOMoAR cPSD| 45076656
Result:
Thus a C program to access file contents randomly is written, executed
and output is verified.
lOMoAR cPSD| 45076656
Aim:
To write a C program to find the area of a rectangle using preprocessor
directives.
Algorithm:
STEP 1: start the program
STEP 2: define preprocessor directive for finding area
STEP 3: read length and breadth
STEP 4: call area function
STEP 5: print area
STEP 6: stop the program
Program:
// C Program to illustrate function like macros
#include <stdio.h>
// macro with parameter
#define AREA(l, b) (l * b)
void main()
{
int l1 = 10, l2 = 5, area;
area = AREA(l1, l2);
printf("Area of rectangle is: %d", area);
getch();
}
lOMoAR cPSD| 45076656
Result:
Thus a C program to find the area of a rectangle using preprocessor
directives is written, executed and output is verified.
lOMoAR cPSD| 45076656
Aim:
To write a C program for implementing Command line arguments.
Algorithm:
STEP 1: start the program
STEP 2: define main function with arguments argument count and argument list
STEP 3: print the argument count
STEP 4: print the argument list
STEP 5: stop the program
Program:
// C program named mainreturn.c to demonstrate the working
// of command line argument
#include <stdio.h>
#include<conio.h>
// defining main with arguments
void main(int argc, char* argv[])
{
int i;
clrscr();
printf("You have entered %d arguments:\n", argc);
for (i = 0; i < argc; i++)
{
printf("%s\n", argv[i]);
}
getch();
}
lOMoAR cPSD| 45076656
Result:
Thus a C program to implement Command line arguments is written,
executed and output is verified.