GDC MEMORIAL COLLEGE
BAHAL (BHIWANI)-127028
Lab Manual
PROGRAMMING IN C (B.Sc.(cs) 1st year)
Department of Computer Science
B.SC.(C. SC.) SEM II
LIST OF PROGRAMS
Program 1: Temperature of a city in Fahrenheit degrees is input through keyboard. Write a
program to convert this temperature into Centigrade degrees.
Program 2:WAP to calculate Simple Interest and Compound Interest.
Program 3: WAP to find largest number among three numbers.
Program 4: WAP to count number of digits, reverse the digit and sum of digits of a given
number.
Program 5:WAP to find factorial of a number.
Program 6: WAP to check whether a given number is prime or not.
Program 7: WAP to check whether a given year is leap year or not.
Program 8: WAP to search for a given number in an array.
Program 9: WAP to find the smallest and largest element in an array.
Program 10: WAP to add and multiply two matrices.
Program 11: WAP to swap two numbers using call by value and call by reference.
Program 12: WAP to count number of words, characters and blanks in a string.
Program 13: WAP to illustrate the usage of structure in processing of student record.
Program 14: WAP to illustrate encryption/decryption of a string.
Program 15: WAP to illustrate pointer arithmetic.
/* Program #1: Temperature of a city in Fahrenheit degrees is input through keyboard.
Write a program to convert this temperature into Centigrade degrees. */
#include<stdio.h>
#include<conio.h>
void main()
float fr,cs;
clrscr();
printf("\n Enter the temperature in fahrenheite: ");
scanf("%f",&fr);
cs=(fr-32)*1.8;
printf("\n Temperature in centigrade:%f",cs);
getch();
/* Program #2:WAP to calculate Simple Interest and Compound Interest.*/
#include<math.h>
#include<stdio.h>
#include<conio.h>
float s_i(int,int,float);
float c_i(int,int,float);
void main()
int pa,t;
float r,si=0,ci;
clrscr();
printf("\n Enter the principal amount, rate and time: ");
scanf("\n %d%f%d",&pa,&r,&t);
si=pa*r*t/100;
si=s_i(pa,t,r);
printf("\n Simple interest:%f",si);
ci=c_i(pa,t,r);
ci=ci-pa;
printf("\n Compound interest:%f",ci);
getch();
float s_i(int p,int t,float r)
float temp;
temp=(p*r*t)/100;
return temp;
float c_i(int p,int t,float r)
float temp;
temp=p*pow((1+r/100),t);
return temp;
/*Program #3: WAP to find largest number among three numbers. */
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c,largest;
clrscr();
printf("\n Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
largest=a;
else
largest=c;
else
if(b>c)
largest=b;
else
largest=c;
printf("\n Largest among three numbers is :%d",largest);
getch();
/* Program #4: WAP to count number of digits, reverse the digit and sum of digits of a
given number. */
#include<stdio.h>
#include<conio.h>
int n_o_d(int);
int rev_no(int);
int s_o_d(int);
void main ()
int number,nod,sod,rn;
clrscr();
printf("\n Enter number: ");
scanf("%d",&number);
nod=n_o_d(number);
printf("\n Number of digits in given number is :%d",nod);
sod=s_o_d (number);
printf("\n Sum of digits of given number is :%d",sod);
rn=rev_no(number);
printf("\n Reverse number is %d",rn);
getch();
int n_o_d (int n)
int count=0;
while(n!=0)
n=n/10;
count++;
}
return(count);
int rev_no(int n)
int rev=0,temp;
while(n!=0)
temp=n%10;
rev=rev*10+temp;
n=n/10;
return(rev);
int s_o_d(int n)
int sum=0, temp;
while(n!=0)
temp=n%10;
sum=sum+temp;
n=n/10;
return(sum);
/* Program #5:WAP to find factorial of a number. */
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
int f,n;
clrscr();
printf("\n Enter a number:");
scanf("%d",&n);
f=factorial(n);
printf("\n Factorial of given no. %d is %d",n,f);
getch();
int factorial (int m)
if(m==1)
return 1;
return(m*factorial(m-1));
/* Program #6: WAP to check whether a given number is prime or not. */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
int div,n;
clrscr();
printf("\n Enter the number:");
scanf("%d",&n);
div=2;
while(div<n/2)
if(n%div==0)
printf("\n %d is not prime",n);
goto end;
//break;
div++;
printf("\n %d is prime",n);
end:
getch();
/* Program #7: WAP to check whether a given year is leap year or not.*/
#include<stdio.h>
#include<conio.h>
void main()
int yr;
clrscr();
printf("\n Enter the year:");
scanf("%d",& yr);
if(yr%100==0)
if(yr%400==0)
printf("Given year is leap year");
else
printf("Given year is not a leap year ");
else
if(yr%4==0)
printf("Given year is leap year ");
getch();
/* Program #8: WAP to search for a given number in an array.*/
#include<conio.h>
#include<stdio.h>
void main()
int a[30],no,i,n;
clrscr();
printf("\n Enter the number of elements in the array:");
scanf("%d",&n);
printf("\n Enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element to be searched");
scanf("%d",&no);
for(i=0;i<n;i++)
if(a[i]==no)
printf("\n Element found at location %d",i+1);
goto end;
else
printf("\n Element not found");
goto end;
end:
getch();
/* Program #9: WAP to find the smallest and largest element in an array. */
#include<stdio.h>
#include<conio.h>
void main()
int a[30],i,min,max,n;
clrscr();
printf("\n Enter the no. of elements in the array:");
scanf("%d",&n);
printf("\n Enter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
min=a[0];
max=a[0];
for(i=0;i<n;i++)
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
printf("\n Largest element in array is: %d\n",max);
printf("\n Smallest elements in array: %d",min);
getch();
/* Program #10: WAP to add and multiply two matrices. */
#include<stdio.h>
#include<conio.h>
void main()
int i,j,m,n,p,q,k;
float a[10][10],b[10][10],c[10][10];
clrscr();
printf("\n Enter the order of first matrix:");
scanf("%d%d",&m,&n);
printf("\n Enter the order of second matrix:");
scanf("%d%d",&p,&q);
if(n!=p)
printf("\n Matrix multiplication is not possible");
goto end;
printf("\n Enter the elements of first matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
printf("\n Enter the elements of second matrix: ");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%f",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
printf("\n Resultant matrix is\n ");
for(i=0;i<m;i++)
for(j=0;j<q;j++)
printf("%f\t",c[i][j]);
printf("\n");
end:
getch();
/* Program #11: WAP to swap two numbers using call by value and call by reference.*/
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void swap_ref(int*,int*);
void main()
int a,b;
clrscr();
printf("\n Enter the numbers:");
scanf("%d%d",&a,&b);
printf("\n Swapping of numbers using call by values");
swap(a,b);
printf("\n Swapping of numbers using call by reference");
printf("\n Values before swap:%d\t%d",a,b);
swap_ref(&a,&b);
printf("\n Values after swap:%d\t%d",a,b);
getch();
void swap(int x,int y)
int temp;
printf("\n Values before swap:%d\t%d",x,y);
temp=x;
x=y;
y=temp;
printf("\n Values after swap:%d\t%d",x,y);
void swap_ref(int*x,int*y)
int temp;
temp=*x;
*x=*y;
*y=temp;
/* Program #12: WAP to count number of words, characters and blanks in a string.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char string[100];
int c,noc=0,noblanks=0,i,len=0,nowords=0;
clrscr();
printf("\n Enter text :");
gets(string);
puts(string);
len=strlen(string);
for(i=0;i<len;i++)
noc++;
for(i=0;i<len;i++)
if(string[i]=='\t'||string[i]==' '||string[i]=='\n')
noblanks++;
for(i=0;i<len;i++)
if(string[i]==' ')
nowords++;
noc=noc-noblanks;
printf("\nNo. of characters %d", noc);
printf("\n No. of blanks: %d",noblanks);
printf("\nNo. of words:%d",nowords+1);
getch();
/* Program #13: WAP to illustrate the usage of structure in processing of student record.*/
#include<stdio.h>
#include<conio.h>
void main()
struct student
int roll_no;
char name[30];
float marks;
};
struct student s;
clrscr();
printf("\n Enter the name, roll no., marks of student: ");
scanf("%s%d%f",&s.name,&s.roll_no,&s.marks);
printf("\n The details of student :");
printf("\n Name:%s",s.name);
printf("\n Roll No.:%d",s.roll_no);
printf("\n Marks:%f",s.marks);
getch();
}
/* Program #14: WAP to illustrate encryption/decryption of a string.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void encryption (char string[]);
void decryption(char string[]);
void main()
char string[30];
clrscr();
printf("Enter the text:");
gets(string);
encryption(string);
getch();
void encryption(char*string)
int i=0;
while(string[i]!='\0')
if(string[i]==' ')
goto jump1;
else if(string[i] >=97)
string[i]=(string[i]-94)%26+97;
else
string[i]=(string[i]-62)%26+65;
jump1:
i++;
printf("The encrypted text is:");
puts(String);
decryption(string);
void decryption(char*string)
int i=0;
while(string[i]!='\0')
if(string[i]==' ')
goto jump2;
else if((string[i] >= 68 && string[i] <= 90) || \ (string[i] >= 100 && string[i] <= 122))
string[i]-=3;
else
string[i]+=23;
jump2:
i++;
printf("The decrypted text is:");
puts(string);
/* Program #15: WAP to illustrate pointer arithmetic.*/
#include<stdio.h>
#include<conio.h>
void main()
int i,*x;
float j,*y;
char k='a',*z;
clrscr();
printf("\n Enter the values of i, j: ");
scanf("%d%f",&i,&j);
printf("\n Value of i=%d",i);
printf("\n Value of j=%f",j);
printf("\n Value of k=%c",k);
x=&i;
y=&j;
z=&k;
printf("\n Original address in x=%u",x);
printf("\n Original address in y=%u",y);
printf("\n Original address in z=%u",z);
x++;
y++;
z++;
printf(“\n After incrementing x, y, z:\n”)
printf("\n New address in x=%u",x);
printf("\n New address in y=%u",y);
printf("\n New address in z=%u",z);
x=x-7;
y=y-5;
z=z-2;
printf(“\n After subtracting some number:\n”)
printf("\n New address in x=%u",x);
printf("\n New address in y=%u",y);
printf("\n New address in z=%u",z);
getch();