0% found this document useful (0 votes)
60 views8 pages

Cpract

The document contains code snippets from multiple C programming practicals involving functions, pointers, arrays, and parameter passing techniques. Specifically, it includes programs to: 1) Calculate mathematical operations on a number using switch case 2) Check if a number is prime using a user-defined function 3) Demonstrate call by reference parameter passing by sorting three numbers in ascending order 4) Recursively check if a number is prime 5) Sort an array using a pointer 6) Demonstrate call by value parameter passing by swapping two numbers in a function

Uploaded by

PRATIKSHA BHOYAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views8 pages

Cpract

The document contains code snippets from multiple C programming practicals involving functions, pointers, arrays, and parameter passing techniques. Specifically, it includes programs to: 1) Calculate mathematical operations on a number using switch case 2) Check if a number is prime using a user-defined function 3) Demonstrate call by reference parameter passing by sorting three numbers in ascending order 4) Recursively check if a number is prime 5) Sort an array using a pointer 6) Demonstrate call by value parameter passing by swapping two numbers in a function

Uploaded by

PRATIKSHA BHOYAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Practical No : 8

/* write a program to find square,square root,cube,cube root of a number using


switch case statement */

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,choice;
clrscr();
printf("Enter a Number:") ;
scanf("%d",&n);
printf("1.Square\n2.Square Root\n3.cube\4.Cube Root\n");
printf("\nEnter a Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nSquare of a number is %d",(n*n));
break;
case 2:
printf("\nSquare Root of a number is %d",sqrt(n));
break;
case 3:
printf("\nCube of a Number is %d",(n*n*n));
break;
case 4:
printf("\nCube Root of a number is %d",pow(n,1.0/3.0));
break;
default:
printf("\nWrong Choice.");
}
getch();
}
Practical No : 11
/* write a program to test whether given number is prime using function */

#include<stdio.h>
#include<conio.h>
int main()
{
int n, res;
clrscr();
printf("Enter a Number:\n");
scanf("%d",&n);
res=prime(n);
if(res==0)
{
printf("%d is a Prime Number", n);
}
else
{
printf("%d is not a prime Number",n);
}
getch();
}
int prime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i!=0)
{
continue;
}
else
{
return 1;
}
}
return 0;
}

Practical No : 13
/* Write a program to demonstrate call by reference parameter passing technique
*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void ascend(int *first,int *mid,int *last)
{
int a,b,c,lar,sml,middle;
a=*first;
b=*mid;
c=*last;
lar=sml=a;
if(lar<b) lar=b;
if(lar<c) lar=c;
if(sml>b) sml=b;
if(sml>c) sml=c;
middle=(a+b+c)-lar-sml;
*first=sml;
*mid=middle;
*last=lar;
}
int main()
{
int a,b,c;
clrscr();
printf("Enter three numbers:");
scanf("%d %d %d",&a,&b,&c);
ascend(&a,&b,&c);
printf("The numbers in ascending order:");
printf("%d %d %d",a,b,c);
getch();
return 0;
}

Practical No : 14
/* Write a recursive program to test whether given number is prime */

#include<stdio.h>
#include<conio.h>
int main()
{
int n,p;
clrscr();
printf("Enter any number:");
scanf("%d",&n);
p=prime(n,n/2);
if(p==1)
{
printf("%d is a prime number",n);
}
else
{
printf("%d is a composite number",n);
}
getch();
}
int prime(int n,int i)
{
if(i==1)
return 1;
if(n%i==0)
return 0;
return prime(n,i-1);
}

Practical No : 16
/* Write a program to sort an Array using Pointer */

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i,j,n,temp;
int *p=&a[0];
clrscr();
scanf("%d",&n);
printf("\nEnter the elements in array");
for(i=0;i<n;i++)
{
scanf("%d",(p+i)); printf("\nEnter the size of array:");

}
printf("\nThe element of an array is:");
for(i=0;i<n;i++)
{
printf(" %d",*(p+i));
}
//Logic to sort an array using pointer
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(*(p+j) > *(p+(j+1)))
{
temp=*(p+j) ;
*(p+j)=*(p+(j+1));
*(p+(j+1))=temp;
}
}
}
printf("\nArray element of an array after sorting :");
for(i=0;i<n;i++)
{
printf(" %d",*(p+i));
}
getch();
}
/* Write a program to sort an Array using Pointer */
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i,j,n,temp;
int *p=&a[0];
clrscr();
printf("\nEnter the size of array:");
scanf("%d",&n);
printf("\nEnter the elements in array");
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}
printf("\nThe element of an array is:");
for(i=0;i<n;i++)
{
printf(" %d",*(p+i));
}
//Logic to sort an array using pointer
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(*(p+j) > *(p+(j+1)))
{
temp=*(p+j) ;
*(p+j)=*(p+(j+1));
*(p+(j+1))=temp;
}
}
}
printf("\nArray element of an array after sorting :");
for(i=0;i<n;i++)
{
printf(" %d",*(p+i));
}
getch();
}

Practical No : 12
/* write a program to demonstrate call by value parameter passing technique */

#include<stdio.h>
#include<conio.h>
int pass(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("The value before swap is x=%d,y=%d\n",x,y);
return 0;
}
int main()
{
int x=14,y=56,temp;
clrscr();
printf("Enter number to check:\n");
pass(x,y);
printf("The swap value is x=%d,y=%d\n",x,y);
getch();
}

You might also like