Ug - Cs - Sem1 - Programmingin C Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Ex:no:11(a) CALL BY VALUE

**********************************************************************
#include<stdio.h>
#include<conio.h>
void interchange(intnumber1,int number2)
{
int temp;
temp=number1;
number1=number2;
number2=temp;
printf("After interchange");
printf("\nnumber1=%d",number1);
printf("\nnumber2=%d",number2);
}
void main()
{
int num1=50,num2=70;
clrscr();
printf("**************************************\n");
printf( "call by value\n");
printf("**************************************\n");
printf("Before interchange\n");
printf("\nnumber1=%d",num1);
printf("\nnumber2=%d",num2);
interchange(num1,num2);
getch();
}
OUTPUT:
Ex:no:11(b) CALL BY REFERENCE

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

#include<stdio.h>
#include<conio.h>
void interchange(int *num1,int *num2)
{
int temp;
temp=*num1;
*num1=*num2;
*num2=temp;
printf("After interchange");
printf("\nnumber1=%d",*num1);
printf("\nnumber2=%d",*num2);
}
void main()
{
int num1=50,num2=70;
clrscr();
printf("**************************************\n");
printf("call by value and call by reference\n");
printf("**************************************\n");
interchange(&num1,&num2);
printf("Before interchange");
printf("\nnumber1=%d",num1);
printf("\nnumber2=%d",num2);
getch();
}
Ex:no:5 TO DISPLAY FIRST TEN NATURAL NUMBERS
**********************************************************************

#include<stdio.h>
#include<conio.h>
void main()
{
inti,sum=0;
clrscr();
printf("*************************************\n");
printf("TO DISPLAY FIRST TEN NATURAL NUMBERS\n");
printf("**************************************\n");
for(i=1;i<=10;i++)
{
printf("no is =%d\n",i);
sum=sum+i;
}
printf("FIRST TEN NATURAL NUBERS ARE:\n");
printf("sum=%d",sum);
getch();
}
OUTPUT
**********************************************************************
Ex:no:12 ADD TWO NUMBERS USING POINTERS
**********************************************************************

#include<stdio.h>
#include<conio.h>
void main()
{
int *p1,*p2,a,b,add;
clrscr();
printf("enter any 2 number to add:");
scanf("%d%d",&a,&b);
p1=&a;
p2=&b;
add=*p1+*p2;
printf("\n addition=%d",add);
getch();
}
OUTPUT:
**********************************************************************
Ex:no 10 FACTORIAL USING RECURSION
**********************************************************************

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
longintfa;
longint fact(long int);
clrscr();
printf("*********************************\n");
printf(" factorial using recursion");
printf("\n*******************************\n");
printf("\n enter the value for n");
scanf("%d",&n);
fa=fact(n);
printf("\n factorial=%d",fa);
getch();
}
longint fact(long int m)
{
if(m==0)
return 0;
if(m==1)
return 1;
else
return(m*fact(m-1));
}
OUTPUT:
**********************************************************************
Ex:no:7 MAXIMUM NUMBER IN ARRAY USING POINTERS

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,max;
int *p;
clrscr();
printf("enter the size of array");
scanf("%d",&n);
printf("enter the elements in array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("elements in the array are");
for(i=0;i<n;i++)
printf("%5d",a[i]);
p=&a[0];
max=a[0];
for(i=0;i<n;i++)
{
if(max<=*p)
max=*p;
p++;
}
printf("maximum element in the array is=%d",max);
getch();
}
OUTPUT:
**********************************************************************
Ex:no:1 CELSIUS TO FAHRENHEIT

#include<stdio.h>
#include<conio.h>
void main()
{
floatcelsius,fahrenheit;
clrscr();
printf("*****************************\n");
printf("celsius to fahrenheit\n");
printf("*****************************\n");
printf("\n enter temp in celsius:");
scanf("%f",&celsius);
fahrenheit=(1.8*celsius)+32;
printf("\n temperature in fahrenheit:%2f",fahrenheit);
getch();
}
OUTPUT:
**********************************************************************
Ex:no:2 ODD OR EVEN
**********************************************************************

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("*************************************\n");
printf("odd or even\n");
printf("*************************************\n");
printf("enter the values for n");
scanf("%d",&n);
if(n%2==0)
printf("even no %d",n);
else
printf("odd no%d",n);
getch();
}
OUTPUT:
**********************************************************************

Ex:no:3 BIGGEST OF THREE NUMBERS


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

#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c;
clrscr();
printf("**************************************\n");
printf("biggest of three numbers\n");
printf("**************************************\n");
printf("enter the value for a,b,c");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("biggest value%d",a);
}
else
{
printf("biggest value%d",c);
}
}
if(b>c)
{
printf("biggest value%d",b);
}
else
{
printf("biggest value%d",c);
}
getch();
}
**********************************************************************

Ex:no:9 QUADRATIC EQUATION


**********************************************************************
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
inta,b,c;
float d,r1,r2;
clrscr();
printf("\n******************************\n");
printf("\n QUADRATIC EQUATION");
printf("\n******************************\n");
printf("enter the value of a,b,c");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d==0)
{
printf("\n ROOTS ARE REAL AND EQUAL\n");
r1=-b/(2.0*a);
r2=r1;
printf("\n root1=%f",r1);
printf("\n root=%f",r2);
}
else if(d>0)
{
printf("\n ROOTS ARE REAL AND UNEQUAL\n");
r1=-b+sqrt(d/(2.0*a));
r2=-b-sqrt(d/(2.0*a));
printf("\n root1=%f",r1);
printf("\n root2=%f",r2);
}
else
{
printf("\n ROOTS ARE IMAGINARY\n");
r1=-b/(2.0*a);
r2=sqrt(-d/(2.0*a));
printf("\n root1=%f",r1);
printf("\n root2=%f",r2);
}
getch();
}
OUTPUT:
**********************************************************************
Ex:no:6 MATRIX MULTIPLICATION
**********************************************************************

#include<stdio.h>
#include<conio.h>
voidmul();
int a[3][3],b[3][3],c[3][3],i,j,k;
void main()
{
clrscr();
printf("*************************************\n");
printf(" MATRIX MULTIPLICATION\n");
printf("*************************************\n");
printf("Enter the array elements for A matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the array elements for B matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
mul();
getch();
}
voidmul()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n MULTIPLICATION OF MATRIX IS\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
OUTPUT:
**********************************************************************

Ex:no:4 TO DISPLAY SUNDAY TO MONDAY USING SWITCH CASE


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

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n************************************************\n");
printf("TO DISPLAY MONDAY TO SUNDAY USING SWITCH CASE\n");
printf("\n*************************************************\n");
printf("\n enter the number b/w 1 and 7:\n");
scanf("%d",&n);
switch(n)
{
case 1:
printf(" sunday is the first day of the week");
break;
case 2:
printf(" Monday is the second day of the week");
break;
case 3:
printf(" Tuesday is the third day of the week");
break;
case 4:
printf(" Wednesday is the fourth day of the week");
break;
case 5:
printf(" Thursday is the fifth day of the week");
break;
case 6:
printf(" Friday is the sixth day of the week");
break;
case 7:
printf(" Saturday is the seventh day of the week");
break;
default:
printf("you have entered a wrong choice");
}
getch();
}
OUTPUT:
**********************************************************************

Ex:no:8 REVERSE OF A NUMBER USING POINTER


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

#include<stdio.h>
#include<conio.h>
void main()
{
intn,a;
int *rev,*rem,*temp;
clrscr();
printf("\n************************************\n");
printf("REVERSE OF A NUMBER USING POINTER\n");
printf("\n************************************\n");
printf("enter any number\n");
scanf("%d",&n);
a=n;
temp=&n;
*rev=0;
while(*temp>0)
{
*rem=*temp%10;
*temp=*temp/10;
*rev=(*rev)*10+*rem;
}
printf("Reverse of %d is %d",a,*rev);
getch();
}
OUTPUT:

You might also like