Minggu, 05 Mei 2013

20 contoh program function (L/C)



Program 1 :
Code :
#include <stdio.h>
#include <conio.h>
int kali(int a,int b, int c)
{
int x;
x = a * b * c;
return(x);
}
int main()
{
int a,b,c;
printf("Masukkan angka 1 : ");
scanf("%d",&a);
printf("Masukkan angka 2 : ");
scanf("%d",&b);
printf("Masukkan angka 3 : ");
scanf("%d",&c);
printf("Hasil perkalian ketiga bilangan adalah %d",kali(a,b,c)); //parameter aktual
getch();
return(0);
}
Output:
 
Program 2 :
Code :
#include <stdio.h>
#include <conio.h>
void desc_sort(int *bil1,int *bil2) //parameter formal
{
if(*bil1<*bil2)
{
*bil1 = *bil1 + *bil2;
*bil2 = *bil1 - *bil2;
*bil1 = *bil1 - *bil2;
}
}
int main()
{
int a,b,c;
printf("Masukkan angka 1 : ");
scanf("%d",&a);
printf("Masukkan angka 2 : ");
scanf("%d",&b);
printf("Masukkan angka 3 : ");
scanf("%d",&c);
desc_sort(&a,&b); //parameter aktual
desc_sort(&a,&c); //parameter aktual
desc_sort(&b,&c); //parameter aktual
printf("Hasil descending sort adalah %2d %2d %2d",a,b,c);
getch();
return(0);
}
Output:
Program 3 :
Code :
# include <stdio.h>
# include <conio.h>
void perkalian (int a, int b, int c)
{
a= a*2 ; b= b*2 ; c= c * 2 ;
}
int main ( )
{
int x = 1, y = 3, z = 7 ;
perkalian ( x, y, z ) ;
printf (" X = %d  Y = %d Z = %d",x,y,z);
getch();
return 0 ;
}




Output:
Program 4 :
Code :
#include <stdio.h>
#include "conio.h"
int div(int num, int value);
int main()
{
            int x;
            printf("Enter a value for its divisors: ");
            scanf("%d",&x);
            div(x,x);
            getch();
            return 0;
}
int div(int num, int value)
{           if ( (num>=1) && (value%num==0) )
            printf("%d ",num);
            num=num-1;
           
            if (num==0) return 0;
            else return div(num,value);
            getch();
            return 0;
}
Output:
Program 5 :
Code :
#include <stdio.h>
#include <conio.h>
double Absolut(double X); /*deklarsi fungsi Absolut */
void main()
{
float Nilai;
Nilai = -123,45;
printf("%7.2f nilai mutlaknya adalah %7.2f\n",Nilai,Absolut(Nilai));
getch();
}
/*----- Fungsi untuk memutlakkan nilai negatip- -----*/
double Absolut(double X) /* definisi fungsi */
{
if(X<0) X = -X;
return(X);
}
Output:
Program 6 :
Code :

#include <stdio.h>
#include <conio.h>
int faktorial (int N); /*prototype fungsi factorial*/
void main()
{
int N;
int fak;
printf ("berapa factorial ? ");scanf("%d",&N);
fak = faktorial(N);
printf("%d factorial = %d\n",N,fak);
getch();
}
/*-----fungsi untuk menghitung nilai N factorial -----*/
int faktorial(int N) /*definisi fungsi*/
{
int I;
int F=1;
if(N<=0)
return(0);
for(I=2;I<=N;I++) F *= I;
return}



Output:
Program 7 :
Code :
#include <stdio.h>
#include <conio.h>
int TAMBAH(int A,int B);
int main(){
int hasil;
hasil = TAMBAH(2,3);
printf("Hasil = %d",hasil);
getch();
}
int TAMBAH(int A,int B){
int C;
C = A + B;
{
float C;
C = 100;
}
return(C);
}
Output:
Program 8 :
Code :
#include <stdio.h>
#include <conio.h>
int d=3,e=1;
void coba_lokal(int a,int b){
int c = 0;
int d = 10;
int e;
e = (a+b) * (c+d);
printf("lokal a = %d\n",a);
printf("lokal b = %d\n",b);
printf("lokal c = %d\n",c);
printf("lokal d = %d\n",d);
printf("lokal e = %d\n",e);
}
void main(){
int a=2;
int b;
b = 4;
int c=0;
printf("global a = %d\n",a);
printf("global b = %d\n",b);
coba_lokal(a,b);
printf("main c = %d\n",c);
printf("global d = %d\n",d);
printf("global e = %d\n",e);
getch();
}
Output:
Program 9 :
Code :
#include <stdio.h>
#include <conio.h>
int c = 4;
int m = 3;
void lokal(){
int a = 5;
int b = a + 2;
printf("lokal a = %d\n",a);
printf("lokal b = %d\n",b);
//karena tidak ada c, maka ambil global
printf("global c = %d\n",c);
}
void main(){
int a = 1;
int b = 2;
int c = 3;
lokal();
printf("main a = %d\n",a);
printf("main b = %d\n",b);
//walaupun global c ada tapi c yang digunakan yang di main
printf("main c = %d\n",c);
//karena tidak ada m, maka ambil global
printf("global m = %d\n",m);
getch();
}
Output:
Program 10 :
Code :
#include <stdio.h>
#include <conio.h>
void coba_static(int a){
static int b=3;
int c=1;
printf("b awal : %d\n",b);
b += a;
printf("b akhir : %d\n",b);
printf("c awal : %d\n",c);
c += a;
printf("c akhir : %d\n",c);
}
void main(){
int a=2;
coba_static(a);
a=3;
coba_static(a);
getch();
}



Output:
Program 11 :
Code :
#include <stdio.h>
#include <conio.h>
void tukar (int *px, int *py);

main()
{
     int a,b;
     a = 88;
     b = 77;

     printf("Nilai sebelum pemanggilan fungsi\n");
     printf("a = %d b = %d\n\n",a,b);

     tukar(&a,&b); /* parameter alamat a dan alamat b */

     printf("Nilai sesudah pemanggilan fungsi\n");
     printf("a = %d b = %d\n\n",a,b);
}

void tukar (int *px, int *py)
{
     int z;
     z = *px;
     *px = *py;
     *py = z;
     printf("Nilai diakhir fungsi tukar()\n");
     printf("*px = %d *py = %d\n\n",*px,*py);
     getch();
}

Output:
Program 12 :
Code :
#include <stdio.h>
#include <conio.h>
            void tukar (int x, int  y);
            main()
            {
                        int a,b;
                        a = 88;
                        b = 77;
           
                        printf("Nilai sebelum pemanggilan fungsi\n");
                        printf("a = %d b = %d\n\n",a,b);

                        tukar(a,b);
                        printf("Nilai sesudah pemanggilan fungsi\n");
                        printf("a = %d b = %d\n\n",a,b);
            }
           
            void tukar (int x, int y)
            {
                        int z;   
                        z = x;
                        x = y;
                        y = z;
                        printf("Nilai diakhir fungsi tukar()\n");
                        printf("x = %d y = %d\n\n",x,y);
     getch ();
     }
Output:
Program 13 :
Code :
#include<stdio.h>
#include<conio.h>

void myFunction();
int add(int a, int b);

int main()
{

            myFunction();
            printf("\n\n%d",add(10,15));

            getch();
}

void myFunction()
{
            printf("This is inside function :D");
}

int add(int a, int b)
{
            return a+b;
}
Output:
Program 14 :
Code :
#include<stdio.h>
#include<conio.h>
void add(int x,int y)
{
int result;
result = x+y;
printf("Sum of %d and %d is %d.\n\n",x,y,result);
}
int main()
{
add(10,15);
add(55,64);
add(168,325);
getch();
}
Output:
Program 15 :
Code :
#include <stdio.h>
#include <conio.h>
float arit_midd( float a, float b, float c ){

  float ar;
  ar = (a + b + c) / 3;
  return ar;

}


int main(void) {

   float x, y, z, midd;
   printf("\nInput Tiga angka : ");
   scanf("%f %f %f", &x, &y, &z );
   midd = arit_midd(x,y,z);
   printf("\nRata- Ratanya : %f", midd);
getch ();
}
Output:
Program 16 :
Code :
#include <stdio.h>
#include <conio.h>
int twotimes(int x){

   printf ("\nF:Argument before change took place %d",x);
   x *= 2;
   printf ("\nF:Argument after tempering with it %d",x);
   return x;

}


int main(void) {

   int number=10;
   printf ("\nM:Number before function-call %d",number);
   twotimes(number);
   printf("\nM:Number after function-call %d",number);
getch();
}
Output:
Program 17 :
Code :
#include <stdio.h>
#include <conio.h>

int number(int n)
{
      return n;
}
void main()
{
      int number(int);
      int a = number(4);
      clrscr();
      printf("\n Number is : %d",a);
      getch();
}


Output:
Program 18 :
Code :
#include <stdio.h>
#include <conio.h>
 
void printno(int a)
{
      printf("\n Number is : %d", a);
}
void main()
{
      int no;
      void printno(int);
      clrscr();
      printf("\n Enter Number : ");
      scanf("%d", &no);
      printno(no);
      getch();

Output:
Program 19:
Code :
#include <stdio.h>
#include <conio.h>

Recursion()
{
            int no;
            printf("\nRecursion... ");
            printf("\n\n Enter Number : ");
            scanf("%d",&no);
            if (no==3)
                        exit(0);
            else
                        Recursion();
}
int main()
{
            Recursion();
}
Output:






Program 20 :
Code :
#include <stdio.h>
#include <stdlib.h>

void func(int);

main(){
      void (*fp)(int);

      fp = func;

      (*fp)(1);
      fp(2);

      exit(EXIT_SUCCESS);
}

void
func(int arg){
      printf("%d\n", arg);
}

Output:




Tidak ada komentar:

Posting Komentar