C Prog Lab
C Prog Lab
1
Output of the program is:
2
PROGRAM – 2
/* Write a program in C to PRINT TEN
numbers. */
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
getch ();
}
3
Output of the program is:
4
PROGRAM – 3
/* Write a program in C to PRINT Matrix by
using nested for loops. */
#include<stdio.h>
#include<conio.h>
#include <stdio.h>
int main()
{
for (int i=0; i<2; i++)
{
for (int j=0; j<4; j++)
{S
printf("%d, %d\n" ,i ,j);
}
}
getch();
}
5
Output of the program is:
6
Program 4
/* Write a program in C to swapping using
“call by value method”. */
#include<stdio.h>
#include<conio.h>
void swap(int a,int b);
void main()
{
int a,b;
clrscr();
printf("\n\n\t\t Enter I number : ");
scanf("%d",&a);
printf("\n\n\t\t Enter II number : ");
scanf("%d",&b);
swap(a,b);
printf("\n\n\t\t After function call : \n");
printf("\n\n\t\t a= %d\tb= %d",a,b);
getch();
}
void swap(int a,int b)
{
7
int c=0;
c=a;
a=b;
b=c;
printf("\n\n\t\t Between function: \n");
printf("\n\n\t\t a= %d\tb= %d",a,b);
}
8
Output of the program is:
9
Program 5
/* Write a program in C to swapping using
“call by reference method”. */
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b);
void main()
{
int a,b;
clrscr();
printf("\n\n\t\t Enter I number : ");
scanf("%d",&a);
printf("\n\n\t\t Enter II number : ");
scanf("%d",&b);
swap(&a,&b);
printf("\n\n\t\t After function call : \n");
printf("\n\t\t a= %d\tb= %d",a,b);
getch();
}
void swap(int *a,int *b)
{
10
int c=0;
c=*a;
*a=*b;
*b=c;
printf("\n\n\t\t Between function: \n");
printf("\n\t\t a= %d\tb= %d",*a,*b);
}
11
Output of the program is:
12
PROGRAM – 6
/* Write a program to multiply two
matrices.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k;
clrscr();
printf("\n\n\t\t Enter the details of Ist Matrix : ");
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf("\n\n\t\t Enter ( %d, %d ) element : ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n\n\t\t Enter the details of IInd Matrix : ");
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf("\n\n\t\t Enter ( %d, %d ) element : ",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
13
c[i][j]=0;
for(k=1;k<=2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n\n\t\t Ist Matrix is : \n\n");
for(i=1;i<=2;i++)
{
printf("\t\t\t");
for(j=1;j<=2;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n\n\t\t IInd Matrix is : \n\n");
for(i=1;i<=2;i++)
{
printf("\t\t\t");
for(j=1;j<=2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n\n\t\t Multiplied Matrix is : \n\n");
for(i=1;i<=2;i++)
{
printf("\t\t\t");
for(j=1;j<=2;j++)
{
printf("%d\t",c[i][j]);
14
}
printf("\n");
}
getch();
}
15
Output of the program is:
16
PROGRAM – 7
/* Write a program to check that the input
string is a palindrome or not.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20],str1[20];
clrscr();
printf("\n\n\t\t Enter a string : ");
scanf("%s",&str);
strcpy(str1,str);
strrev(str1);
if(strcmp(str,str1)==0)
printf("\n\n\t\t String is Palindrome " );
else
printf("\n\n\t\t String is NOT Palindrome " );
getch();
}
17
Output of the program is:
18
PROGRAM – 8
/* Write a program to concatenate two
strings of different lengths.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[40],str1[40];
clrscr();
printf("\n\n\t\t Enter first String : ");
scanf("%s",&str);
printf("\n\n\t\t Enter second String : ");
scanf("%s",&str1);
printf("\n\n\t\t Concatenated String is : %s ",strcat(str,str1));
getch();
}
19
Output of the program is:
20
Program 9
/* Write a program in C to perform the
function of calculator using SWITCH
statement*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void add();
void sub();
void mul();
void divide();
void main()
{
int op;
char ans;
do
{
clrscr();
printf("\n\n\t -------------------------------------------\n");
printf("\n\n\t\t CALCULATOR");
printf("\n\n\t -------------------------------------------\n");
printf("\n\n\t 1. ADDITION");
21
printf("\n\n\t 2. SUBSTRACTION");
printf("\n\n\t 3. MULTIPLICATION");
printf("\n\n\t 4. DIVISION");
printf("\n\n\t 5. EXIT");
printf("\n\n\t -------------------------------------------\n");
printf("\n\n\t Enter your choice : ");
scanf("%d",&op);
switch(op)
{
case 1:
clrscr();
add();
break;
case 2:
clrscr();
sub();
break;
case 3:
clrscr();
mul();
break;
case 4:
22
clrscr();
divide();
break;
case 5:
exit(0);
default:
printf("\n\n\t Wrong choice!!!!!!!!");
}
printf("\n\n\t Do U want to cont......");
fflush(stdin);
scanf("%c",&ans);
}
while(ans=='y'||ans=='Y');
getch();
}
void add()
{
int a,b,c;
printf("\n\n\t
*********************************************");
printf("\n\n\t --------------ADDITION--------------");
printf("\n\n\t
*********************************************");
printf("\n\n\t Enter I number : ");
23
scanf("%d",&a);
printf("\n\n\t Enter II number : ");
scanf("%d",&b);
c=a+b;
printf("\n\n\t %d + %d = %d",a,b,c);
}
void sub()
{
int a,b,c;
printf("\n\n\t
*********************************************");
printf("\n\n\t --------------SUBSTRACTION--------------");
printf("\n\n\t
*********************************************");
printf("\n\n\t Enter I number : ");
scanf("%d",&a);
printf("\n\n\t Enter II number : ");
scanf("%d",&b);
c=a-b;
printf("\n\n\t %d - %d = %d",a,b,c);
}
void mul()
{
int a,b,c;
24
printf("\n\n\t
*********************************************");
printf("\n\n\t --------------MULTIPLICATION--------------");
printf("\n\n\t
*********************************************");
printf("\n\n\t Enter I number : ");
scanf("%d",&a);
printf("\n\n\t Enter II number : ");
scanf("%d",&b);
c=a*b;
printf("\n\n\t %d * %d = %d",a,b,c);
}
void divide()
{
int a,b,c;
printf("\n\n\t
*********************************************");
printf("\n\n\t --------------DIVISION--------------");
printf("\n\n\t
*********************************************");
printf("\n\n\t Enter I number : ");
scanf("%d",&a);
printf("\n\n\t Enter II number : ");
scanf("%d",&b);
25
if(b==0)
{
printf("\n\n\t Division is not possible.");
getch();
exit(1);
}
c=a/b;
printf("\n\n\t %d + %d = %d",a,b,c);
}
26
Output of the program is:
27