Anda di halaman 1dari 18

Nilai jumlah dan rata

#include <stdio.h>
#include <conio.h>
void main ()
{
int x,a,b,c,a1,b1,c1,jumlah,banyak;
float rata;
printf ("masukan angka a :"); scanf("%d",&a);
printf ("masukan angka b :"); scanf("%d",&b);
printf ("masukan angka c :"); scanf("%d",&c);
a1=a!=0;
b1=b!=0;
c1=c!=0;
jumlah=a+b+c;
printf("cetak jumlah = %d\n", jumlah);
banyak=a1+b1+c1 ;
printf ("cetak banyak = %d\n", banyak);
rata = jumlah/banyak;
printf("cetak rata = %f", rata);
}
Mencetak deret
#include <stdio.h>
#include <conio.h>
void main ()
{
int bil;
for (bil=1;bil<100;bil++)
{
if( bil %2 == 1)
printf ("%5.0i",bil);
}
}
Bil ganjil genap
#include <stdio.h>
main(){
int x;
printf("Masukkan bilangan : ");
scanf("%d",&x);
if(x%2==0){
printf("%d adalah bilangan genap\n\n",x);
}else{

printf("%d adalah bilangan ganjil\n\n",x);


}
system("PAUSE");
return 0;
}
Mencari deret pangkat
#include <stdio.h>
int deret2();
int main()
{
deret2();
}
int deret2()
{
int i,a,deret_2;
printf("--Mencari Nilai Pangkat 2-- \n\n");
printf("Masukkan Jumlah Deret Pangkat 2 Yang ingin di Cari: ");
scanf("%d", &a);
for (i = 1; i <= a; i++)
{
deret_2=i*i;
printf("Deret3 (%d) : %d\n", i, deret_2);
}
printf("\n");
}
Deret fibonaci
#include <stdio.h>
int fibonacci()
{
int i,a = 0, b = 1, bil,x;
printf("--Mencari Nilai Fibonacci-- \n\n");
printf ("Masukan banyaknya baris fibonaci yang diinginkan: ");
scanf ("%d",&x);
for (i=0;i<x;i++)
{
if (i<=1)
{
bil = i;

}
else
{
bil=a+b;
a=b;
b=bil;
}
printf ("%d ",bil);
}
printf("\n");
getch();
system("cls");
}
int main()
{
int menu;
while(menu!=2)
{
printf("--Pilihan menu--\n");
printf("1. Mencari deret Fibonacci \n");
printf("2. Exit \n");
printf("Anda memilih menu : ");
scanf("%d", &menu);
system("cls");
if (menu==1)
{
fibonacci();
}
if (menu==2)
{
printf("--Good Bye-- \n\n");
}
else
{
system("cls");
}
}
system("PAUSE");

return 0;
}
Membuat matriks
int main (){
int i,j,baris,kolom;
printf ("masukan baris : "); //user diminta memasukan banyak baris
scanf ("%d",&baris);
printf ("masukan kolom : "); //user diminta memasukan banyak kolom
scanf ("%d",&kolom);
int m[baris][kolom];
printf ("masukkan angka ke dalam matriks : ");
printf ("\n");
/*
pemasukan angka ke dalam matriks oleh user menggunakan perulangan
*/
for (i=0;i<baris;i++){
for(j=0;j<kolom;j++){
printf ("baris %d kolom %d: ",i+1,j+1);
scanf ("%d",&m[i][j]);
}
}
printf ("\n");
/* menampilkan hasil masukan user berbentuk matriks */
for (i=0;i<baris;i++){
for(j=0;j<kolom;j++){
printf ("%d ",m[i][j]);
}
printf ("\n");
}
return 0;
}
Mencetak bilangan prima
int main ()
{
int input;

int counterinput;
int angka;
int hitungbagi;
int counter;
printf("masukan jumlah yang diinginkan : ");
scanf("%d",&input);
counterinput = 1;
angka = 1;
while(counterinput<=input)
{
/*angka 2 termasuk bilangan prima */
if(angka==2)
{
counterinput ++;
printf("%d ",angka);
}
/*bilangan ganjil dan bukan angka 1*/
else if(angka%2==1 && angka!=1)
{
hitungbagi=0;
for(counter=1;counter<=angka;counter++)
{
if(angka%counter==0)
{
hitungbagi++;
}
}
if(hitungbagi==2)
{
counterinput++;
printf("%d ",angka);
}
}

angka ++;
}
return 0;
}
Mencari pers kuadrat rumus abc
#include <stdio.h>
#include <conio.h>
#include <math.h>
void AkarKuadrat(int a, int b, int c){
float x1,x2;
int D;
D=(b*b)-(4*a*c);
if (D==0) x1=-1*(b/(2*a));
else if(D>0){
x1=(-b+(sqrt(D)))/(2*a);
x2=(-b-(sqrt(D)))/(2*a);
}
printf("D = %d\n",D);
printf("x1 = %.2f\n",x1);
printf("x2 = %.2f",x2);
}
int main()
{
int a,b,c;
printf("Masukkan nilai a : \n");scanf("%d",&a);
printf("Masukkan nilai b : \n");scanf("%d",&b);
printf("Masukkan nilai c : \n");scanf("%d",&c);
AkarKuadrat(a,b,c);
return 0;
}
Mencari nilai maks dan nilai minimum
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,max,min,bil;
printf(" Program Mencari Nilai Maksimum dan Minimum\n");
printf("\n Masukkan Banyak Data = ");
scanf("%d",&n);
printf("\n Masukkan Bilangan ke-1 = ");
scanf("%d",&bil);
max=bil;
min=bil;

for(i=2;i<=n;i++)
{
printf("\n Masukkan Bilangan Ke-%d = ",i);
scanf("%d",&bil);
if(bil>max)
max=bil;
if(bil<min)
min=bil;
}
printf("\n Nilai Maksimum nya adalah %d\n",max);
printf("\n Nilai Minimum nya adalah %d\n",min);
getch();
}
Urutkan angka dari terkecil hingga ke besar
int main()
{
int a,b,c,d,tmp;
printf("Masukkan nilai a :");
scanf(" %d",&a);
printf("Masukkan nilai b :");
scanf(" %d",&b);
printf("Masukkan nilai c :");
scanf(" %d",&c);
printf("Masukkan nilai d :");
scanf(" %d",&d);
if (a>b){
(tmp=a,a=b,b=tmp);
}
if (a>c){
(tmp=a,a=c,c=tmp);
}
if (a>d){
(tmp=a,a=d,d=tmp);
}
if (b>c){
(tmp=b,b=c,c=tmp);
}
if (b>d){
(tmp=b,b=d,d=tmp);
}
if (c>d){
(tmp=c,c=d,d=tmp);
}
printf("\n");

printf("Nilai terkecil hingga terbesar %d, %d, %d, %d",a,b,c,d);


printf("\n");
return 0;
}
Lulus atau tidak lulus
#include<stdio.h>
#include<conio.h>
void main ()
{
float nilai;
printf ("masukan nilai yang didapat :");
scanf("%f", &nilai);
if (nilai>65)
printf("lulus :");
else
printf ("tidak lulus :");
}
Menentukan nama hari
#include <stdio.h>
#include <conio.h>
void main ()
{
int hari;
puts ("menentukan nama hari");
puts ("1=senin 2=selasa 3=rabu 4=kamis 5=jumat 6=sabtu
7=minggu");
printf("masukan kode hari (1-7)"); scanf("%s", &hari);
switch (hari)
{
case 1 : puts ("hari senin");
break ;
case 2 : puts ("hari selasa");
break ;
case 3 : puts ("hari rabu");
break ;
case 4 : puts ("hari kamis");
break ;
case 5 : puts ("hari jumat");
break ;
case 6 : puts ("hari sabtu");
break ;
case 7 : puts ("hari minggu");
break ;

mencari ipk
#include <stdio.h>
#include <conio.h>
void IP(int n){
float NA, Ntot=0, IP;
int sks, skstot=0, i, NM;
char makul[20];
typedef struct{
float uas, uts, tugas;
}Nilai;
Nilai Nil[100];
for(i=0;i<n;i++){
printf("\nNama mata kuliah %d : ",i+1);gets(makul);gets(makul);
printf("Jumlah SKS : ");scanf("%s",&sks);
printf("Nilai UAS : ");scanf("%f",&Nil[i].uas);
printf("Nilai UTS : ");scanf("%f",&Nil[i].uts);
printf("Nilai Tugas : ");scanf("%f",&Nil[i].tugas);
NA=(Nil[i].uas*0.45)+(Nil[i].uts*0.35)+(Nil[i].tugas*0.2);
if(NA>=86 && NA<=100){
NM=sks*4;
printf("Nilai Huruf : A\n");
}
else if(NA>=71 && NA<=85){
NM=sks*3;
printf("Nilai Huruf : B\n");
}
else if(NA>=56 && NA<=70){
NM=sks*2;
printf("Nilai Huruf : C\n");
}
else if(NA>=41 && NA<=55){
NM=sks*1;
printf("Nilai Huruf : D\n");
}
else if(NA>=0 && NA<=40){
NM=0;
printf("Nilai Huruf : E\n");
}
Ntot=Ntot+NM;
skstot=skstot+sks;
}
IP=Ntot/skstot;
printf("\nIP anda adalah %.2f",IP);
}
void main (){
char nama[30], nim[11];
int n;
printf("Nama : ");gets(nama);gets(nama);
printf("NIM : ");scanf("%s",&nim);
printf("Banyak mata kuliah yang anda ambil : ");scanf("%d",&n);
IP(n);

getch();
}

}
getch ();
}
Membuat segitiga
#include<stdio.h>
main()
{
int a,b,c,d;
printf("masukkan nilai = "); scanf("%d", &a);
printf("\n");
for(b=a; b>=1; b--)
{
for(c=1; c<=b; c++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Membuat pyramid
int main () {
int baris, spasi, star, i;
printf("tinggi bangun = ");
scanf ("%d",&baris);
for(i=1; i<=baris; i++){
for(spasi=0; spasi < (baris-i); spasi++){
printf(" ");
}
for(star=0; star < 2*i-1; star++){
printf("*");
}
printf("\n");
}

for (i=(baris-1); i>0; i--){


for(spasi=0; spasi<(baris-i); spasi++){
printf(" ");
}
for(star=0; star< 2*i-1; star++){
printf("*");
}
printf("\n");
}
scanf ("%d",&baris);
return 1;
}
Perkalian matriks
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m1[100][100], m2 [100][100], m3 [100][100], b,k,BB,BK,i,sum=0,kk;
printf("=== Buat Matrik ===\n");
printf("masukkan berapa besar Baris Matrik : ");scanf("%d",&b);
printf("masukkan berapa besar Kolom Matrik : ");scanf("%d",&k);
for(i=1; i<3; i++){
printf("\n\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]);}
}
}
}
printf("\n\n=== gambar 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");
}
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;
}
}
}
}
printf("=== Hasil Perkalian Matrik ===\n");
for(BB=0;BB<b;BB++){
for(BK=0;BK<k;BK++){
printf(" %d ",m3[BB][BK]);
}
printf("\n");
}
return 0;
}
Penjumlahan matriks
#include <stdio.h>
#include <conio.h>
void main()
{
int j,i,a[2][2],b[2][2],c[2][2];
a[0][0]=1;
a[0][1]=2;
a[1][0]=5;
a[1][1]=6;
b[0][0]=1;
b[0][1]=2;
b[1][0]=5;

b[1][1]=6;
printf("Matrik a \n");
for (i=0; i<2; i++){
for(j=0;j<2;j++){
printf(" %d",a[i][j]);
}
printf("\n");
}
printf("Matrik b \n");
for (i=0; i<2; i++){
for(j=0;j<2;j++){
printf(" %d",b[i][j]);
}
printf("\n");
}
printf("Hasil a+b \n");
for (i=0; i<2; i++){
for(j=0;j<2;j++){
c[i][j]=a[i][j]+b[i][j];
printf(" %d",c[i][j]);
}
printf("\n");
}
getch();
}
Inverse matrik
#include<stdio.h>
#include<math.h>
#include<conio.h>
float determinant(float a[25][25],float k);

void cofactor(float num[25][25],float f);


void transpose(float num[25][25],float fac[25][25],float r);
int main()
{
float a[25][25],k,d;
int i,j;
printf("-------------------------------------------------------------\n");
printf("----------------program invers matriks------------------------\n");
printf("-------------------------------------------------------------\n");
printf("masukkan ordo matriks : ");
scanf("%f",&k);
printf("masukkan elemen dari matriks %.0fX%.0f \n",k,k);
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
printf("A(%d,%d)",i+1,j+1);
scanf("%f",&a[i][j]);
}
}
d=determinant(a,k);
printf("Determinan = %.2f",d);
if (d==0)
printf("\nInverse matriks tidak temukan\n");
else
cofactor(a,k);
printf("\n\n**** terimah kasih telah menggunakan program!!! ****");
getch();
}
/*For calculating Determinant of the Matrix */
float determinant(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if (k==1)
{
return (a[0][0]);
}
else
{
det=0;
for (c=0;c<k;c++)
{
m=0;
n=0;

for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
b[i][j]=0;
if (i != 0 && j != c)
{
b[m][n]=a[i][j];
if (n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det + s * (a[0][c] * determinant(b,k-1));
s=-1 * s;
}
}
return (det);
}
void cofactor(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for (q=0;q<f;q++)
{
for (p=0;p<f;p++)
{
m=0;
n=0;
for (i=0;i<f;i++)
{
for (j=0;j<f;j++)
{
if (i != q && j != p)
{
b[m][n]=num[i][j];
if (n<(f-2))
n++;
else

{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q + p) * determinant(b,f-1);
}
}
transpose(num,fac,f);
}
/*Finding transpose of matrix*/
void transpose(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inverse[25][25],d;
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=determinant(num,r);
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
inverse[i][j]=b[i][j] / d;
}
}
printf("\n\n\nThe inverse of matrix is : \n");
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
printf("\t%.1f",inverse[i][j]);
}
printf("\n");
}
}
Memasukan inputan nilai
#include <stdio.h>

#include <conio.h>
int i,j,k ;
void main ()
{
int siswa [40] [7] ,k=2;
float rata [40];
for (i=0;i<2;i++){
printf ("nim :"); scanf ("%d", &siswa [i][0]);
printf ("pkn :"); scanf ("%d", &siswa [i][1]);
printf ("kwn :"); scanf ("%d", &siswa [i][2]);
printf ("ind :"); scanf ("%d", &siswa [i][3]);
siswa [1][4]=siswa [i][1]+siswa [i][2]+siswa [i][3];
printf ("------\n :");
rata [i]=siswa [i][4]/3 ;
}
printf (" nim
pkn
kwn
ind
jml
rata \n");
for(i=0;i<k;i++){
printf (" %6d %3d %3d %3d %3d %3.2f \n ", siswa [i][0], siswa [i][1],
siswa [i][2], siswa [i][3], siswa [i][4],rata [i] );
}
}
soal response sortir
#include <stdio.h>
#include <conio.h>
void main(){
int angka[10]={90,38,11,23,56,34,89,36,98,45}, i,j,simp;
puts(" Sebelum" );
for (i=0;i<10;i++){
printf(" %d ",angka[i]);
}
for (i=0;i<9;i++){
for (j=0;j<9;j++){
if (angka[j]<angka[j+1]){
simp=angka[j];
angka[j]=angka[j+1];

angka[j+1]=simp;
}
}
}
puts(" \n sesudah ");
for (i=0;i<10;i++){
printf(" %d ",angka[i]);
}
}
Soal responsi kembalian
#include <stdio.h>
#include <conio.h>
void main(){
int nominal, k100,k50,k20,k10,k5,k2,k1,r500,r100;
int a,b,c,d,e,f,g,z;
printf( "Masukkan Nominal : " );scanf("%d",&nominal);
k100=nominal/100000;z=nominal%100000;
k50 =z/50000; a=z%50000;
k20 =a/20000; b=a%20000;
k10 =b/10000; c=b%10000;
k5 =c/ 5000; d=c%5000;
k2 =d/ 2000; e=d%2000;
k1 =e/ 1000; f=e%1000;
r500 =f/ 500; g=f%500;
r100 =g/ 100;
printf("
printf("
printf("
printf("
printf("
printf("
printf("
printf("
printf("
}

Rp
Rp
Rp
Rp
Rp
Rp
Rp
Rp
Rp

100.000 --> %d\n",k100);


50.000 --> %d\n",k50);
20.000 --> %d\n",k20);
10.000 --> %d\n",k10);
5.000 --> %d\n",k5);
2.000 --> %d\n",k2);
1.000 --> %d\n",k1);
500 --> %d\n",r500);
100 --> %d\n",r100);

Anda mungkin juga menyukai