WAP that uses function prime() for testing prime. Function should take integer argument and return Boolean value.


#include<iostream>
#include<conio.h>

using namespace std;

bool prime(int n);

int main()
{
int n;
cout<<"Enter any number : ";cin>>n;
if(prime(n)==1)
   cout<<n<<" is a prime number.";
else
   cout<<n<<" is not a prime number.";
getch();
return(0);
}

bool prime(int n)
{
if(n==0||n==1||n==2)
   return(0);
else
   {
for(int i=2;i<=(n/2);i++)
{
if(n%i==0)
   return(0);
}
return(1);
}
}

WAP to swap to character data using function.


#include<iostream>
#include<conio.h>

using namespace std;

void swap(char &,char &);

int main()
{
char ch1,ch2;
cout<<"Enter first character = ";cin>>ch1;
cout<<"Enter second character = ";cin>>ch2;
swap(ch1,ch2);
cout<<"After swapping character : ";
cout<<"\nNow first character = "<<ch1;
cout<<"\nNow second character = "<<ch2;
getch();
return(0);
}

void swap(char &a,char &b)
{
char temp;
temp=a;
a=b;
b=temp;
}

C++ : WAP to find surface area of box using inline function.


#include<iostream>
#include<conio.h>

using namespace std;

float surface_area(float l,float b,float h)
{
return(2*(l*b+b*h+l*h));
}

int main()
{
float l,b,h;
cout<<"Length = ";cin>>l;
cout<<"Breadth = ";cin>>b;
cout<<"Height = ";cin>>h;
cout<<"Surface Area = "<<surface_area(l,b,h);
getch();
return(0);
}

C++ : WAP to calculate simple interest using inline function.


#include<iostream>
#include<conio.h>

using namespace std;

float interest(float p,float t,float r)
{
return((p*t*r)/100);
}

int main()
{
float p,t,r;
cout<<"Principle = ";cin>>p;
cout<<"Time = ";cin>>t;
cout<<"Rate = ";cin>>r;
cout<<"Simple Interest = "<<interest(p,t,r);
getch();
return(0);

}

C++ : Write a program to print line of n character of user given character.


#include<iostream>
#include<conio.h>

using namespace std;

void display(char ch,int n);

int main()
{
int n;
char ch;
cout<<"Enter any character : ";cin>>ch;
cout<<"Enter the number of character '"<<ch<<"' to display : ";cin>>n;
display(ch,n);
getch();
return(0);
}

void display(char ch,int n)
{
for(int i=1;i<=n;i++)
{
cout<<ch;
}
}

C++ : Write a program using function to print line of 50 asterisk.


#include<iostream>
#include<conio.h>

using namespace std;

void display(char ch);

int main()
{
display('*');
getch();
return(0);
}

void display(char ch)
{
for(int i=1;i<=50;i++)
{
cout<<ch;
}
}

C : round robin algorithm implementation


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
char p[10][5];
int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m;
float avgwt;

for(i=0;i<n;i++)
{
printf("Enter the process name : ");
scanf("%s",&p[i]);
printf("Enter the processing time : ");
scanf("%d",&pt[i]);
}
m=n;
wt[0]=0;
i=0;
do
{
if(pt[i]>timer)
{
rt=pt[i]-timer;
strcpy(p[n],p[i]);
pt[n]=rt;
et[i]=timer;
n++;
}
else
{
et[i]=pt[i];
}
i++;
wt[i]=wt[i-1]+et[i-1];
}while(i<n);

count=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p[i],p[j])==0)
{
count++;
found=j;
}
}
if(found!=0)
{

wt[i]=wt[found]-(count*timer);
count=0;
found=0;
}
}
for(i=0;i<m;i++)
{
totwt+=wt[i];
}
avgwt=(float)totwt/m;
for(i=0;i<m;i++)
{
printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]);
}
printf("\nTotal waiting time %d\n",totwt);
printf("\nTotal avg.turnaround time: %f",avgwt);
getch();
}