You are on page 1of 32

Practical File of C language

Program 1: To Print the following pattern of n lines.


* * * * *
* * * *
* * *
* *
*

Code:#include<stdio.h>
#include<conio.h>
void main()
{
int space,i,j,k;
clrscr();
printf("Enter the range: ");
scanf("%d",&j);
for(i=1;i<=5;i++)
{
for(space=1;space<=i;space++)
{
printf(" ");
}
for(k=1;k<=j;k++)
{
printf("* ");
}
printf("\n");
j=j-1;
}
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 1

Practical File of C language


Program 2: To Print the following pattern of n lines.
*
***
*****
*******
*********

Code: #include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,n;
clrscr();
printf("how many lines:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(k=0;k<=2*(i-1);k++)
{
printf("*");
}
printf("\n");
}
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 2

Practical File of C language


Program 3: To search a given number from a given list of numbers entered using binary
search?

Code: #include<stdio.h>
#include<conio.h>
void main()
{
int i,n,loc=-1,a[20],beg,end,mid,item;
clrscr();
printf("\n/* program to search a array element with the help of binary
search*/\n");
printf("\n enter the number of elements");
scanf("%d",&n);
printf("\n enter sorted list");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the item u want to search");
scanf("%d",&item);
beg=0;
end=n-1;
while(beg<end)
{
mid=(beg+end)/2;
if(a[mid]==item)
{
loc=mid;
printf("\n number is at %d loc",loc+1);
break;
}
else if(loc==-1)
printf("\n not found");
}
getch();
}

M.C.A.-Ist Sem. (TYC)

Page 3

Practical File of C language


Output:

M.C.A.-Ist Sem. (TYC)

Page 4

Practical File of C language


Program 4: To sort a given list of numbers entered through a keyboard using bubble sort?
Code: #include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,temp;
int a[20];
clrscr();
printf("\n /*Program to perform bubble sort operation in array*/\n");
printf("\n enter the number of elements");
scanf("%d",&n);
printf("\n enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("\n the sorted elements ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
Output:

M.C.A.-Ist Sem. (TYC)

Page 5

Practical File of C language


Program 5: Program to calculate the electrical bill with given units?
Code: #include<stdio.h>
#include<conio.h>
void main()
{
float units,bill;
clrscr();
printf("\n enter the units");
scanf("%f" , &units);
if(units>0 &&units<=100)
{
bill=units*0.50;
printf("%f",bill);
}
else if(units>100 && units<=200)
{
bill=units*0.80;
printf("%f",bill);
}
else
{
bill=units*1.00;
printf("%f",bill);
}
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 6

Practical File of C language

Program 6: Program to perform the arithmetic operation using switch statement?


Code: # include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c,d,e,f;
charnum,n;
start:
clrscr();
printf("\n enter the value of a & b");
scanf("%d%d", &a,&b);
printf("\n enter the character");
scanf("%s",&c);
switch(c)
{
case('+'):
c=a+b;
printf("\n addition is %d",c);
break;
case('-'):
d=a-b;
printf("\n sub is %d",d);
break;
case('*'):
e=a*b;
printf("\n mul is %d",e);
break;
case('/'):
f=a/b;
printf("\n division is %d",f);
break;
default:
printf("\n wrong value");

M.C.A.-Ist Sem. (TYC)

Page 7

Practical File of C language


}
printf("\n do u want to exist");
scanf("%c",&num);
if(num=='n')
{
goto start;
}
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 8

Practical File of C language


Program 7: Program to check whether a string given by user is Palindrome or not?

Code: #include<stdio.h>
#include<conio.h>
void main()
{
char a[14],b[14];
clrscr();
printf("enter the string first string-->");
gets(a);
strcpy(b,a);
strrev(b);
if (strcmp(a,b)==0)
printf("\n\nstring is pallindrome");
else
printf("\n\nstring is not pallindrome");
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 9

Practical File of C language


Program 8: Program to demonstrate how to continue statement works?
Code: #include<stdio.h>
#include<conio.h>
void main()
{
intnum,i;
printf("Enter how many numbers");
scanf("%d",&num);
for(i=0;i<num;i++)
{
if(i<=5)
continue;
printf("\n %d",i);
}
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 10

Practical File of C language


Program 9: Program to check whether a number given by user is Palindrome or not?
Code: #include<conio.h>
#include<stdio.h>
void main()
{
intnum,rem,rev=0,z;
clrscr();
printf(" \n Enter number to check whether number is polindrome or not: ");
scanf("%d",&num);
z=num;
while(num>0)
{
rem=num%10;
num=num/10;
rev=(rev*10)+rem;
}
if(z==rev)
{
printf("\n Number is polindrome");
}
else
{
printf("\n Number is not polindrome");
}
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 11

Practical File of C language


Program 10: Program to display any number in reverse order?
Code: #include<conio.h>
#include<stdio.h>
void main()
{
intnum,rem,rev=0;
clrscr();
printf(" Enter number which you want to reverse: ");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
num=num/10;
rev=(rev*10)+rem;
}
printf("\n Reversed number is %d",rev);
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 12

Practical File of C language


Program 11: Program to calculate the sum of rows and column in multi-dimensional
array?

Code: #include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],s[10],c[10],i,j,m,n;
clrscr();
printf("\n enter no of rows n columns: ");
scanf("%d%d",&m,&n);
printf("\n Enter elements: ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Elements are: ");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t %4d",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
s[i]=0;
for(j=0;j<n;j++)
{
s[i]+=a[i][j];
}
}
for(i=0;i<m;i++)
{
c[i]=0;
for(j=0;j<n;j++)
{
c[i]+=a[j][i];
}

M.C.A.-Ist Sem. (TYC)

Page 13

Practical File of C language


}
printf("\n sum of rows are: ");
printf("\n");
for(i=0;i<m;i++)
{
printf("%4d\t",s[i]);
}
printf("\n sum of columns are");
printf("\n");
for(i=0;i<n;i++)
{
printf("%4d\t",c[i]);
}
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 14

Practical File of C language


Program 12: Program to count the number of vowels, whitespaces, and digits in a string?
Code: #include<stdio.h>
#include<conio.h>
void main()
{
char string[100];
int vowel=0,i,whitespace=0,character=0,number=0;
clrscr();
printf("\n Enter the String=");
gets(string);
for(i=0;string[i]!='\0';i++)
{
if((string[i]=='a')||(string[i]=='A')||(string[i]=='e')||(string[i]=='E')||
(string[i]=='i')||(string[i]=='I')||(string[i]=='o')||( string[i]=='O')||
( string[i]=='u')||(string[i]=='U'))
{
vowel++;
}
else if(string[i]==' ')
{
whitespace++;
}
else if((string[i]=='0')||(string[i]=='1')||(string[i]=='2')||(string[i]=='3')||
(string[i]=='4')||(string[i]=='5')||(string[i]=='6')||(string[i]=='7')||
(string[i]=='8')||(string[i]=='9')||(string[i]=='0'))
{
number++;
}
character++;
}
printf("\n Number of vowel in string is = %d ",vowel);
printf("\n Number of whitespaces in string is = %d ",whitespace);
printf("\n Number of digits in string is = %d ",number);
printf("\n Length of string = %d ",character);
getch();
}

Output:
M.C.A.-Ist Sem. (TYC)

Page 15

Practical File of C language

M.C.A.-Ist Sem. (TYC)

Page 16

Practical File of C language


Program 13: Program to sort the strings into alphabetical order?
Code: #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10][10],temp[10];
int i,j,n;
clrscr();
printf("enter the no. of students:");
scanf("%d",&n);
printf("enter the names of %d students\n",n);
for(i=0;i<n;i++)
{
fflush(stdin);
gets(name[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\nNames in sorted order are....\n");
for(i=0;i<n;i++)
puts(name[i]);
getch();
}

M.C.A.-Ist Sem. (TYC)

Page 17

Practical File of C language


Output:

M.C.A.-Ist Sem. (TYC)

Page 18

Practical File of C language


Program 14: Program to implement different string manipulation functions using switch
statement?
Code: #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int d,d1,compare;
int choice;
printf("Enter 1st String:");
gets(str1);
printf("Enter 2nd String:");
gets(str2);
printf("\n1st string:->");
puts(str1);
printf("2nd string:->");
puts(str2);
printf("\n\npress 1 for finding length of strings\n");
printf("press 2 for concatinate strings\n");
printf("press 3 for copying 2nd string into 1st string\n");
printf("press 4 for reversing strings\n");
printf("press 5 for converting strings into upper case\n");
printf("press 6 for converting strings into lower case\n");
printf("press 7 for comparing two strings\n");
printf("\n\nENTER YOUR CHOICE:-->");
scanf("%d",&choice);
switch(choice)
{
case 1:
d=strlen(str1);
d1=strlen(str2);
printf("\nlength of 1st string is:%d",d);
printf("\nlength of 2nd string is:%d",d1);
break;
case 2:
strcat(str1,str2);
printf("After concatination string is:");
puts(str1);
break;
case 3:
strcpy(str1,str2);
printf("After copying string is:");

M.C.A.-Ist Sem. (TYC)

Page 19

Practical File of C language


puts(str1);
break;
case 4:
strrev(str1);
strrev(str2);
printf("\nAfter reversing 1st string:->");
puts(str1);
printf("After reversing 2nd string:->");
puts(str2);
break;
case 5:
strupr(str1);
strupr(str2);
printf("\nAfter converting into upper case 1st string:->");
puts(str1);
printf("After converting into upper case 2nd string:->");
puts(str2);
break;
case 6:
strlwr(str1);
strlwr(str2);
printf("\nAfter converting into lower case 1st string:->");
puts(str1);
printf("After converting into lower case 2nd string:->");
puts(str2);
break;
case 7:
compare=strcmp(str1,str2);
if(compare==0)
{
printf("strings are equal");
}
else if(compare==-1)
{
printf("the first string is smaller");
}
else if(compare==1)
{
printf("the second string is smaller");
}
break;
}
getch();
}

M.C.A.-Ist Sem. (TYC)

Page 20

Practical File of C language

Outputs:

M.C.A.-Ist Sem. (TYC)

Page 21

Practical File of C language

M.C.A.-Ist Sem. (TYC)

Page 22

Practical File of C language


Program 15: Program to perform multiplication of two dimensional array?
Code: #include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5];
int i,j,m,n,p,q;
clrscr();
printf("enter row and column size of matrix A");
scanf("%d%d",&m,&n);
printf(" enter row and column size of matrix B");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("matrix can be multiplied \n");
printf("enter matrix a elements \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);}
}
}
printf("enter matrix b element");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]*b[i][j];
}
}
printf("multiplication of A and B matrices \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

M.C.A.-Ist Sem. (TYC)

Page 23

Practical File of C language


printf("%d ",c[i][j]);
}
}
}
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 24

Practical File of C language


Program 16: Program to show that when array is passed to function it is nit copied, but
original array is reffered in that function?

Code: #include<stdio.h>
#include<conio.h>
void modify (int[],int);
void main()
{
int i,n,a[100];
printf("\n Enter the size of array: ");
scanf("%d",&n);
printf("\n Enter the elements of array: \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n In Main function.\n Elements of array: \n");
for(i=0;i<n;i++)
{
//a[i]=i+1;
printf("%d\t",a[i]);
}
modify(a,n);
printf("\n In main fuction after calling function.");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
void modify(int b[],int m)
{
int i;
printf("\n In called function. \n Elements of array: \n");
for(i=0;i<m;i++)
{
b[i]=0;
printf("%d\t",b[i]);
}
return;
}

M.C.A.-Ist Sem. (TYC)

Page 25

Practical File of C language

Output:

M.C.A.-Ist Sem. (TYC)

Page 26

Practical File of C language


Program 17: Program to convert lowercase text to uppercase text?
Code: #include<stdio.h>
#include<conio.h>
void main()
{
char c1,c2;
clrscr();
printf("\n Enter a lowercase character: ");
scanf("%c",&c1);
c2=(c1>='a'&&c1<='z')?('A'+c1-'a'):c1;
printf("\n Upper equivalent is: %c\n",c2);
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 27

Practical File of C language


Program 18: Program to convert binary number to decimal number?
Code: # include<stdio.h>
# include<math.h>
void main()
{
long int binary;
int rem,res=0,count=0;
clrscr();
printf("\n Enter binary number: ");
scanf("%ld",&binary);
while(binary>0)
{
rem=binary%10;
res=res+(rem*pow(2,count));
count++;
binary=binary/10;
}
printf("\n Decimal equivalent number: %d",res);
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 28

Practical File of C language


Program 19: Program to convert a decimal number to a binary number?
Code: #include<stdio.h>
#include<conio.h>
main()
{
long int d,b;
long int i=0,a[20],j;
clrscr();
printf("\n Enter the number : ");
scanf("%ld",&d);
while(d>0)
{
a[i]=d%2;
d=d/2;
i=i+1;
}
printf("\n The binary number: ");
for(j=i-1;j>=0;j--)
printf("%ld",a[j]);
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 29

Practical File of C language


Program 20: Program to count how many numbers are +ve, -ve, and zero?
Code: #include<conio.h>
#include<stdio.h>
main()
{
int i, a[20],n,countp=0;
int countn=0,countz=0;
clrscr();
printf("\n Enter the number of element: ");
scanf("%d",&n);
printf("\n Enter the element: ") ;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]>0)
countp++;
else if(a[i]<0)
countn++;
else
countz++;
}
printf("\n There are %d (+ve)number.",countp);
printf("\n There are %d (-ve)number.",countn);
printf("\n There are %d(zero's).",countz);
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 30

Practical File of C language


Program 21: Program to find the sum of first 100 even numbers?
Code: #include<stdio.h>
#include<conio.h>
void main()
{
int j,i,n,oddsum=0;
int evensum=0;
clrscr();
printf(" Odd numbers upto 100 are: \n");
for(i=1;i<=100;i=i+2)
{
printf("\t %d",i);
oddsum=oddsum+i;
}
printf("\n Sum of odd numbers upto 100 are: %d\n",oddsum);
printf("\n Even numbers upto 100 are : \n");
for(j=0;j<=100;j=j+2)
{
printf("\t %d",j);
evensum=evensum+j;
}
printf("\n Sum of even numbers upto 100 are: %d",evensum);
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 31

Practical File of C language


Program 22: Program to find the greatest of three numbers using nested if statement?
Code: #include<stdio.h>
#include<conio.h>
void main()
{
int Num1,Num2,Num3;
clrscr();
printf(" Enter Any three Number's : ");
scanf("%d %d %d",&Num1,&Num2,&Num3);
if(Num1>Num2 && Num1>Num3)
{
printf(" %d Is Greater.",Num1);
}
else if(Num2>Num1 && Num2>Num3)
{
printf(" %d Is Greater.",Num2);
}
else if(Num3>Num2 && Num3>Num1)
{
printf(" %d Is Greater.",Num3);
}
else
{
printf(" Invalid Input.");
}
getch();
}

Output:

M.C.A.-Ist Sem. (TYC)

Page 32

You might also like