Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Saturday, October 9, 2010

C Program for Linear Search

Here is my sample of C Program for Linear Search


#include
main()
{
int A[100],i,n,s,ch;
printf("\nEnter the size of the list : ");
scanf("%d",&n);
printf("\nEnter the elements of the list : ");
for(i=0;i
scanf("%d",&A[i]);

while(ch){
printf("\nEnter the element you want to search : ");
scanf("%d",&s);
for(i=0;i
if(s==A[i])
break;
if(i
printf("\nThe element found at index %d",i);
else
printf("\nThe element not found");
printf("\nSearch another element (yes=1,no=0)? ");
scanf("%d",&ch);
}
}

Saturday, May 1, 2010

Mencari Faktor Persekutuan Terbesar (FPB) Dengan C++

Berikut adalah program untuk mencari Faktor Persekutuan Terbesar (FPB)


#include< iostream >
using namespace std;

int gcd(int x, int y)
{
 int rem;
 if(y==0)
  return (x);
 else
 {
  rem = x%y;
  return (gcd(y,rem));
 }
}

int main()
{
 int a,b;
 cout<<"\nEnter two number : ";cin >> a >> b;
 
 cout<<"Greatest Common Divisor of "<< a <<" and "<< b;
cout <<" is "<< gcd(a,b)<< endl;
}


Keterangan:
Program diatas terdiri dari 2 fungsi gcd(int x,int y) dan main().
Fungsi gcd(int,int) adalah fungsi yang digunakan untuk mencari FPB.
Fungsi main() digunakan untuk mengeksekusi program.