Assalamualaikum wr wb.
Berikut saya lampirkan beberapa source kode yang pernah saya pelajari :
1. untuk menghitung jumlah kapital
#include <stdio.h>
#include <conio.h>
main()
{
char st[80];
int i, jum_hrf_kecil=0, jum_hrf_besar=0;
puts("masukkan sembarang string");
gets(st);
for(i=0;st[i];i++)
if(st[i]>='a'&&st[i]<='z')
jum_hrf_kecil++;
else
if(st[i]<='A' && st[i]<='Z')
jum_hrf_besar++;
printf("Jumlah huruf besar : %d\n",jum_hrf_besar);
printf("Jumlah huruf kecil : %d\n",jum_hrf_kecil);
getch();
}
#include <conio.h>
main()
{
char st[80];
int i, jum_hrf_kecil=0, jum_hrf_besar=0;
puts("masukkan sembarang string");
gets(st);
for(i=0;st[i];i++)
if(st[i]>='a'&&st[i]<='z')
jum_hrf_kecil++;
else
if(st[i]<='A' && st[i]<='Z')
jum_hrf_besar++;
printf("Jumlah huruf besar : %d\n",jum_hrf_besar);
printf("Jumlah huruf kecil : %d\n",jum_hrf_kecil);
getch();
}
2. Huruf besar dan kebalikan
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>
void main()
{
char a[100];
int b,c;
printf("Masukkan kata atau kalimat = ");
gets(a);
b=strlen(a);
strupr(a);
printf("Ejaan yang terbalik dan Kapital = ");
for(c=0;c<b;c++)
{ printf("%c",a[b-1-c]);
} getch ();
}
#include <conio.h>
#include <string.h>
#include <iostream.h>
void main()
{
char a[100];
int b,c;
printf("Masukkan kata atau kalimat = ");
gets(a);
b=strlen(a);
strupr(a);
printf("Ejaan yang terbalik dan Kapital = ");
for(c=0;c<b;c++)
{ printf("%c",a[b-1-c]);
} getch ();
}
3. menghitung jumlah spasi
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char a[30];
int b,c,d=0;
printf("=====Program Jumlah Spasi Pada Kalimat=====");
printf("\n\nKetikan Kalimat Maks 30 Karakter:\n\n");
gets(a);
b=strlen(a);
for(c=0;c<=b;c++)
if(a[c]==' ')
{
d=d+1;
}
printf("\n\nJumlah Spasi=%d\n\n",d);
getch();
}
#include <conio.h>
#include <string.h>
void main(){
char a[30];
int b,c,d=0;
printf("=====Program Jumlah Spasi Pada Kalimat=====");
printf("\n\nKetikan Kalimat Maks 30 Karakter:\n\n");
gets(a);
b=strlen(a);
for(c=0;c<=b;c++)
if(a[c]==' ')
{
d=d+1;
}
printf("\n\nJumlah Spasi=%d\n\n",d);
getch();
}
4. kalimat polindrom
#include <stdio.h>
#include <conio.h>
#include <string.h>
void palindrom ( char data [], int x);
main()
{
char word [25];
int len_word;
printf("masukkan kata : ");
gets(word);
len_word=strlen(word);
printf("%d kata ", len_word);
palindrom(word, len_word);
getch();
return 0;
}
void palindrom ( char data [], int x)
{
int i=0, k=0;
while (i<(x-1)/2)
{
if (data [i]!=data[x-1-i])
{
k++;
break;
}
i++;
}
printf ("\n");
if (k==0)
printf ("polindrom");
else
printf("bukan polindrom");
} ak
#include <conio.h>
#include <string.h>
void palindrom ( char data [], int x);
main()
{
char word [25];
int len_word;
printf("masukkan kata : ");
gets(word);
len_word=strlen(word);
printf("%d kata ", len_word);
palindrom(word, len_word);
getch();
return 0;
}
void palindrom ( char data [], int x)
{
int i=0, k=0;
while (i<(x-1)/2)
{
if (data [i]!=data[x-1-i])
{
k++;
break;
}
i++;
}
printf ("\n");
if (k==0)
printf ("polindrom");
else
printf("bukan polindrom");
} ak
5. array matriks
#include <conio.h>
#include <stdio.h>
main()
{
int i,j;
int matriks [3][2]={{1,2},{3,4},{5,6}};
for (i=0;i<3;i++)
{
for (j=0;j<2;j++)
printf("%i",matriks [i][j]);
printf("\n");
}
getch();
}
#include <stdio.h>
main()
{
int i,j;
int matriks [3][2]={{1,2},{3,4},{5,6}};
for (i=0;i<3;i++)
{
for (j=0;j<2;j++)
printf("%i",matriks [i][j]);
printf("\n");
}
getch();
}
6. transpose matriks
#include<stdio.h>
#include<conio.h>
#include<windows.h>
main()
{
int pil,a,b,i,j,sum=0,k;
int M1[50][50];
int M2[50][50];
int M3[50][50];
do
{
system ("cls");
printf("\n**************************************");
printf("\n Belajar Array dan String ");
printf("\n**************************************");
printf("\n1. Penjumlahan ");
printf("\n2. Pengurangan ");
printf("\n3. Perkalian Transpose 2x2 ");
printf("\n4. Perkalian Transpose 3x3 ");
printf("\n5. Keluar ");
printf("\n**************************************");
printf("\n\tMasukkan Pilihan [1-5]: ");
scanf("%d",&pil);
if (pil==1)
{
printf("\n**************************************");
printf("\nPenjumlahan Matriks\n");
printf("Masukkan Baris : ");
scanf ("%d",&a );
printf("Masukkan Kolom : ");
scanf ("%d",&b );
printf("\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("Elemen M1[%d][%d]: ",i,j);
scanf("%d",&M1[i][j]);
}
}
printf("\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("Elemen M2[%d][%d]: ",i,j);
scanf("%d",&M2[i][j]);
}
}
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
M3[i][j]=M1[i][j] + M2[i][j];
}
printf("\n");
}
printf("Hasil Penjumlahan Matriks \n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("%d ",M3[i][j]);
}
printf("\n");
}
}
else if (pil==2)
{
printf("\n**************************************");
printf("\nPengurangan Matriks\n");
printf("Masukkan Baris : ");
scanf ("%d",&a);
printf("Masukkan Kolom : ");
scanf ("%d",&b);
printf("\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("Elemen M1[%d][%d]: ",i,j);
scanf("%d",&M1[i][j]);
}
}
printf("\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("Elemen M2[%d][%d]: ",i,j);
scanf("%d",&M2[i][j]);
}
}
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
M3[i][j]=M1[i][j] - M2[i][j];
}
printf("\n");
}
printf("Hasil Pengurangan Matriks \n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("%d ",M3[i][j]);
}
printf("\n");
}
}
else if (pil==3)
{
printf("\n**************************************");
printf("\nPerkalian Transpose Matriks 2x2\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Elemen M1[%d][%d]: ",i,j);
scanf("%d",&M1[i][j]);
}
}
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Elemen M2[%d][%d]: ",i,j);
scanf("%d",&M2[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
sum=0;
for(k=0;k<2;k++)
{
sum=sum+M1[i][k]* M2[k][j];
}
M3[i][j]=sum;
}
}
}
}
printf("Hasil Perkalian Matriks\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",M3[i][j]);
}
printf("\n");
}
printf("Hasil Perkalian Transpose Matriks\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",M3[j][i]);
}
printf("\n");
}
}
else if (pil==4)
{
printf("\n**************************************");
printf("\nPerkalian Transpose Matriks 3x3\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Elemen M1[%d][%d]: ",i,j);
scanf("%d",&M1[i][j]);
}
}
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Elemen M2[%d][%d]: ",i,j);
scanf("%d",&M2[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum=sum+M1[i][k]* M2[k][j];
}
M3[i][j]=sum;
}
}
}
}
printf("Hasil Perkalian Matriks\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",M3[i][j]);
}
printf("\n");
}
printf("Hasil Perkalian Transpose Matriks\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",M3[j][i]);
}
printf("\n");
}
}
getch();
}
while(pil!=5);
}
#include<conio.h>
#include<windows.h>
main()
{
int pil,a,b,i,j,sum=0,k;
int M1[50][50];
int M2[50][50];
int M3[50][50];
do
{
system ("cls");
printf("\n**************************************");
printf("\n Belajar Array dan String ");
printf("\n**************************************");
printf("\n1. Penjumlahan ");
printf("\n2. Pengurangan ");
printf("\n3. Perkalian Transpose 2x2 ");
printf("\n4. Perkalian Transpose 3x3 ");
printf("\n5. Keluar ");
printf("\n**************************************");
printf("\n\tMasukkan Pilihan [1-5]: ");
scanf("%d",&pil);
if (pil==1)
{
printf("\n**************************************");
printf("\nPenjumlahan Matriks\n");
printf("Masukkan Baris : ");
scanf ("%d",&a );
printf("Masukkan Kolom : ");
scanf ("%d",&b );
printf("\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("Elemen M1[%d][%d]: ",i,j);
scanf("%d",&M1[i][j]);
}
}
printf("\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("Elemen M2[%d][%d]: ",i,j);
scanf("%d",&M2[i][j]);
}
}
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
M3[i][j]=M1[i][j] + M2[i][j];
}
printf("\n");
}
printf("Hasil Penjumlahan Matriks \n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("%d ",M3[i][j]);
}
printf("\n");
}
}
else if (pil==2)
{
printf("\n**************************************");
printf("\nPengurangan Matriks\n");
printf("Masukkan Baris : ");
scanf ("%d",&a);
printf("Masukkan Kolom : ");
scanf ("%d",&b);
printf("\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("Elemen M1[%d][%d]: ",i,j);
scanf("%d",&M1[i][j]);
}
}
printf("\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("Elemen M2[%d][%d]: ",i,j);
scanf("%d",&M2[i][j]);
}
}
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
M3[i][j]=M1[i][j] - M2[i][j];
}
printf("\n");
}
printf("Hasil Pengurangan Matriks \n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("%d ",M3[i][j]);
}
printf("\n");
}
}
else if (pil==3)
{
printf("\n**************************************");
printf("\nPerkalian Transpose Matriks 2x2\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Elemen M1[%d][%d]: ",i,j);
scanf("%d",&M1[i][j]);
}
}
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Elemen M2[%d][%d]: ",i,j);
scanf("%d",&M2[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
sum=0;
for(k=0;k<2;k++)
{
sum=sum+M1[i][k]* M2[k][j];
}
M3[i][j]=sum;
}
}
}
}
printf("Hasil Perkalian Matriks\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",M3[i][j]);
}
printf("\n");
}
printf("Hasil Perkalian Transpose Matriks\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",M3[j][i]);
}
printf("\n");
}
}
else if (pil==4)
{
printf("\n**************************************");
printf("\nPerkalian Transpose Matriks 3x3\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Elemen M1[%d][%d]: ",i,j);
scanf("%d",&M1[i][j]);
}
}
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Elemen M2[%d][%d]: ",i,j);
scanf("%d",&M2[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum=sum+M1[i][k]* M2[k][j];
}
M3[i][j]=sum;
}
}
}
}
printf("Hasil Perkalian Matriks\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",M3[i][j]);
}
printf("\n");
}
printf("Hasil Perkalian Transpose Matriks\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",M3[j][i]);
}
printf("\n");
}
}
getch();
}
while(pil!=5);
}
7. program array and string
#include <stdio.h>
#include <conio.h>
int main()
{
int i,j;
int matrikA [2][2], matrikB [2][2], matrikHasil [100][100] ;
int b, k;
char pil;
clrscr();
printf("+=========================================+\n");
printf("| MENU UTAMA |\n");
printf("|-----------------------------------------|\n");
printf("|[1] Penjumlahan Matriks |\n");
printf("|[2] Pengurangan Matriks |\n");
printf("|[3] Perkalian Matriks Transpose Ordo 2x2|\n");
printf("|[4] Perkalian Matriks Transpose Ordo 3x3|\n");
printf("|[0] Keluar |\n\n");
printf("|-----------------------------------------|\n");
printf("+=========================================+\n");
printf("|Input pilihan (hanya angka) |: ");
gotoxy(46,12); scanf("%c",&pil);
switch (pil)
{
case '1' : clrscr();
printf("Menu Penjumlahan Matriks");
printf("\n----------\n");
printf("Masukkan jumlah baris: "); scanf("%i", &b);
printf("Masukkan jumlah kolom: "); scanf("%i", &k);
printf("\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("Masukkan Elemen A : ");
scanf("%i",&matrikA[i][j]);
}
} printf("\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("Masukkan Elemen B : ");
scanf("%d",&matrikB[i][j]);
}
}
printf("\nElemen dari Matrik A adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikA [i][j]);
}
printf("\n");
}
printf("\nElemen dari Matrik B adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikB [i][j]);
}
printf("\n");
}
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
matrikHasil [i][j]= matrikA [i][j]+ matrikB [i][j];
}
}
printf("\nMatriks Hasil Penjumlahan Adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikHasil [i][j]);
}
printf("\n");
}
break;
case '2' : clrscr();
printf("Menu Pengurangan Matriks");
printf("\n----------\n");
printf("Masukkan jumlah baris: "); scanf("%i", &b);
printf("Masukkan jumlah kolom: "); scanf("%i", &k);
printf("\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("Masukkan Elemen A : ");
scanf("%i",&matrikA[i][j]);
}
} printf("\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("Masukkan Elemen B : ");
scanf("%d",&matrikB[i][j]);
}
}
printf("\nElemen dari Matrik A adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikA [i][j]);
}
printf("\n");
}
printf("\nElemen dari Matrik B adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikB [i][j]);
}
printf("\n");
}
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
matrikHasil [i][j]= matrikA [i][j]- matrikB [i][j];
}
}
printf("\nMatriks Hasil Pengurangan Adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikHasil [i][j]);
}
printf("\n");
}
break;
case '3' : clrscr();
printf("Menu Perkalian Matriks");
printf("\n----------\n");
printf("Masukkan jumlah baris: "); scanf("%i", &b);
printf("Masukkan jumlah kolom: "); scanf("%i", &k);
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Masukkan Elemen A : ");
scanf("%i",&matrikA[i][j]);
}
} printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Masukkan Elemen B : ");
scanf("%d",&matrikB[i][j]);
}
}
printf("\nElemen dari Matrik A adalah :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",matrikA [i][j]);
}
printf("\n");
}
printf("\nElemen dari Matrik B adalah :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",matrikB [i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
matrikHasil[i][j] = 0;
for(k=0;k<2;k++)
matrikHasil[i][j] = matrikHasil[i][j] + (matrikA [i][k]* matrikB [k][j]);
}
}
printf("\nMatriks Hasil Perkalian Adalah :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",matrikHasil [i][j]);
}
printf("\n");
}
printf("\nMatriks Transpose Perkalian Adalah :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",matrikHasil [j][i]);
}
printf("\n");
}
break;
case '4' : clrscr();
printf("Menu Perkalian Matriks");
printf("\n----------\n");
printf("Masukkan jumlah baris: "); scanf("%i", &b);
printf("Masukkan jumlah kolom: "); scanf("%i", &k);
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Masukkan Elemen A : ");
scanf("%i",&matrikA[i][j]);
}
} printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Masukkan Elemen B : ");
scanf("%d",&matrikB[i][j]);
}
}
printf("\nElemen dari Matrik A adalah :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikA [i][j]);
}
printf("\n");
}
printf("\nElemen dari Matrik B adalah :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikB [i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
matrikHasil[i][j] = 0;
for(k=0;k<3;k++)
matrikHasil[i][j] = matrikHasil[i][j] + (matrikA [i][k]* matrikB [k][j]);
}
}
printf("\nMatriks Hasil Perkalian Adalah :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikHasil [i][j]);
}
printf("\n");
}
printf("\nMatriks Transpose Perkalian Adalah :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikHasil [j][i]);
}
printf("\n");
}
}
getch();
}
#include <conio.h>
int main()
{
int i,j;
int matrikA [2][2], matrikB [2][2], matrikHasil [100][100] ;
int b, k;
char pil;
clrscr();
printf("+=========================================+\n");
printf("| MENU UTAMA |\n");
printf("|-----------------------------------------|\n");
printf("|[1] Penjumlahan Matriks |\n");
printf("|[2] Pengurangan Matriks |\n");
printf("|[3] Perkalian Matriks Transpose Ordo 2x2|\n");
printf("|[4] Perkalian Matriks Transpose Ordo 3x3|\n");
printf("|[0] Keluar |\n\n");
printf("|-----------------------------------------|\n");
printf("+=========================================+\n");
printf("|Input pilihan (hanya angka) |: ");
gotoxy(46,12); scanf("%c",&pil);
switch (pil)
{
case '1' : clrscr();
printf("Menu Penjumlahan Matriks");
printf("\n----------\n");
printf("Masukkan jumlah baris: "); scanf("%i", &b);
printf("Masukkan jumlah kolom: "); scanf("%i", &k);
printf("\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("Masukkan Elemen A : ");
scanf("%i",&matrikA[i][j]);
}
} printf("\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("Masukkan Elemen B : ");
scanf("%d",&matrikB[i][j]);
}
}
printf("\nElemen dari Matrik A adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikA [i][j]);
}
printf("\n");
}
printf("\nElemen dari Matrik B adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikB [i][j]);
}
printf("\n");
}
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
matrikHasil [i][j]= matrikA [i][j]+ matrikB [i][j];
}
}
printf("\nMatriks Hasil Penjumlahan Adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikHasil [i][j]);
}
printf("\n");
}
break;
case '2' : clrscr();
printf("Menu Pengurangan Matriks");
printf("\n----------\n");
printf("Masukkan jumlah baris: "); scanf("%i", &b);
printf("Masukkan jumlah kolom: "); scanf("%i", &k);
printf("\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("Masukkan Elemen A : ");
scanf("%i",&matrikA[i][j]);
}
} printf("\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("Masukkan Elemen B : ");
scanf("%d",&matrikB[i][j]);
}
}
printf("\nElemen dari Matrik A adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikA [i][j]);
}
printf("\n");
}
printf("\nElemen dari Matrik B adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikB [i][j]);
}
printf("\n");
}
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
matrikHasil [i][j]= matrikA [i][j]- matrikB [i][j];
}
}
printf("\nMatriks Hasil Pengurangan Adalah :\n");
for(i=0;i<b;i++)
{
for(j=0;j<k;j++)
{
printf("%d\t",matrikHasil [i][j]);
}
printf("\n");
}
break;
case '3' : clrscr();
printf("Menu Perkalian Matriks");
printf("\n----------\n");
printf("Masukkan jumlah baris: "); scanf("%i", &b);
printf("Masukkan jumlah kolom: "); scanf("%i", &k);
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Masukkan Elemen A : ");
scanf("%i",&matrikA[i][j]);
}
} printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Masukkan Elemen B : ");
scanf("%d",&matrikB[i][j]);
}
}
printf("\nElemen dari Matrik A adalah :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",matrikA [i][j]);
}
printf("\n");
}
printf("\nElemen dari Matrik B adalah :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",matrikB [i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
matrikHasil[i][j] = 0;
for(k=0;k<2;k++)
matrikHasil[i][j] = matrikHasil[i][j] + (matrikA [i][k]* matrikB [k][j]);
}
}
printf("\nMatriks Hasil Perkalian Adalah :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",matrikHasil [i][j]);
}
printf("\n");
}
printf("\nMatriks Transpose Perkalian Adalah :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",matrikHasil [j][i]);
}
printf("\n");
}
break;
case '4' : clrscr();
printf("Menu Perkalian Matriks");
printf("\n----------\n");
printf("Masukkan jumlah baris: "); scanf("%i", &b);
printf("Masukkan jumlah kolom: "); scanf("%i", &k);
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Masukkan Elemen A : ");
scanf("%i",&matrikA[i][j]);
}
} printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Masukkan Elemen B : ");
scanf("%d",&matrikB[i][j]);
}
}
printf("\nElemen dari Matrik A adalah :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikA [i][j]);
}
printf("\n");
}
printf("\nElemen dari Matrik B adalah :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikB [i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
matrikHasil[i][j] = 0;
for(k=0;k<3;k++)
matrikHasil[i][j] = matrikHasil[i][j] + (matrikA [i][k]* matrikB [k][j]);
}
}
printf("\nMatriks Hasil Perkalian Adalah :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikHasil [i][j]);
}
printf("\n");
}
printf("\nMatriks Transpose Perkalian Adalah :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrikHasil [j][i]);
}
printf("\n");
}
}
getch();
}
8. perkalian matriks
#include <stdio.h>
#include <conio.h>
int main()
{
int m1[100][100], m2[100][100], m3[100][100], b, k, BB, BK, i, sum = 0, kk;
/*Menginputkan jumlah dan baris matrik*/
printf("-----------Buat Matrik----------\n");
printf("\n");
printf("Masukkan berapa besar Baris Matrik : ");
scanf("%d", &b);
printf("Masukkan berapa besar Kolom Matrik : ");
scanf("%d", &k);
/*Input bilangan baris dan kolom*/
for(i=1; i<3; i++)
{
printf("\n---------Buat Matrik ke : %d-----------\n", i);
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
if(i == 1)
{
printf("Bilangan baris ke %d kolom ke %d : ", BB+1, BK+1);
scanf("%d", &m1[BB][BK]);
}
else
{
printf("Bilangan baris ke %d kolom ke %d : ", BB+1, BK+1);
scanf("%d", &m2[BB][BK]);
}
}
}
}
/*Bentuk matrik yang dibentuk*/
printf("\nGambar Matrik\n");
for(i=1; i<3; i++)
{
printf("Matrik ke %d\n", i);
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
if(i == 1)
{
printf(" %d ",m1[BB][BK]);
}
else
{
printf(" %d ",m2[BB][BK]);
}
}
printf("\n");
}
printf("\n\n");
}
/*Proses perkalian matrik*/
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
sum = 0;
for(kk=0; kk<k; kk++)
{
sum = sum + m1[BB][kk] * m2[kk][BK];
}
m3[BB][BK] = sum;
}
}
}
}
/*Hasil perkalian matrik*/
printf("Hasil Perkalian Matrik\n");
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
printf(" %d ", m3[BB][BK]);
}
printf("\n");
}
getch();
}
#include <conio.h>
int main()
{
int m1[100][100], m2[100][100], m3[100][100], b, k, BB, BK, i, sum = 0, kk;
/*Menginputkan jumlah dan baris matrik*/
printf("-----------Buat Matrik----------\n");
printf("\n");
printf("Masukkan berapa besar Baris Matrik : ");
scanf("%d", &b);
printf("Masukkan berapa besar Kolom Matrik : ");
scanf("%d", &k);
/*Input bilangan baris dan kolom*/
for(i=1; i<3; i++)
{
printf("\n---------Buat Matrik ke : %d-----------\n", i);
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
if(i == 1)
{
printf("Bilangan baris ke %d kolom ke %d : ", BB+1, BK+1);
scanf("%d", &m1[BB][BK]);
}
else
{
printf("Bilangan baris ke %d kolom ke %d : ", BB+1, BK+1);
scanf("%d", &m2[BB][BK]);
}
}
}
}
/*Bentuk matrik yang dibentuk*/
printf("\nGambar Matrik\n");
for(i=1; i<3; i++)
{
printf("Matrik ke %d\n", i);
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
if(i == 1)
{
printf(" %d ",m1[BB][BK]);
}
else
{
printf(" %d ",m2[BB][BK]);
}
}
printf("\n");
}
printf("\n\n");
}
/*Proses perkalian matrik*/
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
sum = 0;
for(kk=0; kk<k; kk++)
{
sum = sum + m1[BB][kk] * m2[kk][BK];
}
m3[BB][BK] = sum;
}
}
}
}
/*Hasil perkalian matrik*/
printf("Hasil Perkalian Matrik\n");
for(BB=0; BB<b; BB++)
{
for(BK=0; BK<k; BK++)
{
printf(" %d ", m3[BB][BK]);
}
printf("\n");
}
getch();
}
9. matriks kali, tambah, kurang, bagi
#include <stdio.h>
#include <stdlib.h>
//Ini adalah baris deklarasi array 2 dimensi
float entryA[2][2],entryB[2][2],varC[2][2];
int a,b,c;
void pilihan(){ //fungsi pilihan digunakan untuk memilih menu penghitungan program
printf("\n[1] Penjumlahan");
printf("\n[2] Pengurangan");
printf("\n[3] Perkalian");
printf("\n[4] Pembagian");
printf("\n[0] Keluar");
}
void Input(){ //fungsi input digunakan untuk menyimpan inputan dari user
int i,j;
printf("\n----Matriks A----\n");
for (i=0; i<2; i++){ //pengulangan untuk matrik A dengan jumlah perulangan < 2
for (j=0; j<2; j++){
printf("Input Matrix A[%d][%d] : ",i, j);
scanf("%f",&entryA[i][j]);
}
}
printf("\n----Matriks B----\n");
//perulangan matriks B
for (i=0; i<2; i++){
for (j=0; j<2; j++){
printf("Input Matrix B[%d][%d] : ",i, j);
scanf("%f",&entryB[i][j]); //menyimpan inputan dari user dari hasil pengulangan ke dalam variabel
}
}
}
//menunjukkan jenis operasi dalam matriks misal menunjukkan operasi penjumlahan
void show(char opr){
for (a=0; a<2; a++){ //menampilkan nilai matrik A yang tadi diinputkan
for (b=0; b<2; b++){
printf("%2.0f ",entryA[a][b]);
}
if(a==0) printf("\t%c\t",opr);
else printf("\t \t");
for (b=0; b<2; b++){
printf("%2.0f ",entryB[a][b]); //menampilkan nilai matriks B dari inputan user ke variabel
}
printf("\n");
}
}
void penjumlahan(){ //memanggil fungsi penjumlahan
Input();//memanggil fungsi input
show('+');//memanggil fungsi show jenis operasi + /jumlah
printf("Hasil Penjumlahan:\n");
for (a=0; a<2; a++){
for (b=0; b<2; b++){
printf("%2.0f ",entryA[a][b]+entryB[a][b]);
}
printf("\n");
}
}
void pengurangan(){//fungsi pengurangan
Input(); //memanggil fungsi show jenis operasi (–) minus/kurang
show('-');
printf("Hasil Pengurangan:\n");
for (a=0; a<2; a++){
for (b=0; b<2; b++){
printf("%2.0f ",entryA[a][b]-entryB[a][b]);
}
printf("\n");
}
}
void perkalian(){ //fungsi perkalian
Input();//memanggil fungsi input
//memanggil fungsi show jenis operasi perkalian dan mencetak karakter “x”
show('x');
printf("Hasil Perkalian:\n");
for (a=0; a<2; a++){
for (b=0; b<2; b++){
varC[a][b]=0;
for (c=0; c<2; c++){
varC[a][b]+=entryA[a][c]*entryB[c][b]; /*artinya adalah
varC[a][b]= varC[a][b]+(entryA[a][c]*entryB[c][b]) */
}
printf("%2.0f ",varC[a][b]);
}
printf("\n");
}
}
void pembagian(){//fungsi pembagian
float detB,temp,varBinvers[2][2];
Input();
show(':');
detB = 1/((entryB[0][0]*entryB[1][1])-(entryB[0][1]*entryB[1][0]));
varBinvers[0][0] = detB*entryB[1][1];
varBinvers[0][1] = detB*(-entryB[0][1]);
varBinvers[1][0] = detB*(-entryB[1][0]);
varBinvers[1][1] = detB*entryB[0][0];
for (a=0; a<2; a++){
for (b=0; b<2; b++){
printf("%2.0f ",entryA[a][b]);
}
if(a==0) printf("\tx\t");
else printf("\t \t");
for (b=0; b<2; b++){
printf("%6.2f ",varBinvers[a][b]);
}
printf("\n");
}
printf("Hasil Pembagian:\n");
for (a=0; a<2; a++){
for (b=0; b<2; b++){
varC[a][b]=0;
for (c=0; c<2; c++){
varC[a][b]+=entryA[a][c]*varBinvers[c][b];
}
printf("%6.2f ",varC[a][b]);
}
printf("\n");
}
}
int main(void){ //fungsi utama dari main
int selesai=1,pilih; //program akan berlanjut jika variabel selesai bernilai 1
printf("\n-----------PROGRAM PENGHITUNGAN MATRIKS 2 ORDO------------\n\n");
while(selesai==1){
pilihan();
printf("\nMasukkan pilihan >");
scanf("%d",&pilih);
switch(pilih){
case 0 : selesai=0; break; //dan program akan selesai jika bernilai/input angka 0
case 1 : penjumlahan(); break;
case 2 : pengurangan(); break;
case 3 : perkalian(); break;
case 4 : pembagian(); break;
default : printf("Pilihan Salah\n"); break;
}
}
system("pause");
return 0;
}
#include <stdlib.h>
//Ini adalah baris deklarasi array 2 dimensi
float entryA[2][2],entryB[2][2],varC[2][2];
int a,b,c;
void pilihan(){ //fungsi pilihan digunakan untuk memilih menu penghitungan program
printf("\n[1] Penjumlahan");
printf("\n[2] Pengurangan");
printf("\n[3] Perkalian");
printf("\n[4] Pembagian");
printf("\n[0] Keluar");
}
void Input(){ //fungsi input digunakan untuk menyimpan inputan dari user
int i,j;
printf("\n----Matriks A----\n");
for (i=0; i<2; i++){ //pengulangan untuk matrik A dengan jumlah perulangan < 2
for (j=0; j<2; j++){
printf("Input Matrix A[%d][%d] : ",i, j);
scanf("%f",&entryA[i][j]);
}
}
printf("\n----Matriks B----\n");
//perulangan matriks B
for (i=0; i<2; i++){
for (j=0; j<2; j++){
printf("Input Matrix B[%d][%d] : ",i, j);
scanf("%f",&entryB[i][j]); //menyimpan inputan dari user dari hasil pengulangan ke dalam variabel
}
}
}
//menunjukkan jenis operasi dalam matriks misal menunjukkan operasi penjumlahan
void show(char opr){
for (a=0; a<2; a++){ //menampilkan nilai matrik A yang tadi diinputkan
for (b=0; b<2; b++){
printf("%2.0f ",entryA[a][b]);
}
if(a==0) printf("\t%c\t",opr);
else printf("\t \t");
for (b=0; b<2; b++){
printf("%2.0f ",entryB[a][b]); //menampilkan nilai matriks B dari inputan user ke variabel
}
printf("\n");
}
}
void penjumlahan(){ //memanggil fungsi penjumlahan
Input();//memanggil fungsi input
show('+');//memanggil fungsi show jenis operasi + /jumlah
printf("Hasil Penjumlahan:\n");
for (a=0; a<2; a++){
for (b=0; b<2; b++){
printf("%2.0f ",entryA[a][b]+entryB[a][b]);
}
printf("\n");
}
}
void pengurangan(){//fungsi pengurangan
Input(); //memanggil fungsi show jenis operasi (–) minus/kurang
show('-');
printf("Hasil Pengurangan:\n");
for (a=0; a<2; a++){
for (b=0; b<2; b++){
printf("%2.0f ",entryA[a][b]-entryB[a][b]);
}
printf("\n");
}
}
void perkalian(){ //fungsi perkalian
Input();//memanggil fungsi input
//memanggil fungsi show jenis operasi perkalian dan mencetak karakter “x”
show('x');
printf("Hasil Perkalian:\n");
for (a=0; a<2; a++){
for (b=0; b<2; b++){
varC[a][b]=0;
for (c=0; c<2; c++){
varC[a][b]+=entryA[a][c]*entryB[c][b]; /*artinya adalah
varC[a][b]= varC[a][b]+(entryA[a][c]*entryB[c][b]) */
}
printf("%2.0f ",varC[a][b]);
}
printf("\n");
}
}
void pembagian(){//fungsi pembagian
float detB,temp,varBinvers[2][2];
Input();
show(':');
detB = 1/((entryB[0][0]*entryB[1][1])-(entryB[0][1]*entryB[1][0]));
varBinvers[0][0] = detB*entryB[1][1];
varBinvers[0][1] = detB*(-entryB[0][1]);
varBinvers[1][0] = detB*(-entryB[1][0]);
varBinvers[1][1] = detB*entryB[0][0];
for (a=0; a<2; a++){
for (b=0; b<2; b++){
printf("%2.0f ",entryA[a][b]);
}
if(a==0) printf("\tx\t");
else printf("\t \t");
for (b=0; b<2; b++){
printf("%6.2f ",varBinvers[a][b]);
}
printf("\n");
}
printf("Hasil Pembagian:\n");
for (a=0; a<2; a++){
for (b=0; b<2; b++){
varC[a][b]=0;
for (c=0; c<2; c++){
varC[a][b]+=entryA[a][c]*varBinvers[c][b];
}
printf("%6.2f ",varC[a][b]);
}
printf("\n");
}
}
int main(void){ //fungsi utama dari main
int selesai=1,pilih; //program akan berlanjut jika variabel selesai bernilai 1
printf("\n-----------PROGRAM PENGHITUNGAN MATRIKS 2 ORDO------------\n\n");
while(selesai==1){
pilihan();
printf("\nMasukkan pilihan >");
scanf("%d",&pilih);
switch(pilih){
case 0 : selesai=0; break; //dan program akan selesai jika bernilai/input angka 0
case 1 : penjumlahan(); break;
case 2 : pengurangan(); break;
case 3 : perkalian(); break;
case 4 : pembagian(); break;
default : printf("Pilihan Salah\n"); break;
}
}
system("pause");
return 0;
}
10. matriks 2x2
#include <stdio.h>
#include <conio.h>
void main()
{
int A[3][4], B[3][4], X[3][4], i, j;
clrscr();
printf ("Perkalian Matriks (Ordo 2x2)\n\n");
printf ("----------------------------\n\n");
/******* Masukkan matriks A *******/
printf("Penetapan Nilai Matriks A\n");
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Baris ke - %i, Kolom ke - %i : ",i+1,j+1);
fflush(stdin);scanf("%i",&A[i][j]);
printf("\n");
}
}
printf("\n");
/******** Masukkan matriks B ********/
printf("Penetapan Nilai Matriks B\n");
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Baris ke - %i, Kolom ke - %i : ",i+1,j+1);
fflush(stdin);scanf("%i",&B[i][j]);
printf("\n");
}
}
/******** Proses perkalian matriks A dan B ********/
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
X[i][j]=(A[i][0]*B[0][j])+(A[i][1]*B[1][j]);
}
}
/******** Cetak hasil perkalian matriks A dan B *******/
printf("\nHasil perkalian matriks A dan B\n");
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%6i",X[i][j]);printf("\n");
}
printf("\n\n");
getch();
}
#include <conio.h>
void main()
{
int A[3][4], B[3][4], X[3][4], i, j;
clrscr();
printf ("Perkalian Matriks (Ordo 2x2)\n\n");
printf ("----------------------------\n\n");
/******* Masukkan matriks A *******/
printf("Penetapan Nilai Matriks A\n");
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Baris ke - %i, Kolom ke - %i : ",i+1,j+1);
fflush(stdin);scanf("%i",&A[i][j]);
printf("\n");
}
}
printf("\n");
/******** Masukkan matriks B ********/
printf("Penetapan Nilai Matriks B\n");
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Baris ke - %i, Kolom ke - %i : ",i+1,j+1);
fflush(stdin);scanf("%i",&B[i][j]);
printf("\n");
}
}
/******** Proses perkalian matriks A dan B ********/
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
X[i][j]=(A[i][0]*B[0][j])+(A[i][1]*B[1][j]);
}
}
/******** Cetak hasil perkalian matriks A dan B *******/
printf("\nHasil perkalian matriks A dan B\n");
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%6i",X[i][j]);printf("\n");
}
printf("\n\n");
getch();
}
11. matriks transpose 2x2
# include <stdio.h>
# include <conio.h>
void input_matriks(int matriks_a[][2], int matriks_b[][2]) {
int i,j;
printf(" Matriks A : \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
scanf("%d", &matriks_a[i][j]);
}
printf("\nMatriks B : \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
scanf("%d", &matriks_b[i][j]);
}
}
void kali_matriks(int matriks_a[][2], int matriks_b[][2], int matriks_c[][2]) {
int i,j,k;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
matriks_c[i][j] = 0;
for (k = 0; k < 2; k++)
matriks_c[i][j] += matriks_a[i][k] * matriks_b[k][j];
}
}
}
void transpos_matriks(int matriks_c[][2], int trans[][2]) {
int i,j;
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
trans[j][i] = matriks_c[i][j];
}
int main() {
int matriks_a[2][2], matriks_b[2][2], matriks_c[2][2], trans[2][2];
int i,j;
printf(" —– Program Perkalian dan Transpose Matriks (2*2) —–\n\n");
input_matriks(matriks_a, matriks_b);
kali_matriks(matriks_a, matriks_b, matriks_c);
transpos_matriks(matriks_c, trans);
printf("\nHasil Perkalian Matrik A x B = (Matrik C) : \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%2d", matriks_c[i][j]);
printf("\n");
}
printf("\nHasil Transpose matrik C adalah: \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%2d", trans[i][j]);
printf("\n");
}
getch();
}
# include <conio.h>
void input_matriks(int matriks_a[][2], int matriks_b[][2]) {
int i,j;
printf(" Matriks A : \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
scanf("%d", &matriks_a[i][j]);
}
printf("\nMatriks B : \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
scanf("%d", &matriks_b[i][j]);
}
}
void kali_matriks(int matriks_a[][2], int matriks_b[][2], int matriks_c[][2]) {
int i,j,k;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
matriks_c[i][j] = 0;
for (k = 0; k < 2; k++)
matriks_c[i][j] += matriks_a[i][k] * matriks_b[k][j];
}
}
}
void transpos_matriks(int matriks_c[][2], int trans[][2]) {
int i,j;
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
trans[j][i] = matriks_c[i][j];
}
int main() {
int matriks_a[2][2], matriks_b[2][2], matriks_c[2][2], trans[2][2];
int i,j;
printf(" —– Program Perkalian dan Transpose Matriks (2*2) —–\n\n");
input_matriks(matriks_a, matriks_b);
kali_matriks(matriks_a, matriks_b, matriks_c);
transpos_matriks(matriks_c, trans);
printf("\nHasil Perkalian Matrik A x B = (Matrik C) : \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%2d", matriks_c[i][j]);
printf("\n");
}
printf("\nHasil Transpose matrik C adalah: \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%2d", trans[i][j]);
printf("\n");
}
getch();
}
12. matriks transpose 3x3
# include <stdio.h>
# include <conio.h>
void input_matriks(int matriks_a[][3], int matriks_b[][3]) {
int i,j;
printf(" Matriks A : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
scanf("%d", &matriks_a[i][j]);
}
printf("\nMatriks B : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
scanf("%d", &matriks_b[i][j]);
}
}
void kali_matriks(int matriks_a[][3], int matriks_b[][3], int matriks_c[][3]) {
int i,j,k;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
matriks_c[i][j] = 0;
for (k = 0; k < 3; k++)
matriks_c[i][j] += matriks_a[i][k] * matriks_b[k][j];
}
}
}
void transpos_matriks(int matriks_c[][3], int trans[][3]) {
int i,j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
trans[j][i] = matriks_c[i][j];
}
int main() {
int matriks_a[3][3], matriks_b[3][3], matriks_c[3][3], trans[3][3];
int i,j;
printf(" —– Program Perkalian dan Transpose Matriks (3*3) —–\n\n");
input_matriks(matriks_a, matriks_b);
kali_matriks(matriks_a, matriks_b, matriks_c);
transpos_matriks(matriks_c, trans);
printf("\nHasil Perkalian Matrik A x B = (Matrik C) : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%3d", matriks_c[i][j]);
printf("\n");
}
printf("\nHasil Transpose matrik C adalah: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%3d", trans[i][j]);
printf("\n");
}
getch();
}
# include <conio.h>
void input_matriks(int matriks_a[][3], int matriks_b[][3]) {
int i,j;
printf(" Matriks A : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
scanf("%d", &matriks_a[i][j]);
}
printf("\nMatriks B : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
scanf("%d", &matriks_b[i][j]);
}
}
void kali_matriks(int matriks_a[][3], int matriks_b[][3], int matriks_c[][3]) {
int i,j,k;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
matriks_c[i][j] = 0;
for (k = 0; k < 3; k++)
matriks_c[i][j] += matriks_a[i][k] * matriks_b[k][j];
}
}
}
void transpos_matriks(int matriks_c[][3], int trans[][3]) {
int i,j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
trans[j][i] = matriks_c[i][j];
}
int main() {
int matriks_a[3][3], matriks_b[3][3], matriks_c[3][3], trans[3][3];
int i,j;
printf(" —– Program Perkalian dan Transpose Matriks (3*3) —–\n\n");
input_matriks(matriks_a, matriks_b);
kali_matriks(matriks_a, matriks_b, matriks_c);
transpos_matriks(matriks_c, trans);
printf("\nHasil Perkalian Matrik A x B = (Matrik C) : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%3d", matriks_c[i][j]);
printf("\n");
}
printf("\nHasil Transpose matrik C adalah: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
printf("%3d", trans[i][j]);
printf("\n");
}
getch();
}
13. function matriks array menggunakan void, dan non void
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int i,j,k,pilihan;
int ar1[3][3], ar2[3][3], ar3[3][3],det,ari[3][3];
char op;
void menu()
{
printf(" MENU UTAMA PROGRAM MATRIKS MENGGUNAKAN FUNCTION\n");
printf(" SITI MULIA SARI J1F115017 \n\n");
printf("[1] Menu Penjumlahan Matriks\n");
printf("[2] Menu Pengurangan Matriks\n");
printf("[3] Menu Perkalian Matriks\n");
printf("[4] Keluar\n");
printf("\n");
printf("Masukkan Pilihan : ");scanf("%d",&pilihan);
printf("\n");
}
void input ()
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf("A%d%d = ",i+1,j+1);scanf("%d",&ar1[i][j]);
}
printf("\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf("B%d%d = ",i+1,j+1);scanf("%d",&ar2[i][j]);
}
}
void hasil()
{
printf("\n");
printf("%d %d %d \t%d %d %d \t%d %d %d\n",ar1[0][0],ar1[0][1],ar1[0][2],ar2[1][0],ar2[1][1],ar2[1][2],ar3[2][0],ar3[2][1],ar3[2][2]);
printf("%d %d %d \t%d %d %d \t%d %d %d\n",ar1[0][0],ar1[0][1],ar1[0][2],ar2[1][0],ar2[1][1],ar2[1][2],ar3[2][0],ar3[2][1],ar3[2][2]);
printf("%d %d %d \t%d %d %d \t%d %d %d\n",ar1[0][1],ar1[0][1],ar1[0][2],ar2[1][0],ar2[1][1],ar2[1][2],ar3[2][0],ar3[2][1],ar3[2][2]);
printf("\n");
getch();
}
void jumlah(int ar1[3][3],int ar2[3][3], int ar3[3][3])
{
int i,j;
input();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
ar3[i][j] = ar1[i][j] + ar2[i][j];
op = '+';
hasil();
}
void kurang(int ar1[3][3],int ar2[3][3], int ar3[3][3])
{
int i,j;
input();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
ar3[i][j] = ar1[i][j] - ar2[i][j];
op = '-';
hasil();
}
void kali(int ar1[3][3],int ar2[3][3], int ar3[3][3])
{
int i,j,k;
input();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
ar3[i][j] = 0;
for(k=0;k<3;k++)
ar3[i][j] = ar3[i][j] +(ar1[i][k] * ar2[k][j]);
}
op = '*';
hasil();
}
int main()
{
do
{
clrscr();
menu ();
switch(pilihan)
{
case 1:{
jumlah(ar1,ar2,ar3);
break;
}
case 2:{
kurang(ar1,ar2,ar3);
break;
}
case 3:{
kali(ar1,ar2,ar3);
break;
}
default: {printf ("pilihan salah"); getch();}
}
printf("\n");
}
while (pilihan!=4);
}
#include <conio.h>
#include <stdlib.h>
int i,j,k,pilihan;
int ar1[3][3], ar2[3][3], ar3[3][3],det,ari[3][3];
char op;
void menu()
{
printf(" MENU UTAMA PROGRAM MATRIKS MENGGUNAKAN FUNCTION\n");
printf(" SITI MULIA SARI J1F115017 \n\n");
printf("[1] Menu Penjumlahan Matriks\n");
printf("[2] Menu Pengurangan Matriks\n");
printf("[3] Menu Perkalian Matriks\n");
printf("[4] Keluar\n");
printf("\n");
printf("Masukkan Pilihan : ");scanf("%d",&pilihan);
printf("\n");
}
void input ()
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf("A%d%d = ",i+1,j+1);scanf("%d",&ar1[i][j]);
}
printf("\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf("B%d%d = ",i+1,j+1);scanf("%d",&ar2[i][j]);
}
}
void hasil()
{
printf("\n");
printf("%d %d %d \t%d %d %d \t%d %d %d\n",ar1[0][0],ar1[0][1],ar1[0][2],ar2[1][0],ar2[1][1],ar2[1][2],ar3[2][0],ar3[2][1],ar3[2][2]);
printf("%d %d %d \t%d %d %d \t%d %d %d\n",ar1[0][0],ar1[0][1],ar1[0][2],ar2[1][0],ar2[1][1],ar2[1][2],ar3[2][0],ar3[2][1],ar3[2][2]);
printf("%d %d %d \t%d %d %d \t%d %d %d\n",ar1[0][1],ar1[0][1],ar1[0][2],ar2[1][0],ar2[1][1],ar2[1][2],ar3[2][0],ar3[2][1],ar3[2][2]);
printf("\n");
getch();
}
void jumlah(int ar1[3][3],int ar2[3][3], int ar3[3][3])
{
int i,j;
input();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
ar3[i][j] = ar1[i][j] + ar2[i][j];
op = '+';
hasil();
}
void kurang(int ar1[3][3],int ar2[3][3], int ar3[3][3])
{
int i,j;
input();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
ar3[i][j] = ar1[i][j] - ar2[i][j];
op = '-';
hasil();
}
void kali(int ar1[3][3],int ar2[3][3], int ar3[3][3])
{
int i,j,k;
input();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
ar3[i][j] = 0;
for(k=0;k<3;k++)
ar3[i][j] = ar3[i][j] +(ar1[i][k] * ar2[k][j]);
}
op = '*';
hasil();
}
int main()
{
do
{
clrscr();
menu ();
switch(pilihan)
{
case 1:{
jumlah(ar1,ar2,ar3);
break;
}
case 2:{
kurang(ar1,ar2,ar3);
break;
}
case 3:{
kali(ar1,ar2,ar3);
break;
}
default: {printf ("pilihan salah"); getch();}
}
printf("\n");
}
while (pilihan!=4);
}
14. bilangan fibonacci dan luas segitiga
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,c,p,l,t,keliling,jumlah,pil,pil2;
float luas;
printf("+====================================+\n");
printf("| MENU UTAMA |\n");
printf("|------------------------------------|\n");
printf("| 1. Menghitung Bilangan Fibonacci |\n");
printf("| 2. Menghitung Luas Segitiga |\n");
printf("| 3. Keluar |\n");
printf("|------------------------------------|\n");
printf("| Masukkan pilihan (1-3) : |\n");
printf("+====================================+\n");
gotoxy(28,8); scanf("%d",&pil);
switch (pil)
{
case 1 : {
clrscr();
a=0;
b=1;
c=0;
printf("Algoritma Untuk Menampilkan Deret Fibonacci\n");
printf("===========================================\n");
printf("Masukkan Jumlah deret : ");
scanf ("%d",&jumlah);
printf("\n\nDeretnya adalah sebagai berikut :\n");
printf("============================================\n");
for (int i=1;i<=jumlah;i++)
{
c=a+b;
b=a;
a=c;
printf("%d ",c);
}
break;
}
case 2 : clrscr();
printf("Menu Segitiga");
printf("\n----------\n");
printf("1. Menghitung Luas\n");
printf("2. Menghitung Keliling\n\n");
printf("Masukkan pilihan : ");scanf("%d",&pil2);
if (pil2==1)
{
clrscr();
printf("Menghitung Luas Segitiga\n");
printf("\n-------------------------\n");
printf("alas :");scanf("%d",&a);
printf("tinggi : ");scanf("%d",&t);
luas=0.5*a*t;
printf("Luas : %g",luas);
}
else if (pil2==2)
{
clrscr();
printf("Menghitung Keliling Segitiga\n");
printf("----------------------------\n");
printf("panjang: ");scanf("%d",&p);
printf("alas: ");scanf("%d",&a);
printf("tinggi: ");scanf("%d",&t);
keliling=p+a+t;
printf("\nKeliling : %d",keliling);
}
break;
case 3 : printf("\n\n\n\nTERIMAKASIH");
break;
default: printf("\n\n\n\nPilihan yang anda masukan salah !!!\n\n");
}
getch();
}
#include <conio.h>
main()
{
int a,b,c,p,l,t,keliling,jumlah,pil,pil2;
float luas;
printf("+====================================+\n");
printf("| MENU UTAMA |\n");
printf("|------------------------------------|\n");
printf("| 1. Menghitung Bilangan Fibonacci |\n");
printf("| 2. Menghitung Luas Segitiga |\n");
printf("| 3. Keluar |\n");
printf("|------------------------------------|\n");
printf("| Masukkan pilihan (1-3) : |\n");
printf("+====================================+\n");
gotoxy(28,8); scanf("%d",&pil);
switch (pil)
{
case 1 : {
clrscr();
a=0;
b=1;
c=0;
printf("Algoritma Untuk Menampilkan Deret Fibonacci\n");
printf("===========================================\n");
printf("Masukkan Jumlah deret : ");
scanf ("%d",&jumlah);
printf("\n\nDeretnya adalah sebagai berikut :\n");
printf("============================================\n");
for (int i=1;i<=jumlah;i++)
{
c=a+b;
b=a;
a=c;
printf("%d ",c);
}
break;
}
case 2 : clrscr();
printf("Menu Segitiga");
printf("\n----------\n");
printf("1. Menghitung Luas\n");
printf("2. Menghitung Keliling\n\n");
printf("Masukkan pilihan : ");scanf("%d",&pil2);
if (pil2==1)
{
clrscr();
printf("Menghitung Luas Segitiga\n");
printf("\n-------------------------\n");
printf("alas :");scanf("%d",&a);
printf("tinggi : ");scanf("%d",&t);
luas=0.5*a*t;
printf("Luas : %g",luas);
}
else if (pil2==2)
{
clrscr();
printf("Menghitung Keliling Segitiga\n");
printf("----------------------------\n");
printf("panjang: ");scanf("%d",&p);
printf("alas: ");scanf("%d",&a);
printf("tinggi: ");scanf("%d",&t);
keliling=p+a+t;
printf("\nKeliling : %d",keliling);
}
break;
case 3 : printf("\n\n\n\nTERIMAKASIH");
break;
default: printf("\n\n\n\nPilihan yang anda masukan salah !!!\n\n");
}
getch();
}
15. bilangan fibonacci dan bilangan prima
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,c,bilprima,jumlah,pil,pil2;
printf("+====================================+\n");
printf("| MENU UTAMA |\n");
printf("|------------------------------------|\n");
printf("| 1. Bilangan Fibonacci |\n");
printf("| 2. Bilangan Prima |\n");
printf("| 3. Keluar |\n");
printf("|------------------------------------|\n");
printf("| Masukkan pilihan (1-3) : |\n");
printf("+====================================+\n");
gotoxy(28,8); scanf("%d",&pil);
switch (pil)
{
case 1 : {
clrscr();
a=0;
b=1;
c=0;
printf("Algoritma Untuk Menampilkan Deret Fibonacci\n");
printf("===========================================\n");
printf("Masukkan Jumlah deret : ");
scanf ("%d",&jumlah);
printf("\n\nDeretnya adalah sebagai berikut :\n");
printf("===========================================\n");
for (int i=1;i<=jumlah;i++)
{
c=a+b;
b=a;
a=c;
printf("%d ",c);
}
break;
}
case 2 : {
clrscr();
bilprima:
printf("Algoritma Untuk Menampilkan Bilangan Prima\n");
printf("===========================================\n");
printf("Masukkan nilai awal:");
scanf("%d",&a);
printf("Masukkan nilai akhir:");
scanf("%d",&b);
if (a>b) goto bilprima;
else
{
printf("Bilangan Prima antara %d-%d : \n", a,b);
for (c=a;c<=b;c++)
{if ((c==2)||(c==3)||(c==5)||(c==7)){printf("%d ",c);}
else if ((c%2!=0)&&(c%3!=0)&&(c%5!=0)&&(c%7!=0))
{printf("%d ", c);}
}
}
break;
case 3 : printf("\n\n\n\n T E R I M A K A S I H ");
break;
default: printf("\n\n\n\n Maaf Pilihan yang anda masukan salah !!!\n\n silahkan memilih angka 1-3");
}
}
getch();
}
#include <conio.h>
main()
{
int a,b,c,bilprima,jumlah,pil,pil2;
printf("+====================================+\n");
printf("| MENU UTAMA |\n");
printf("|------------------------------------|\n");
printf("| 1. Bilangan Fibonacci |\n");
printf("| 2. Bilangan Prima |\n");
printf("| 3. Keluar |\n");
printf("|------------------------------------|\n");
printf("| Masukkan pilihan (1-3) : |\n");
printf("+====================================+\n");
gotoxy(28,8); scanf("%d",&pil);
switch (pil)
{
case 1 : {
clrscr();
a=0;
b=1;
c=0;
printf("Algoritma Untuk Menampilkan Deret Fibonacci\n");
printf("===========================================\n");
printf("Masukkan Jumlah deret : ");
scanf ("%d",&jumlah);
printf("\n\nDeretnya adalah sebagai berikut :\n");
printf("===========================================\n");
for (int i=1;i<=jumlah;i++)
{
c=a+b;
b=a;
a=c;
printf("%d ",c);
}
break;
}
case 2 : {
clrscr();
bilprima:
printf("Algoritma Untuk Menampilkan Bilangan Prima\n");
printf("===========================================\n");
printf("Masukkan nilai awal:");
scanf("%d",&a);
printf("Masukkan nilai akhir:");
scanf("%d",&b);
if (a>b) goto bilprima;
else
{
printf("Bilangan Prima antara %d-%d : \n", a,b);
for (c=a;c<=b;c++)
{if ((c==2)||(c==3)||(c==5)||(c==7)){printf("%d ",c);}
else if ((c%2!=0)&&(c%3!=0)&&(c%5!=0)&&(c%7!=0))
{printf("%d ", c);}
}
}
break;
case 3 : printf("\n\n\n\n T E R I M A K A S I H ");
break;
default: printf("\n\n\n\n Maaf Pilihan yang anda masukan salah !!!\n\n silahkan memilih angka 1-3");
}
}
getch();
}
16. harga penjualan mini market
#include<stdio.h>
#include<conio.h>
main()
{
int kode,jumlah, harga,total;
char *nama;
printf(" Selamat Datang Di Toko Sari Mulia\n\n");
printf("\nKode : ");scanf("%d",&kode);
if (kode==11)
{nama="TV";
harga=15000000;
}
else if (kode==22)
{nama="Kulkas";
harga=3750000;
}
else if(kode==33)
{nama="Mesin Cuci";
harga=567000;
}
else
nama ="-";
printf("Nama :%s",nama);
printf("\nHarga: %d",harga);
printf("\nJumlah Beli : "); scanf("%d",&jumlah);
total=harga*jumlah;
printf("Total Harga : %d",total);
getch ();
}
#include<conio.h>
main()
{
int kode,jumlah, harga,total;
char *nama;
printf(" Selamat Datang Di Toko Sari Mulia\n\n");
printf("\nKode : ");scanf("%d",&kode);
if (kode==11)
{nama="TV";
harga=15000000;
}
else if (kode==22)
{nama="Kulkas";
harga=3750000;
}
else if(kode==33)
{nama="Mesin Cuci";
harga=567000;
}
else
nama ="-";
printf("Nama :%s",nama);
printf("\nHarga: %d",harga);
printf("\nJumlah Beli : "); scanf("%d",&jumlah);
total=harga*jumlah;
printf("Total Harga : %d",total);
getch ();
}
17. jumlah barang, harga diskon
#include"stdio.h"
#include"conio.h"
void main ()
{
char kode[20],nama[20];
float jumlah, harga, diskon, harga_diskon;
clrscr();
printf("==============Supermarket Sari=================\n");
printf("\n Kode Barang :");
scanf("%s",&kode);
printf("\n Nama Barang :");
scanf("%s",&nama);
printf("\n Harga : Rp ");
scanf("%f",&harga);
printf("\n Jumlah :");
scanf("%f",&jumlah);
diskon=0,3*harga;
harga_diskon=harga-diskon;
printf("=================================\n");
printf("Kode Barag = %s\n",kode);
printf("Nama Barang = %s\n",nama);
printf("Harga = Rp %.2f\n",harga);
printf("Diskon = 20\%\n");
printf("Jumlah = %.0f\n",jumlah);
printf("Jumlah harga = Rp %.2f\n",harga_diskon*jumlah);
printf("================================\n");
getch();
}
#include"conio.h"
void main ()
{
char kode[20],nama[20];
float jumlah, harga, diskon, harga_diskon;
clrscr();
printf("==============Supermarket Sari=================\n");
printf("\n Kode Barang :");
scanf("%s",&kode);
printf("\n Nama Barang :");
scanf("%s",&nama);
printf("\n Harga : Rp ");
scanf("%f",&harga);
printf("\n Jumlah :");
scanf("%f",&jumlah);
diskon=0,3*harga;
harga_diskon=harga-diskon;
printf("=================================\n");
printf("Kode Barag = %s\n",kode);
printf("Nama Barang = %s\n",nama);
printf("Harga = Rp %.2f\n",harga);
printf("Diskon = 20\%\n");
printf("Jumlah = %.0f\n",jumlah);
printf("Jumlah harga = Rp %.2f\n",harga_diskon*jumlah);
printf("================================\n");
getch();
}
Sampai disini dulu, semoga bermanfaat :)
Wassalamualaikum wr wb.
0 komentar:
Posting Komentar