You are on page 1of 21

C-Lab manual I sem B.

Sc

PART A

1. Program to convert upper case into lower case and vice


versa

//program to convert the characters from upper to lower and vice


versa

#include<stdio.h>
#include<string.h>

main()
{
int i;
char str[30];
clrscr();
printf("enter the string\n");
gets(str);
printf("the given string is %s\n",str);

for(i=0;str[i]!='\0';i++)
{
if(islower(str[i]))
str[i]=toupper(str[i]);
else
str[i]=tolower(str[i]);
}

printf("converted string is %s\n",str);


getch();
}

Output

Dept. of Computer Science SSCASCW,Tumkur Page 1


C-Lab manual I sem B.Sc

2. Program to count the occurrences of character in a string

//program to search and count the character


#include<stdio.h>
#include<string.h>
main()
{
char str[80],ch;
int count=0,i,len;
clrscr();
printf("enter a string\n");
gets(str);
printf("enter a searching character\n");
scanf("%c",&ch);
len=strlen(str);

for(i=0;i<=len;i++)
{
if(str[i]==ch)
count++;
}
if(count>0)
printf("number of searching character=%d\n",count);
else
printf("does not exists");
getch();
}

Output:

Dept. of Computer Science SSCASCW,Tumkur Page 2


C-Lab manual I sem B.Sc

3. Program to print the right most digit in a number

//to print right most digit Output:

#include<stdio.h>
void main()
{
int n,i;
clrscr();
printf("enter n\n");
scanf("%d",&n);

i=n%10;
switch(i)
{
case 0:printf("right most digit is zero\n");
break;
case 1:printf("0ne\n");
break;
case 2:printf("two\n");
break;
case 3:printf("three\n");
break;
case 4:printf("four\n");
break;
case 5:printf("five\n");
break;
case 6:printf("six\n");
break;
case 7:printf("seven\n");
break;
case 8:printf("eight\n");
break;
case 9:printf("nine\n");
break;
}

getch();
}

Dept. of Computer Science SSCASCW,Tumkur Page 3


C-Lab manual I sem B.Sc

4. Program to count the number of numerals, uppercase,


lowercase and special character in a given string.

//program to count uppercase, lowercase, digits and special characters

#include <stdio.h>
#include<string.h>
Output:
void main()
{
char str[40];
int i,uc=0,lc=0,nc=0,oc=0,len;
clrscr();
printf("enter a string\n");
gets(str);
len=strlen(str);

for(i=0;i<len;i++)
{
if(isupper(str[i]))
uc++;
else if(islower(str[i]))
lc++;
else if(isdigit(str[i]))
nc++;
else
oc++;
}

printf("number of upper case=%d\n",uc);


printf("number of lower case=%d\n",lc);
printf("number of numerals =%d\n",nc);
printf("number of other characters=%d\n",oc);

getch();
}

Dept. of Computer Science SSCASCW,Tumkur Page 4


C-Lab manual I sem B.Sc

5. Program to check whether a given string is palindrome or


not

//to check whether a given string is palindrome or not

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf("enter a string\n");
gets(str1);
strcpy(str2,str1);
strrev(str2);

if(strcmp(str1,str2)==0)
printf("the given string is palindrome\n");
else
printf("the given string is not a palindrome\n");

getch();
}

Output:

Dept. of Computer Science SSCASCW,Tumkur Page 5


C-Lab manual I sem B.Sc

6. Program to find the value of sin(x)


#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{ Output:
float x;
float i,j,k,n,f,l;
float r,sum=0;
clrscr();

printf("enter the value of x in degrees");


scanf("%f",&x);
printf("enter the number of terms");
scanf("%f",&n);

r=M_PI/180.0*x;
sum=0;
k=1;
j=1;

for(i=1;i<=n;i++)
{
f=1;
for(l=1;l<=j;l++)
f=f*l;
sum=sum+pow(r,j)/f*k;
k=-k;
j=j+2;
}

printf("sine(%f)=%f",x,sum);
getch();

Dept. of Computer Science SSCASCW,Tumkur Page 6


C-Lab manual I sem B.Sc

7. Program to find the value of cos(x)

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
float x;
float i,j,k,n,f,l;
float r,sum=0;
clrscr();
printf("enter the value of x in degrees");
scanf("%f",&x);
printf("enter the number of terms");
scanf("%f",&n);
Output:
r=M_PI/180.0*x;
sum=1;
k=-1;
j=2;

for(i=2;i<=n;i++)
{
f=1;
for(l=1;l<=j;l++)
f=f*l;
sum=sum+pow(r,j)/f*k;
k=-k;
j=j+2;
}
printf("cosine(%f)=%f",i,sum);
getch();

Dept. of Computer Science SSCASCW,Tumkur Page 7


C-Lab manual I sem B.Sc

8. Program to find the transpose of the matrix


// to find the transpose of a matrix

#include<stdio.h>

void main()
{
int i,j,a[20][20],m,n;
clrscr();
printf("enter the order of matrix\n");
scanf("%d%d",&m,&n);
printf("enter the matrix elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
Output
printf("the elements of matrix are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);

printf("\n");
}

printf("transpose of a matrix are\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[j][i]);

printf("\n");
}
getch();
}

Dept. of Computer Science SSCASCW,Tumkur Page 8


C-Lab manual I sem B.Sc

9. Program to check whether a matrix is identity matrix or


not
//program to find whether a given matrix is identity or not

#include<stdio.h>
#include<conio.h>
Output
void main()
{
int i,j,m,n,a[20][20],flag,identity;
clrscr();
printf("enter the matrix order\n");
scanf("%d%d",&m,&n);
printf("enter the matrix elemts\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(i==j)
{
if(a[i][j]!=1)
{
flag=0;
break;
}
}
else if(a[i][j]!=0)
{
flag=1;
break;
}

Dept. of Computer Science SSCASCW,Tumkur Page 9


C-Lab manual I sem B.Sc

if(flag==1)
printf("the given matrix is identity\n");
else
printf("the given matrix is not identity\n");
getch();
}

10. Program to find the product of 2 matrix


//product of 2 matrix

#include<stdio.h>
void main()
{
int i,j,k,a[10][10],b[10][10],c[10][10],m,n,p,q;
clrscr();
printf("enter the order of matrix A\n");
scanf("%d%d",&m,&n);
printf("enter the elements of matrix A\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("enter the order of matrix B\n");


scanf("%d%d",&p,&q);
printf("enter the elements of matrix B\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);

for(i=0;i<m;i++) Output

for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("multiplied matrix is\n");
for(i=0;i<p;i++)

Dept. of Computer Science SSCASCW,Tumkur Page 10


C-Lab manual I sem B.Sc

{
for(j=0;j<n;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}

1. Program to arrange numbers in ascending order

//to arrange numbers in ascending order

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,a[20],n;
clrscr();
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the array elements\n"); Output
for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

printf("the sorted array elements are\n");


for(i=0;i<n;i++)
printf("%d\n",a[i]);

getch();
}

Dept. of Computer Science SSCASCW,Tumkur Page 11


C-Lab manual I sem B.Sc

2. Program to arrange names in ascending order

//to sort the names in alphabetical order

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[80][50],temp[50];
int i,j,n;
clrscr();
printf("enter the number of names to be to entered\n");
scanf("%d",&n);
printf("enter the names\n");
for(i=0;i<n;i++)
scanf("%s",str[i]); Output

for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(str[i],str[j])>=0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
printf("sorted names are\n");
for(i=0;i<n;i++)
printf("%s\n",str[i]);

getch();
}

Dept. of Computer Science SSCASCW,Tumkur Page 12


C-Lab manual I sem B.Sc

3. Program to display the first n terms of Fibonacci series


using recursive functions

//to generate Fibonacci series using recursive functions

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("enter the number fo terms\n");
scanf("%d",&n); Output

printf("the fibonacci series is\n");


for(i=1;i<n;i++)
printf("%d\n",fib(i));
getch();
}

fib(int i)
{
if(i==1)
return(0);
else if(i==2)
return(1);
else
return(fib(i-1)+fib(i-2));
}

Dept. of Computer Science SSCASCW,Tumkur Page 13


C-Lab manual I sem B.Sc

4. Program to find the roots of a quadratic equation using


MACRO

//To find the roots: Quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>

# define SQR(x) (x*x)

void main()
{
int a,b,c;
float r1,r2,d;
clrscr();
printf("enter a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
d=SQR(b)-(4*a*c);

if(d>0)
{
printf("the roots are realand distinct\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("root1=%f\n",r1);
printf("root2=%f\n",r2);
}
else if(d==0)

Dept. of Computer Science SSCASCW,Tumkur Page 14


C-Lab manual I sem B.Sc

{
printf("roots are equal\n");
r1=-b/(2*a);
r2=-b/(2*a);
printf("root1=%f\n",r1);
printf("root2=%f\n",r2);
}
else
printf("roots are imaginary");
getch();
}

Output

Dept. of Computer Science SSCASCW,Tumkur Page 15


C-Lab manual I sem B.Sc

5. Program to demonstrate the use of structure

//demo of structure(to display the information of a student using


structure

#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int id;
char name[20];
char address[40];
float per;
char course[6];
}std;

clrscr();
printf("STUDENT INFORMATION\n");
printf("enter student id\n");
scanf("%d",&std.id);
printf("enter the name\n");
scanf("%s",std.name);
printf("enter the student address\n");
scanf("%s",std.address);
printf("enter the percentage of a student\n");
scanf("%f",&std.per);
printf("enter the course\n");

Dept. of Computer Science SSCASCW,Tumkur Page 16


C-Lab manual I sem B.Sc

scanf("%s",std.course);

printf("student is=%d\n",std.id);
printf("student name is=%s\n",std.name);
printf("student address is=%s\n",std.address);
printf("student percentage is=%f\n",std.per);
printf("student course is=%s\n",std.course);
getch();
}

Output

Dept. of Computer Science SSCASCW,Tumkur Page 17


C-Lab manual I sem B.Sc

6. Program to demonstrate the use of union

//demo of union(to display the information of a student using


structure

#include<stdio.h>
#include<conio.h>
void main()
{
union student
{
int id;
char name[20];
char address[40];
float per;
char course[6];
}std;

clrscr();
printf("STUDENT INFORMATION\n");
printf("enter student id\n");
scanf("%d",&std.id);
printf("student is=%d\n",std.id);

printf("enter the name\n");


scanf("%s",std.name);
printf("student name is=%s\n",std.name);

printf("enter the student address\n");

Dept. of Computer Science SSCASCW,Tumkur Page 18


C-Lab manual I sem B.Sc

scanf("%s",std.address);
printf("student address is=%s\n",std.address);

printf("enter the percentage of a student\n");


scanf("%f",&std.per);
printf("student percentage is=%f\n",std.per);

printf("enter the course\n");


scanf("%s",std.course);
printf("student course is=%s\n",std.course);
getch();
}
Output

Dept. of Computer Science SSCASCW,Tumkur Page 19


C-Lab manual I sem B.Sc

7. Program to show the difference between cal by value and


call by reference

//call by value and reference

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("CALL BY VALUE\n");
printf("enter the value of a and b\n");
scanf("%d%d",&a,&b);
printf("before swapping a and b are\t%d\t%d\n",a,b);
swap(a,b);
printf("after swapping a and b are\t %d \t%d\n",a,b);

printf("CALL BY REFERENCE\n");
printf("before swapping a and b are\t %d\t %d\n",a,b);
swap1(&a,&b);
printf("after swapping a and b are\t %d\t %d\n",a,b);
getch();
}

swap(int x, int y)
{
int temp;
temp=x;
x=y;

Dept. of Computer Science SSCASCW,Tumkur Page 20


C-Lab manual I sem B.Sc

y=temp;
}

swap1(int *x,int *y)


{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Output

Dept. of Computer Science SSCASCW,Tumkur Page 21

You might also like