C Manual
C Manual
(Computer Science)
SEMESTER – I
Programming in C Lab Manual
1. Write a program to find the largest two (three) numbers using if and conditional operator.
using if-else-if
#include <stdio.h>
int main()
{
int a, b, c;
printf("Please Enter three different values\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
{
printf("\n%d is Greater Than both %d and %d", a, b, c);
}
else if (b > a && b > c)
{
printf("\n%d is Greater Than both %d and %d", b, a, c);
}
else if (c > a && c > b)
{
printf("\n%d is Greater Than both %d and %d", c, a, b);
}
else
{
printf("\nEither any two values or all the three values are equal");
}
return 0;
}
Output:
Please Enter three different values
34
23
54
54 is Greater Than both 34 and 23
1
2. Write a program to print the reverse of a given number.
#include<stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
return 0;
}
Output:
Enter a number: 123
Reversed Number: 321
3. Write a program to print the prime number from 2 to n where n is given by user
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
printf("Enter the number till which you want prime numbers\n");
scanf("%d",&n);
printf("Prime numbers are:-\n");
for(i=2;i<=n;i++)
{
int c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d ",i);
}
}
}
Output:
Enter the number till which you want prime numbers
20
Prime numbers are:-
2 3 5 7 11 13 17 19
2
4. Write a program to find the roots of a quadratic equation using switch statement.
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,x,d,r1,r2;
int z;
printf ("Enter the values of a, b and c \n");
scanf ("%f %f %f", &a, &b, &c);
x = (b*b) - 4 * a * c;
d = sqrt (fabs(x));
if (d==0)
z=1;
else if (d>0)
z=2;
else
z = 3;
switch (z)
{
case 1 : r1 = - b / 2 * a;
r2 = - b / 2 * a;
printf ("\n The roots are real and equal and the roots are %f %f",r1,r2);
break;
case 2 : r1 = (-b + d) / 2 * a;
r2= (-b - d) / 2 * a;
printf ("The roots are real and unequal and the roots are %f %f ", r1, r2);
break;
case 3 : r1 = (-b + d) / 2 * a;
r2= (-b - d) / 2 * a;
printf ("The roots are imaginary and unequal and the roots are %f %fi ", r1, r2);
break;
}
return 0;
}
Output:
Enter the values of a, b and c
1
6
-7
The roots are real and unequal and the roots are 1.00 and -7.00
5. Write a program to print a triangle of stars as follows (take number of lines from user):
*
***
*****
*******
*********
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,c,s;
printf(“enter number of rows:”);
scanf(“%d”,&n);
3
for(r=1;n>=r;r++)
{
for(s=n;s>=1;s--)
{
printf(“ “);
}
for(c=r;c>=1;c--)
{
printf(“*”);
}
for(c=r;c>1;c--)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}
Output:
Enter number of rows:5
*
***
*****
*******
*********
6. Write a program to find largest and smallest elements in a given list of numbers
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
return 0;
}
Output:
How many elements:5
Enter the Array:1 8 12 4 6
The largest element is 12
The smallest element is 1
4
7. Write a program to find the product of two matrices.
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first 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 number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
Output:
Enter the number of rows and columns of first matrix 3 3
Enter the elements of first matrix
120
011
201
Enter the number of rows and columns of second matrix 3 3
Enter the elements of second matrix
112
211
5
121
Product of entered matrices:-
5 3 4
3 3 2
3 4 5
8. Write a program to find the GCD of two numbers using iteration and recursion.
Using iteration:
#include <stdio.h>
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("GCD = %d",n1);
return 0;
}
Output:
Enter two positive integers: 81
153
GCD = 9
Using recursion:
#include <stdio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
6
9. Write a program to illustrate the use of storage classes.
#include<stdio.h>
#include<conio.h>
void fun1(void);
void fun2(void);
int globvar=10;
void main()
{
printf(“\n storage classes and scope\n”);
globvar=20;
printf(“\n variable globvar,in main()=%d\n”,globvar);
fun1();
printf(“\n variable globvar,in main()=%d\n”,globvar);
fun2();
printf(“\n variable globvar,in main()=%d\n”,globvar);
getch();
}
int globvar2=30;
void fun1(void)
{
char globvar;
globvar=’A’;
globvar2=40;
printf(“\n In fun1(),globvar=%c and globvar2=%d\n”,globvar,globvar2);
}
void fun2(void)
{
double globvar2;
globvar=50;
globvar2=1.234;
printf(“\n In fun2(),globvar=%d and globvar2=%.4f\n”,globvar,globvar2);
}
Output:
storage classes and scope
variable globvar,in main()=20
In fun1(),globvar=A and globvar2=40
variable globvar,in main()=20
In fun2(),globvar=50 and globvar2=1.2340
variable globvar,in main()=50
10. Write a program to demonstrate the call by value and the call by reference concepts
call by value:
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
7
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
call by reference:
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
11. Write a program that prints a table indicating the number of occurrences of each alphabet in the text
entered as command line arguments.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(int argc,char *argv[])
{
char str[20],ch;
int count=0,i;
if(arg!=3)
{
printf(“You must provide arguments <string>,<characters>\n”);
return 1;
8
}
strcpy(str,args[1]);
ch=*argv[2];
for(i=0;str[i]!=’\0’;i++)
{
if(str[i]==ch)
count++;
}
if(count==0)
printf(“character “%c” is not present”,ch);
else
printf(“occurrence of character “%c” in string \”%s \” is:%d”,ch,str,count);
getch();
}
Output:
Occurrence of character ‘n’ in string “international” is:3
#include<stdio.h>
#include<conio.h>
enum week {sun, mon, tue, wed, thu, fri, sat};
void main()
{
enum week today;
today=tue;
printf("%d day",today+1);
getch();
}
Output:
3 day
13. Write a program to demonstrate use of string functions string.h header file.
#include <stdio.h>
#include <string.h>//taking string.h header file which defines most of the library functions.
int main()
{
char name[50],name1[50],name2[50],ch='B';//declaring strings
char *x;//pointer
printf("Enter 1st string: ");
gets(name);//input 1st string
printf("Enter 2nd string: ");
gets(name1);//input second string
printf("strlen() function: %d\n",strlen(name));//prints the length of the name[] string
printf("strcat() function: %s\n",strcat(name,name1));//concatenates the two strings and stores
the result in name[]
strcpy(name2,name);//copying the string in name[] to name2[]
printf("strcpy() function: %s\n",name2);//display the copied string
printf("strcmp() function: %d\n",strcmp(name,name1));//compare the two strings
printf("strlwr() function: %s\n",strlwr(name));//convert the string to lowercase
printf("strupr() function: %s\n",strupr(name));//convert the string to uppercase
9
x=strchr(name,ch);//store the pointer of the character in the variable
printf("strchr()function. The string after ch is: %s\n",x);//display the rest of the string
x=strstr(name,name1);//stores the pointer of the string in the variable
printf("strstr() function: %s",x);//displays the rest of the string.
return 0;
}
Output:-
Enter 1st string: The bell rings
Enter 2nd string: bell
strlen() function: 14
strcat() function: The bell ringsbell
strcpy() function: The bell ringsbell
strcmp() function: -1
strlwr() function: the bell ringsbell
strupr() function: THE BELL RINGSBELL
strchr()function. The string after ch is: BELL RINGSBELL
strstr() function:
14. Write a program that opens a file and counts the number of characters in a file.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int count=0;
FILE *fptr;
clrscr();
fptr=fopen("text.txt","w");
if(fptr==NULL)
{
printf("File can't be created\a");
getch();
exit(0);
}
printf("Enter some text and press enter key:\n");
while((ch=getche())!='\r')
{
fputc(ch,fptr);
}
fclose(fptr);
fptr=fopen("text.txt","r");
printf("\nContents of the File is:");
while((ch=fgetc(fptr))!=EOF)
{
count++;
printf("%c",ch);
}
fclose(fptr);
printf("\nThe number of characters present in file is: %d",count);
getch();
}
10
Output:
Enter some text and press enter key:
Hello world
Contents of the File is: Hello world
The number of characters present in file is:11
15. Write a program to create a structure Student containing fields for Roll No., Name, Class, Year and
Total Marks. Create 10 students and store them in a file.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int rollno;
char sclass[10];
int year;
int totalmarks;
};
void main()
{
struct student s1[10],s2[10];
int i;
FILE *fptr=fopen("student.txt","wb");
clrscr();
for(i=0;i<10;i++)
{
fflush(stdin);
printf("\n Enter name: ");
scanf("%s",s1[i].name);
printf("\n Enter roll no.: ");
scanf("%d",&s1[i].rollno);
printf(“enter class:”);
scanf(“%s”,s1[i].sclass);
printf(“enter year:”);
scanf("%d",&s1[i].year);
printf(“enter total marks:”);
scanf(“%d”,&s1[i].totalmarks);
}
fwrite(s1,sizeof(s1),1,fptr);
fclose(fptr);
fptr=fopen(“file.txt”,”rb”);
fread(s2,sizeof(s2),1,fptr);
for(i=0;i<10;i++)
{
printf("\n Name: %s\n Roll no.: %d\n class: %s\n Year: %d\n Totalmarks:
%d\n",s2[i].name,s2[i].rollno,s2[i].sclass,s2[i].year,s2[i].totalmarks);
}
fclose(fptr);
getch();
11
Output:
Enter name: ABC
Enter roll no.: 101
enter class: first
enter year:2008
enter total marks:405
Enter name: XYZ
Enter roll no.: 102
enter class: second
enter year:2009
enter total marks:415
.
.
.
.
Name: ABC
Roll no.: 101
class: first
Year:2008
Totalmarks:405
Name: XYZ
Roll no.: 102
class: second
Year:2009
Totalmarks:415
.
.
16. Write a program that opens an existing text file and copies it to a new text file with all lowercase
letters changed to capital letters and all other characters unchanged.
#include<stdio.h>
#include<conio.h>
void main()
{
char oldfile[80],newfile[80];
FILE *fp,*fp1;
int c;
puts(“enter old file:”);
gets(oldfile);
puts(“enter new file:”);
gets(newfile);
if((fp=fopen(oldfile,”rb”))==NULL)
{
puts(“Error opening old file”);
exit(1);
}
if((fp=fopen(newfile,”wb”))==NULL)
{
puts(“Error opening new file”);
fclose(fp);
exit(1);
}
while(!feof(fp))
12
{
c=getc(fp);
if((97 <= c) && (c <= 122))
fputc((c-32),fp1);
else
fputc(c,fp1);
}
fclose(fp);
fclose(fp1);
getch();
}
Output:
enter old file:( Enter old.txt file having some text in small case)
enter new file: (enter new.txt file)
/*After program execution the old.txt file text will be converted to upper case and stored in
new.txt file*/
13