Selasa, 21 Desember 2010

Contoh2 Program Bahasa C ???

Latihan I Luas Balok :

#include<stdio.h>
main()
{
int p=0;
int l=0;
int t=0;
int luas=0;

printf("luas balok\n");
printf("masukan panjang : ");
scanf("%d", &p);
printf("masukan lebar : ");
scanf("%d", &l);
printf("masukan tinggi : ");
scanf("%d", &t);

luas = p*l*t;
printf("luasnya adalah =%d", luas);
}

Bintang :


#include<stdio.h>

int main()
{
int i=0;
int j=0;
int a=0;

printf("masukan anda : ");
scanf("%d", &a);
for (i=1;i<=a;i++)
{
for (j=0;j<(a-i);j++)
{
printf("^");
}
for (j=0;j<((2*i)-1);j++)
{
printf("*");
}
for (j=0;j<(a-i);j++)
{
printf("^");
}
printf("\n");
}
getch();
return 1;
}

Latihan II Biodata :


#include<stdio.h>
main()
{
printf("nama: abdun wijaya \n");
printf("nim: 0902309 \n");
printf("ttl: Cirebon, 02 oktober 1990 \n");
printf("alamat: Kapten Abdul Hamid no.125, Panorama \n");
printf("hobby: organisasi dan gamers \n");
printf("Jenjang Pendidikan : \n");
printf(" - TK Nurrusa'adah 1995-1997 \n");
printf(" - SDN 1 Panembahan 1997-2003 \n");
printf(" - SMPN 2 Cirebon 2003-2006 \n");
printf(" - SMAN 4 Cirebon 2006-2009 \n");
printf(" - Universitas Pendidikan Indonesia 2009- ... \n");


getch ();
return 1;
}

Latihan III Luas Segitiga :


#include<stdio.h>
main()
{
int a=0;
int l=0;
float luas=0;
int kel=0;

printf("luas segitiga\n");
printf("masukan alas : ");
scanf("%d", &a);
printf("masukan tinggi : ");
scanf("%d", &l);

luas = (a*l)/2;
printf("luasnya adalah =%.2f", luas);

}

Latihan IV Biodata ke-2 :


#include<stdio.h>
#include<string.h>

typedef struct{
char a[25];
}string;

main()
{
string nama;
string nim;
string ttl;
string alamat;

printf("masukan nama anda: \n");
scanf("%s", &nama);
printf("masukan nim anda: \n");
scanf("%s", &nim);
printf("masukan tempat dan tanggal lahir anda: \n");
scanf("%s", &ttl);
printf("masukan alamat anda: \n");
scanf("%s", &alamat);

printf("\n\n\nnama anda >> %s \n", &nama);
printf("nim anda >> %s \n", &nim);
printf("tempat tanggal lahir anda >> %s \n", &ttl);
printf("alamat anda >> %s \n", &alamat);

getch();
return 1;
}

Latihan V Diamond :


#include<stdio.h>

main()
{
int a,b,c,d,T;
printf("Program latihan 1 diamond\n");
printf("Inputkan angka: ");
scanf("%i",&T);

for(a=1;a<=T;a++){
for(b=T;b>=a;b--)
{
printf(" ");
}
for(c=1;c<=a;c++)
{
printf("*");
}
for(c=1;c<=a;c++)
{
printf("*");
}
printf("\n");
}

for(a=T;a>=1;a--){
for(b=T;b>=a;b--)
{
printf(" ");
}
for(c=1;c<=a;c++)
{
printf("*");
}
for(c=1;c<=a;c++)
{
printf("*");
}
printf("\n");
}
getch();
return 1;
}

Latihan VI Looping :


#include <stdio.h>
#include <conio.h>
int main(){
char a;
printf("Contoh program latihan make while\n");
printf("Akan berhenti jika anda inputkan karakter '-'\n");
printf("Inputkan: ");
scanf("%c",&a);
while (a!='-')
{
printf("Masukan karakter: ");
scanf("%c",&a);
}
getch();
return 1;
}


Latihan VII Kupu-kupu :


#include <stdio.h> int main() { int a,b,c,T; printf("Program latihan 3 dasi kupu-kupu\n"); printf("Inputan: "); scanf("%i",&T); for (a=1;a<=T;a++) { for (b=1;b<=a;b++) printf("*"); for (c=1;c<=(2*(T-a));c++) printf(" "); for (b=1;b<=a;b++) printf("*"); printf("\n"); } for (a=T-1;a>=1;a--) { for (b=1;b<=a;b++) printf("*"); for (c=1;c<=(2*(T-a));c++) printf(" "); for (b=1;b<=a;b++) printf("*"); printf("\n"); } } 

Latihan VIII Inputan Kata :


#include <stdio.h> #include <stdlib.h> char* ubah (char* s) { int pjg ,i; char* kata_br; pjg = strlen(s); for (i=0;i<pjg;i++) { if (islower(s[i])) { s[i] = toupper(s[i]); } else { s[i] = tolower(s[i]); } } return s; } char* urut (char* s) { int pjg ,i,j; char temp; pjg = strlen(s); for (i=pjg-2;i>=0;i--) { for (j=0;j<=i;j++) { if (tolower(s[j]) >= tolower(s[j+1])) { temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; } } } return s; } int main(int argc, char *argv[]) { char kalimat[100]; char* konversi; int i; printf("Latihan 1 Inputkan kata/kalimat:"); gets(kalimat); konversi = ubah(kalimat); printf("\n\n\n"); printf("%s",urut(konversi)); printf("\n\n\n\n"); system("PAUSE"); return 0; }  


Latihan IX Sorting :


#include <stdio.h> int main() { int N,i,y,j; char a[35]; char temp; printf("Program Latihan 2 sorting/mengurutkan word\n"); printf("\n"); printf("\n"); printf("huruf yang diurutkan: "); scanf("%d",&N); for (i=1;i<=N;i++) { printf("huruf ke %d: ",i); scanf("%s",&a[i]); } printf("\n"); for (i=N-1;i>=0;i--) { for (j=0;j<=i;j++) { if ((a[j]) >= (a[j+1])) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } printf("urutan huruf \n"); for (i=1;i<=N;i++) { printf("huruf ke %d adalah %c\n",i,a[i]); } getch(); return 0; }

Tidak ada komentar:

Posting Komentar