• Home
  • Posts RSS
  • Comments RSS
  • Edit
  • Contoh 8 Program C

    9 Jun 2013
    Contoh Program Bahasa C

    1.

    #include "stdio.h"
    #include "conio.h"
    void tampil(void);
    int i = 25; /* variabel global */
    void main(){
    clrscr();
    printf("Nilai variabel i dalam fungsi main() adalah %i\n", i);
    tampil();
    i = i * 4; /* nilai i yang dikali 4 adalah 25 (global) bukan 10 */
    printf("Nilai variabel i dalam fungsi main() sekarang adalah %i\n",i);
    getch();
    }
    void tampil(void){
    int i = 10; /* variabel lokal */
    printf("Nilai variabel i dalam fungsi tampil() adalah %i\n", i);
    }


    2.

    /*Contoh Program Penggunaan Variabel Statis*/
    #include <stdio.h>
    void fung_y(void);
    main()
    {
    int y = 20;
    fung_y();
    fung_y();
    printf("Nilai y dalam main() = %d\n", y);
    }
    void fung_y(void)
    {
    static int y; /*Variabel statis*/
    y++;
    printf("Nilai y dalam fung_y() = %d\n", y);
    }


    3.

    #include "stdio.h"
    #include "conio.h"
    void main(){ 
    register int i; /* variabel register */
    int jumlah;
    clrscr();
    for(i=1; i<=100; i++)
    jumlah = jumlah + i;
    printf("1+2+3+...+100 = %i\n", jumlah);
    getch();
    }


    4.

    /*Contoh fungsi dalam fungsi*/
    #include <stdio.h>
    void fungsi_1(void);
    void fungsi_2(void);
    main()
    {
    fungsi_1();
    }
    void fungsi_1()
    {
    puts("fungsi 1 dijalankan");
    fungsi_2();
    }
    void fungsi_2()
    {
    puts("fungsi 2 dijalankan");
    }


    5.

    /*Contoh fungsi rekursif untuk menyelesaikan faktorial sebuah bilangan*/
    #include "stdio.h"
    #include "conio.h"
    long int faktorial(int N); /* prototype fungsi faktorial */
    void main(){ 
    int N;
    clrscr();
    printf("Berapa factorial ? "); scanf("%i", &N);
    printf("Faktorial dari %i = %ld\n", N, faktorial(N));
    getch();
    }
    long int faktorial(int N) /* definisi fungsi faktorial */
    {
    if(N==0)
    return(1);
    else
    return(N * faktorial(N - 1)); 
    /* fungsi faktorial() memanggil fungsi faktorial()*/
    }


    6.

    #include<stdio.h>
    #include<conio.h>
    void main(){ 
    char *Alamat_X,X;
    clrscr();
    X='M';
    Alamat_X=&X;
    printf("Nilai variabel X,yaitu 'M' berada pada alamat %p\n",Alamat_X);
    getch();
    }


    7.

    #include<stdio.h>
    #include<conio.h>
    void main(){ 
    char *Alamat_X,X,Y,Z;
    X='M';
    Alamat_X=&X;
    Y=X;
    Z=*Alamat_X;
    clrscr();
    printf("Nilai variabel X adalah %c\n",X);
    printf("Nilai variabel Y adalah %c\n",Y);
    printf("Nilai variabel Z adalah %c\n",Z);
    printf("Nilai variabel X berada pada alamat %p\n",Alamat_X);
    getch();
    }


    8.

    #include<stdio.h>
    #include<conio.h>
    void main(){ 
    float nilai, *Alamat=&nilai;
    nilai = 23.99;
    clrscr();
    printf("nilai %6.2f berada di alamat memori %p\n",nilai,Alamat);
    }


    9.

    #include "stdio.h"
    #include "conio.h"
    void main()
    { int x, y; /* x dan y bertipe int */
    int *px; /* px pointer yang menunjuk objek */
    clrscr();
    x = 87;
    px = &x; /* px berisi alamat dari x */
    y = *px; /* y berisi nilai yang ditunjuk px */
    printf("Alamat x = %p\n", &x);
    printf("Isi px = %p\n", px);
    printf("Isi x = %i\n", x);
    printf("Nilai yang ditunjuk oleh px = %i\n", *px);
    printf("Nilai y = %i\n", y);
    getch();
    }


    10.

    #include<stdio.h>
    #include<conio.h>
    void main(){ 
    float nilai, *X1,*X2;
    nilai = 23.99;
    X1=&nilai;
    X2=X1;
    clrscr();
    printf("nilai variabel nilai ada di alamat memori %p\n",X1);
    printf("nilai variabel nilai ada di alamat memori %p\n",X2);
    }


    11.

    #include<stdio.h>
    #include<conio.h>
    void main(){ 
    int nilai[3],*point;
    nilai[0] = 23;
    nilai[1] = 99;
    nilai[2] = 27;
    point = &nilai[0];
    clrscr();
    printf("nilai %d ada di alamat memori %p\n",*point,point);
    printf("nilai %d ada di alamat memori %p\n",*(point+1),point+1);
    printf("nilai %d ada di alamat memori %p\n",*(point+2),point+2);
    }


    12.

    #include<stdio.h>
    #include<conio.h>
    void main(){ 
    char Nama[]="DIAN",*PoinNama;
    int i;
    PoinNama=Nama;
    clrscr();
    /*Variabel PoinNama menunjuk ke array Nama*/
    printf("Isi dari array Nama adalah\n");
    for(i=0;i<4;i++){
    printf("%c",*PoinNama);
       PoinNama++;
    }
    printf("\n");
    }


    13.

    #include "stdio.h"
    #include "conio.h"
    void main(){ 
    int a = 100, b = 200, *pa, *pb;
    clrscr();
    pa = &a;
    pb = &b;
    if(pa < pb)
    printf("pa menunjuk ke memori lebih rendah dari pb\n");
    if(pa == pb)
    printf("pa menunjuk ke memori yang sama dengan pb\n");
    if(pa > pb)
    printf("pa menunjuk ke memori lebih tinggi dari pb\n");
    getch();
    }


    14.

    #include "stdio.h"
    #include "conio.h"
    char *nama1 = "SPIDERMAN";
    char *nama2 = "GATOTKACA";
    void main(){ 
    char namax;
    clrscr();
    puts("SEMULA :");
    printf("Saya suka >> %s\n", nama1);
    printf("Tapi saya juga suka >> %s\n", nama2);
    /* Penukaran string yang ditunjuk oleh pointer nama1 dan nama2 */
    printf("SEKARANG :");
    printf("Saya suka >> %s\n", nama1);
    printf("Dan saya juga masih suka >> %s\n", nama2);
    getch();
    }


    15.

    #include<stdio.h>
    #include<conio.h>
    #include<graphics.h>
    int xmax, ymax;
    int GraphDriver, GraphMode, ErrorCode;
    main()
    {
    GraphDriver = DETECT;
    initgraph(&GraphDriver, &GraphMode, "c:\\TC\\bgi");
    ErrorCode = graphresult();
    if (ErrorCode != grOk) {
    printf("Graphics system error : %s\n",grapherrormsg(ErrorCode));
          exit(1);
          getch();
    }
    xmax = getmaxx();
    ymax = getmaxy();
          rectangle(0,0,xmax,ymax);
    line(0,0,xmax,ymax);
          line(0,ymax,xmax,0);
    getch();
    closegraph();
    return 0;
    }


    16.

    #include<stdio.h>
    #include<conio.h>
    #include<graphics.h>
    int xmax, ymax;
    int GraphDriver, GraphMode, ErrorCode;
    main()
    {
    GraphDriver = DETECT;
    initgraph(&GraphDriver, &GraphMode, "c:\\TC\\bgi");
    ErrorCode = graphresult();
    if (ErrorCode != grOk) {
    printf("Graphics system error : %s\n",grapherrormsg(ErrorCode));
          exit(1);
          getch();
    }
    xmax = getmaxx();
    ymax = getmaxy();
    setbkcolor(11);
    setcolor(1);
    rectangle(xmax/4,ymax/4,3*xmax/4,3*ymax/4);
    setcolor(3);
    line(xmax/4,ymax/4,3*xmax/4,3*ymax/4);
    line(3*xmax/4,ymax/4,xmax/4,3*ymax/4);
    setcolor(4);
    circle(xmax/2,ymax/2,200);
    getch();
    closegraph();
    return 0;
    }


    17.

    #include<stdio.h>
    #include<conio.h>
    #include<graphics.h>
    int x,y;
    int graphdriver,graphmode,errorcode;
    main(){
    graphdriver=DETECT;
    initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
    errorcode=graphresult();
    if(errorcode != grOk){
      printf("Grafik error : %s\n",grapherrormsg(errorcode));
      getch();
      }
    settextstyle(3,0,4);
    outtextxy(25,50,"PEMROGRAMAN C");
    outtextxy(25,100,"Nama Saya : MR. LOBA-LOBA");
    outtextxy(25,130,"Created by : Dianws");

    x=getmaxx()/2;
    y=getmaxy()/2;

    bar(x-75,y-50,x+75,y+100);
    setcolor(0);
    setfillstyle(9,14);
    fillellipse(x,y+25,60,60);
    setcolor(3);
    setlinestyle(1,1,1);
    setcolor(4);
    setlinestyle(0,0,3); 

    setfillstyle(10,0);
    fillellipse(x-25,y+10,5,5);
    fillellipse(x+25,y+10,5,5);

    setcolor(0);
    setlinestyle(0,0,3);
    ellipse(x,y+40,180,360,20,10);

    getch();
    closegraph();
    return 0;
    }

    0 komentar:

    Posting Komentar