EEM-119 Algoritma ve Programlama I Dersi
2022-2023 Güz Dönemi Ders 6
C Programlama örnekleri
#include <stdio.h>
int main(){
int i, j;
float x, y;
i=10;
j=20;
x=43.289;
y=5551;
printf("i=%d , j=%d, x=%f, y=%f",i, j, x, y);
}
i=10 , j=20, x=43.289001, y=5551.000000
#include <stdio.h>
int main(){
int i, j;
i=10;
j=20;
printf("i=%-5d",j);
printf("i=%5d\n",i);
}
i=20 i= 10
#include <stdio.h>
int main(){
int i, j;
i=1000000;
j=20;
printf("j=%.5d\n",j);
printf("i=%.5d",i);
}
j=00020 i=1000000
#include <stdio.h>
int main(){
int j;
j=20;
printf("j=%7.5d\n",j);
}
j= 00020
#include <stdio.h>
int main(){
float x, y;
x=43.289;
y=5551;
printf("x=%.7f, y=%.2f", x, y);
}
x=43.2890015, y=5551.00
#include <stdio.h>
int main(){
float x, y;
x=43.289;
y=5551;
printf("x=%20.7f, y=%9.2f", x, y);
}
x= 43.2890015, y= 5551.00
#include <stdio.h>
int main(){
printf("basit\thiza\ticin\n");
printf("ters\tslash\tt kullanilabilir");
}
basit hiza icin ters slash t kullanilabilir
#include <stdio.h>
int main(){
int a,b,c;
printf("birinci sayiyi giriniz: ");
scanf("%d",&a);
printf("ikinci sayiyi giriniz: ");
scanf("%d",&b);
c=a+b;
printf("sayilarin toplami: %d",c);
}
birinci sayiyi giriniz: 10 ikinci sayiyi giriniz: 20 sayilarin toplami: 30
#include <stdio.h>
int main(){
float yaricap, pi=3.14, alan;
printf("Yaricapi giriniz: ");
scanf("%f",&yaricap);
alan = pi * yaricap * yaricap;
printf("alan: %f", alan);
}
Yaricapi giriniz: 10 alan: 314.000000
#include <stdio.h>
#define PI 3.14
int main(){
float yaricap, alan;
printf("Yaricapi giriniz: ");
scanf("%f",&yaricap);
alan = PI * yaricap * yaricap;
printf("alan: %f", alan);
}
Yaricapi giriniz: 10 alan: 314.000000
#include <stdio.h>
int main(){
float a,b,c,d;
printf("birinci sayiyi giriniz (x/y): ");
scanf("%f/%f",&a,&b);
printf("ikinci sayiyi giriniz (x/y): ");
scanf("%f/%f",&c,&d);
printf("sonuc: %.2f/%.2f", a*d+b*c,b*d);
}
birinci sayiyi giriniz (x/y): 3/5 ikinci sayiyi giriniz (x/y): 2/7 sonuc: 31.00/35.00
- işlem önceliği:
i+j*k -> i+(j*k)
-i + -j -> (-i) + (-j)
-+i -> (-(+i))
#include <stdio.h>
int main(){
int i;
float f;
i=72.9999;
f=134;
printf("i: %d, f: %f",i,f);
}
i: 72, f: 134.000000
#include <stdio.h>
int main(){
int i,j,k;
i=j=k=10; // (i=j), (j=k) ,(k=10)
printf("i: %d, j: %d k:%d",i,j,k);
}
i: 10, j: 10 k:10
#include <stdio.h>
int main(){
int i;
float f;
f=i=33.99;
printf("i: %d, f: %f ",i,f);
}
i: 33, f: 33.000000
#include <stdio.h>
int main(){
int i=1,j=2,k=0;
k= 1+ (i=j);
printf("i: %d, j: %d, k:%d ",i, j, k);
}
i: 2, j: 2, k:3
- yanlis degisken tanimlamalari
int degisken 1;
int 1degisken;
int 1 degisken;
int degisken..1;
int değişken;
int DEGİSKEN;
- dogru degisken tanimlamalari
int degisken1;
int degisken_1;
int _degisken;
int _1degisken;
int bu_degisken;
int buDegisken;
int Bu1Degisken;
int BUDEGISKEN;
- yanlis atamalar
1=a; // doğrusu: a=1;
1+a=b; // doğrusu: b=a+1;
-i=j; // doğrusu: i=-j;
#include <stdio.h>
int main(){
int i=100,j=20,k=30;
i += j; //i=i+j;
printf("%d\n",i);
i += 20; //i=i+j;
printf("%d\n",i);
i -= k; //i=i-k;
printf("%d\n",i);
i *= j; // i=i*j;
printf("%d\n",i);
i /= k; // i=i/k;
printf("%d\n",i);
j=7;
i %= j; // i=i%j
printf("%d\n",i);
return 0;
}
120 140 110 2200 73 3
#include <stdio.h>
int main(){
int a=10, b=20;
a += b++;
printf("%d %d\n",a,b);
a += ++b;
printf("%d %d\n",a,b);
return 0;
}
30 21 52 22
#include <stdio.h>
int main(){
int a=10, b=20, c=30;
c+=a++ + ++b;
printf("%d %d %d\n",a,b,c);
return 0;
}
11 21 61
#include <stdio.h>
int main(){
float a=100, b=200, c;
float x, y=1200, z=900;
c=a+b;
x=y*z;
printf("%10.2f + %10.2f = %10.2f\n",a,b,c);
printf("%10.2f x %10.2f = %10.2f",y,z,x);
return 0;
}
100.00 + 200.00 = 300.00 1200.00 x 900.00 = 1080000.00
#include <stdio.h>
int main(){
int a,b,c;
printf("birinci sayi: ");
scanf("%d",&a);
printf("ikinci sayi: ");
scanf("%d",&b);
printf("ucuncu sayi: ");
scanf("%d",&c);
printf("girilen sayilarin toplami: %d\n",a+b+c);
printf("girilen sayilarin ortalamasi: %d",(a+b+c)/3);
return 0;
}
birinci sayi: 12 ikinci sayi: 14 ucuncu sayi: 20 girilen sayilarin toplami: 46 girilen sayilarin ortalamasi: 15
#include <stdio.h>
int main(){
float a,b,c;
printf("uc sayi giriniz: ");
scanf("%f%f%f",&a,&b,&c);
printf("girilen sayilarin toplami: %.2f\n",a+b+c);
printf("girilen sayilarin ortalamasi: %.2f",(a+b+c)/3);
return 0;
}
<pre>
uc sayi giriniz:
12 14 20
girilen sayilarin toplami: 46.00
girilen sayilarin ortalamasi: 15.33
</pre>
- Dikdortgenin alanini bulan program
#include <stdio.h>
int main(){
double kenar1, kenar2;
double alan;
printf("birinci kenari giriniz: ");
scanf("%lf",&kenar1);
printf("ikinci kenari giriniz: ");
scanf("%lf",&kenar2);
alan= kenar1*kenar2;
printf("hesaplanan alan: %.2lf",alan);
return 0;
}
birinci kenari giriniz: 10 ikinci kenari giriniz: 20 hesaplanan alan: 200.00
- kurenin hacmi
#include <stdio.h>
#define PI 3.14
int main(){
double r;
double hacim;
printf("yaricapi giriniz: ");
scanf("%lf",&r);
hacim=(4./3)*PI*r*r*r;
printf("hesaplanan hacim: %.2lf",hacim);
return 0;
}
yaricapi giriniz: 10 hesaplanan hacim: 4186.67
#include <stdio.h>
#define PI (22./7)
int main(){
double r;
double hacim;
printf("yaricapi giriniz: ");
scanf("%lf",&r);
hacim=(4./3)*PI*r*r*r;
printf("hesaplanan hacim: %.2lf",hacim);
return 0;
}
yaricapi giriniz: 10 hesaplanan hacim: 4190.48