EEM-119 Algoritma ve Programlama I Dersi
- Format belirleyicileri
#include <stdio.h>
int main(int argc, char *argv[]) {
int i, j;
float x, y;
i=10;
j=20;
x=23.45;
y=7442;
printf("i:%d, j:%d, x:%f, y:%f",i,j,x,y);
return 0;
}
i:10, j:20, x:23.450001, y:7442.000000
#include <stdio.h>
int main(int argc, char *argv[]) {
int i, j;
i=10;
j=20;
printf("i:%5d\n",i);
printf("i:%5d\n",123);
printf("i:%5d\n",1230);
printf("i:%5d\n",1230123);
printf("j:%-5d, i:%d\n",j,i);
printf("j:%-5d, i:%d",1230123,i);
return 0;
}
i: 10 i: 123 i: 1230 i:1230123 j:20 , i:10 j:1230123, i:10
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
i=10;
printf("i:%7.5d\n",i);
return 0;
}
i: 00010
#include <stdio.h>
int main(int argc, char *argv[]) {
float x,y;
x=23.45;
y=7442;
printf("x:%.5f, y:%.2f\n",x,y);
return 0;
}
x:23.45000, y:7442.00
#include <stdio.h>
int main(int argc, char *argv[]) {
float x,y;
x=23.45;
y=7442;
printf("x:%10.5f, y:%14.2f\n",x,y);
return 0;
}
x: 23.45000, y: 7442.00
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("basit\thizalama\ticin\n");
printf("\\t\tkullanilabilir.");
return 0;
}
basit hizalama icin \t kullanilabilir.
- scanf fonksiyonu
#include <stdio.h>
int main(int argc, char *argv[]) {
int a,b,c;
printf("birinci sayiyi giriniz: ");
scanf("%d",&a);
printf("ikinci sayiyi giriniz: ");
scanf("%d",&b);
c=a+b;
printf("%d+%d=%d",a,b,c);
return 0;
}
birinci sayiyi giriniz: 12 ikinci sayiyi giriniz: 25 12+25=37
#include <stdio.h>
int main(int argc, char *argv[]) {
float pi=3.14, r;
float alan;
printf("alani bulmak icin yaricapi giriniz: ");
scanf("%f",&r);
alan=pi*r*r;
printf("yaricapi %.2f olan dairenin alani: %.2f", r, alan);
return 0;
}
alani bulmak icin yaricapi giriniz: 5 yaricapi 5.00 olan dairenin alani: 78.50
#include <stdio.h>
#define PI 3.14
int main(int argc, char *argv[]) {
float r;
float alan;
printf("alani bulmak icin yaricapi giriniz: ");
scanf("%f",&r);
alan=PI*r*r;
printf("yaricapi %.2f olan dairenin alani: %.2f", r, alan);
return 0;
}
alani bulmak icin yaricapi giriniz: 5 yaricapi 5.00 olan dairenin alani: 78.50
#include <stdio.h>
int main(int argc, char *argv[]) {
int x,y;
printf("iki sayi giriniz: ");
scanf("%d",&x);
scanf("%d",&y);
printf("%d / %d = %d",x,y,x/y);
return 0;
}
iki sayi giriniz: 20 5 20 / 5 = 4
#include <stdio.h>
int main(int argc, char *argv[]) {
float x,y;
printf("iki sayi giriniz: ");
scanf("%f",&x);
scanf("%f",&y);
printf("%.2f / %.2f = %.2f",x,y,x/y);
return 0;
}
iki sayi giriniz: 22 5 22.00 / 5.00 = 4.40
#include <stdio.h>
int main(int argc, char *argv[]) {
float x,y;
printf("iki sayi giriniz: ");
scanf("%f%f",&x,&y);
printf("%.2f / %.2f = %.2f",x,y,x/y);
return 0;
}
iki sayi giriniz: 17 4 17.00 / 4.00 = 4.25
-İşlem önceliği
#include <stdio.h>
int main(int argc, char *argv[]) {
int a=10,b=20,c=30;
printf("%5d\n",a+b*c);
printf("%5d\n",a+-b*c);
printf("%5d\n",-a+-b);
return 0;
}
610 -590 -30
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
i=72.9999;
printf("%d",i);
return 0;
}
72
#include <stdio.h>
int main(int argc, char *argv[]) {
int i,j,k;
i=j=k=20;
printf("%d %d %d",i,j,k);
return 0;
}
20 20 20
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
float f;
f=i=20.78;
printf("%d %f",i,f);
return 0;
}
20 20.000000
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
float f;
i=f=20.78;
printf("%d %f",i,f);
return 0;
}
20 20.780001
#include <stdio.h>
int main(int argc, char *argv[]) {
int i,j,k;
k=5+(i=j=10);
printf("%d %d %d",i,j,k);
return 0;
}
10 10 15
- yanlış degisken tanimlama sekilleri
int degisken 1 int 1.degisken int 1degisken int degisken.1 int değişken int DEĞİŞKEN
- dogru degisken tanimalamali
int degisken1 int degisken_1 int birinci_degisken int _degisken int bu_degisken int Bu_1inci_degisken int BUDEGISKEN int BU_DEGISKEN int degisken_
- yanlis atamalar
1=a; // a=1 olmali 1+a=b; // b=a+1 olmali -i=j; // i=-j olmali
- Bileşke Operatörler
#include <stdio.h>
int main(int argc, char *argv[]) {
int i=100,j=20,k=30;
i += j; // i = i+j
printf("%d\n",i);
i += 5;
printf("%d\n",i);
j -= k;
printf("%d\n",j);
i*=j+5;
printf("%d",i);
return 0;
}
120 125 -10 -625
#include <stdio.h>
int main(int argc, char *argv[]) {
int i=100,j=20,k=30;
i /= k;
printf("%d\n",i);
j %= i;
printf("%d\n",j);
return 0;
}
3 2
- Arttirma ve azaltma operatörleri
#include <stdio.h>
int main(int argc, char *argv[]) {
int a=10;
a++;
++a;
printf("%d\n",a);
printf("%d\n",a++);
printf("%d\n",++a);
return 0;
}
12 12 14
#include <stdio.h>
int main(int argc, char *argv[]) {
int a=10, b=20;
a += b++;
printf("%d\n",a++);
a += ++b;
printf("%d\n",++a);
return 0;
}
30 54
#include <stdio.h>
int main(int argc, char *argv[]) {
int a=10, b=20, c=30;
c += a++ + ++b;
printf("%d %d %d",a,b,c);
return 0;
}
11 21 61
-Örnekler
#include <stdio.h>
int main(int argc, char *argv[]) {
int x,y,z;
printf("uc sayi giriniz: ");
scanf("%d%d%d",&x,&y,&z);
printf("sayilarin toplami: %d\n",x+y+z);
printf("sayilarin ortalamasi: %d",(x+y+z)/3);
return 0;
}
uc sayi giriniz: 2 3 5 sayilarin toplami: 10 sayilarin ortalamasi: 3
#include <stdio.h>
int main(int argc, char *argv[]) {
int x,y,z;
printf("uc sayi giriniz: ");
scanf("%d%d%d",&x,&y,&z);
printf("sayilarin toplami: %d\n",x+y+z);
printf("sayilarin ortalamasi: %f",(x+y+z)/3.0);
return 0;
}
uc sayi giriniz: 2 3 5 sayilarin toplami: 10 sayilarin ortalamasi: 3.333333
#include <stdio.h>
int main(int argc, char *argv[]) {
float kenar1, kenar2, alan;
printf("dikdortgenin alanini bulan program:\n");
printf("birinci kenari giriniz: ");
scanf("%f",&kenar1);
printf("ikinci kenari giriniz: ");
scanf("%f",&kenar2);
alan=kenar1*kenar2;
printf("kenarlari %.2f ve %.2f olan dikdortgenin alani:%.2f",
kenar1, kenar2, alan);
return 0;
}
dikdortgenin alanini bulan program: birinci kenari giriniz: 3 ikinci kenari giriniz: 11.7 kenarlari 3.00 ve 11.70 olan dikdortgenin alani:35.10
#include <stdio.h>
#define PI 3.14
int main(int argc, char *argv[]) {
//kurenin hacmini bulan program
double yaricap, hacim;
printf("yaricapi giriniz: ");
scanf("%lf",&yaricap);
hacim = (4./3) * PI * yaricap * yaricap * yaricap;
printf("yaricapi %.2lf olan kurenin hacmi:\n", yaricap);
printf("%.2lf", hacim);
return 0;
}
yaricapi giriniz: 10 yaricapi 10.00 olan kurenin hacmi: 4186.67
#include <stdio.h>
#define PI (22.0/7)
int main(int argc, char *argv[]) {
//kurenin hacmini bulan program
double yaricap, hacim;
printf("yaricapi giriniz: ");
scanf("%lf",&yaricap);
hacim = (4./3) * PI * yaricap * yaricap * yaricap;
printf("yaricapi %.2lf olan kurenin hacmi:\n", yaricap);
printf("%.2lf", hacim);
return 0;
}
yaricapi giriniz: 10 yaricapi 10.00 olan kurenin hacmi: 4190.48