Contoh 6 Program C
9 Jun 2013
Contoh Program Bahasa C
1.
/* Menginput bilangan ke dalam array dimensi dua dan menampilkannya */
#include "stdio.h"
#include "conio.h"
void main()
{ int baris, kolom, matriks[3][4];
clrscr();
/* Input elemen array secara Row Major Order */
printf("Input elemen Array : \n");
for(baris=0; baris<3; baris++)
{ for(kolom=0; kolom<4; kolom++)
{ printf("matriks[%i][%i] = ", baris+1, kolom+1);
scanf("%i", &matriks[baris][kolom]);
} printf("\n");
}
/* Tampilkan elemen Array secara Row Major Order */
printf("Isi array : \n");
for(baris=0; baris<3; baris++)
{ for(kolom=0; kolom<4; kolom++)
{ printf("%4i", matriks[baris][kolom]);
}
printf("\n");
}
getch();
}
1.
/* Menginput bilangan ke dalam array dimensi dua dan menampilkannya */
#include "stdio.h"
#include "conio.h"
void main()
{ int baris, kolom, matriks[3][4];
clrscr();
/* Input elemen array secara Row Major Order */
printf("Input elemen Array : \n");
for(baris=0; baris<3; baris++)
{ for(kolom=0; kolom<4; kolom++)
{ printf("matriks[%i][%i] = ", baris+1, kolom+1);
scanf("%i", &matriks[baris][kolom]);
} printf("\n");
}
/* Tampilkan elemen Array secara Row Major Order */
printf("Isi array : \n");
for(baris=0; baris<3; baris++)
{ for(kolom=0; kolom<4; kolom++)
{ printf("%4i", matriks[baris][kolom]);
}
printf("\n");
}
getch();
}
2.
#include<stdio.h>
#include<conio.h>
main(){
char nama[10][80],jwb;
float x[10];
float total=0;
int i,n,j;
clrscr();
printf("Input jumlah data max 10 = ");
scanf("%i",&n);
for(i=0;i<=n-1;i++)
{
printf("\nData ke %i\n",i+1);
printf("Nama Mahasiswa = "); fflush(stdin);
scanf("%[^\n]",nama[i]);
printf("input nilai = "); fflush(stdin);
scanf("%f",&x[i]);
total=total+x[i];
}
printf ("Rata-rata = %f\n",total/n);
printf("Apakah Anda ingin menampilkan \
data yg telah diinput?(Y/N) ");
jwb=getche();
if (toupper(jwb)=='Y')
{ for(i=0;i<n;i++)
{
printf("\nData ke %i\n",i+1);
printf("Nama Mahasiswa = %s\n",nama[i]);
printf("Nilai = %f\n",x[i]);
}
}
getch();
}
3.
/* Program penjumlahan matriks dua dimensi */
#include "stdio.h"
#include "conio.h"
void main()
{ int A[3][4], B[3][4], X[3][4], i, j;
clrscr();
/******* Masukkan matriks A *******/
for(i=0;i<3;i++)
{ for(j=0;j<4;j++)
{ printf("input data matrik A[%i][%i] : ",i+1,j+1);
fflush(stdin);scanf("%i",&A[i][j]);
} printf("\n");
}
printf("\n");
/******** Masukkan matriks B ********/
for(i=0;i<3;i++)
{ for(j=0;j<4;j++)
{ printf("input data matrik B[%i][%i] : ",i+1,j+1);
fflush(stdin);scanf("%i",&B[i][j]);
} printf("\n");
}
printf("\n");
/******** Proses penjumlahan matriks A dan B ********/
for(i=0;i<3;i++)
{ for(j=0;j<4;j++)
{ X[i][j]=A[i][j]+B[i][j];
}
}
/******** Cetak isi matriks A ********/
printf("\n matrik A\n");
for(i=0;i<3;i++)
{ for(j=0;j<4;j++)
printf("%6i",A[i][j]);
printf("\n");
}
printf("\n");
/******** Cetak isi matriks B *******/
printf("\n matrik B\n");
for(i=0;i<3;i++)
{ for(j=0;j<4;j++)
printf("%6i",B[i][j]);printf("\n");
}
printf("\n");
/******* Cetak hasil penjumlahan matriks A dan B *******/
printf("\n matrik penjumlahan A+B\n");
for(i=0;i<3;i++)
{ for(j=0;j<4;j++)
printf("%6i",X[i][j]);printf("\n");
}
printf("\n\n");
getch();
}
4.
/*Contoh Array Tdk Berukuran*/
#include <stdio.h>
void main()
{
float x[] = {5,3,7}, total = 0;
int i;
for (i=0;i<=2;i++)
total = total + x[i];
printf ("Total = %f\n",total);
}
5.
#include "stdio.h"
#include "conio.h"
#include "math.h"
void main()
{
int x, y;
float z;
clrscr();
printf("Menghitung x pangkat y\n");
printf("x = "); scanf("%i", &x);
printf("y = "); scanf("%i", &y);
printf("%i dipangkatkan dengan %i = %7.2lf", x, y, pow(x, y));
getch();
clrscr();
printf("Menghitung akar suatu bilangan z\n");
printf("z = "); scanf("%f", &z);
printf("Akar dari %f adalah %7.2lf", z, sqrt(z));
getch();
}
6.
#include "stdio.h"
#include "conio.h"
#include "math.h"
void main()
{
float sudut;
clrscr();
printf("Menghitung nilai sinus, cosinus dan tangens\n");
printf("Masukkan sudut : "); scanf("%f", &sudut);
printf("Nilai sinus %.2f derajat = %.3f\n", sudut, sin(sudut));
printf("Nilai cosinus %.2f derajat = %.3f\n", sudut, cos(sudut));
printf("Nilai tangens %.2f derajat = %.3f\n", sudut, tan(sudut));
getch();
}
7.
#include "stdio.h"
#include "conio.h"
#include "math.h"
void main()
{ char x[4] = "100", y[5] ="10.3";
int a;
float b;
clrscr();
a = atoi(x); b = atof(y);
printf("Semula A = %s B = %s\n", x,y);
printf("Setelah dikonversi A = %i B = %.2f", a,b);
getch();
}
8.
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
void main()
{
int x, y;
div_t hasil;
clrscr();
printf("Menghitung sisa dan hasil pembagian x dengan y\n");
printf("x = "); scanf("%i", &x);
printf("y = "); scanf("%i", &y);
hasil = div(x,y);
printf("\n\n");
printf("%3i div %3i = %3i sisa %3i", x, y,hasil.quot,hasil.rem);
getch();
}
9.
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
void main()
{
int x, y, z;
clrscr();
printf("Menentukan bilangan terbesar dan terkecil\n");
printf("X = "); scanf("%i", &x);
printf("Y = "); scanf("%i", &y);
printf("Z = "); scanf("%i", &z);
printf("\nBilangan terbesar : %i", max(max(x, y), z));
printf("\nBilangan terkecil : %i", min(min(x, y), z));
getch();
}
10.
#include<stdio.h>
#include<conio.h>
void info_program(); /*Prototype Fungsi*/
main()
{
clrscr();
printf("\nInfo Pembuat Program \n");
info_program();
getch();
info_program();
}
void info_program() /*Definisi Fungsi*/
{
printf("Designed Program by \n");
printf("Lab. Kom. Digital \n");
printf("Universitas Sumatera Utara \n");
}
0 komentar: