Tuesday, 24 February 2015

Function in C++

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

main()
{
clrscr();

void printline(char); // Function Declaration

printline('*');
cout<<"\n\t\t Languages\n";
printline('&');

cout<<"\n\t\t C";
cout<<"\n\t\t C++";
cout<<"\n\t\t Java\n";

printline('-');

getch();
}

void printline(char c)
{
int i;
for(i=0;i<65;i++)
cout<<c;
}



====================


/* Passing Arrays to functions */

#include<iostream.h>
#include<conio.h>
int sum(int[],int);
main()
{
int a[5];
clrscr();

a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;

cout<<"\n Sum is :"<<sum(a,5);
getch();
}
sum(int a[],int n)
{
int sum=0,i;
for(i=0;i<n;i++)
sum=sum+a[i];
return sum;
}

===========================

#include<iostream.h>
#include<conio.h>
long PrimeCheck(long);
main()
{
long prime;
clrscr();
cout<<"\n Enter Number To Check Whether Prime or Not\n";
cin>>prime;
if(PrimeCheck(prime))
cout<<"\n Given No is not prime no";
else
cout<<"\n Given No is prime no";

getch();
}
long PrimeCheck(long prime)
{
long i;
for(i=2;i<prime;i++)
{
if(prime%i==0)
{
return 1;
}
}
return 0;
}

No comments:

Post a Comment