Program c String Menghitung Jumlah Karakter

February 5, 2011

#include <stdio.h>
#define MAKS 256
main()
{
int i, jumkar = 0;
char teks[MAKS];
puts(“Masukkan suatu kalimat (maks 255 karakter).”);
//masukan dr keyboard
gets(teks);
for(i=0; teks[i]; i++)
jumkar++;
printf(“\nJumlah karakter = %d\n”, jumkar);
getche();
}


Program c If Else

February 5, 2011

#include<stdio.h>
main()
{
int n;
printf(“masukkan nilai anda \n”);
scanf(“%i”,&n);
if(n>=80 && n < 100)
printf(“kategori A \n”);
else
if(n>=70 && n <= 79)
printf(“kategori B \n”);
else
if(n>=60 && n <= 69)
printf(“kategori C \n”);
else
if(n>=50 && n <= 59)
printf(“kategori D \n”);
else
printf(“kategori E \n”);
}


Program c menghitung rata-rata nilai mahasiswa

February 5, 2011

#include <stdio.h>
main()
{
int j,i,m,total=0 ;
float x[10],rata,n;
char nama[10];

printf(“jumlah mahasiswa “);
scanf(“%d”,&m);
for (i=1; i<=m; i++)
{
printf(“nama mahasiswa ke_%d “,i);
scanf(“%s”,&nama);

printf(“jumlah nilai “);
scanf(“%f”,&n);
for (j=1 ;j<=n ;j++){
printf(“nilai ke %d = “,j);
scanf(“%f”,&x[j]);
total=total+x[j];

}}rata=total/n ;

printf(“nilai rata2 %s adalah %2.2f \n”,nama,rata)   ;

}


Program c membuat kolom perkalian

February 5, 2011

#include<stdio.h>
main()
{
int i;
i=1;
do
{
printf(“%i\n”,i);
i++;
}
while(i<=10);
getch();
return 0;
}


Program c Do While

February 5, 2011

#include<stdio.h>
main()
{
int i;
i=1;
do
{
printf(“%i\n”,i);
i++;
}
while(i<=10);
getch();
return 0;
}


Program c For

February 5, 2011

#include<stdio.h>
main()
{
int baris,kolom,hasil_tambah;
for(baris=1;baris<=5;baris++)
{
for(kolom=1;kolom<=5;kolom++)
{
hasil_tambah=baris+kolom;
printf(“%4d”,hasil_tambah);
}
printf(“\n\n”);
}
getche();
return(0);
}


Program c Segitiga Alphabet

February 5, 2011

#include <stdio.h>
#include <windows.h>

#define MAX 25
#define ALPHA 26
#define HORIZONTAL 0
#define VERTICAL 1

void FillBuffer(char*, int, int);
void DisplayBuffer(const char*, int, int);
void FlipBuffer(char*, int, int, int);
void Delay(void);

int main(int argc, char *argv[]) {
char buffer[MAX][MAX];
int max;
printf(“-> Tinggi : “);
scanf(“%d”,&max);
if((max > 0) && (max <= 25)) {
FillBuffer((char*)buffer,max,max);
printf(“-> Pertama :\n”);
DisplayBuffer((char*)buffer,max,max);
FlipBuffer((char*)buffer,max,max,HORIZONTAL);
printf(“\n-> Kedua :\n”);
DisplayBuffer((char*)buffer,max,max);
} else {
printf(“\n-> I am Sorry!\n”);
}
system(“PAUSE”);
return 0;
}

void FillBuffer(char* buffer, int rows, int cols) {
int row, col, idx = 0;
memset(buffer,’ ‘,rows*cols);
for(row = 0; row < rows; ++row) {
for(col = 0; col < row+1; ++col) {
*(buffer+cols*row+col) = ‘a’+idx;
idx = (idx+1)%ALPHA;
}
}
}

void DisplayBuffer(const char* buffer, int rows, int cols) {
int row, col;
for(row = 0; row < rows; ++row) {
for(col = 0; col < cols; ++col) {
printf(“%2c”,*(buffer+cols*row+col));
Delay();
}
printf(“\n”);
}
}

void FlipBuffer(char* buffer, int rows, int cols, int direction) {
switch(direction) {
case HORIZONTAL : {
int row;
for(row = 0; row < rows; ++row) {
int begin = 0, end = cols-1;
while(begin < end) {
int idx = cols*row;
char tmp = *(buffer+idx+begin);
*(buffer+idx+begin) = *(buffer+idx+end);
*(buffer+idx+end) = tmp;
++begin;
–end;
}
}
} break;

case VERTICAL : {
int col;
for(col = 0; col < cols; ++col) {
int begin = 0, end = rows-1;
while(begin < end) {
char tmp = *(buffer+rows*+col+begin);
*(buffer+rows*+col+begin) = *(buffer+rows*+col+end);
*(buffer+rows*+col+end) = tmp;
++begin;
–end;
}
}
} break;

default :
break;
}
}

void Delay(void) {
int i = 0;
while(i < 10000000) {
++i;
}
}