0% found this document useful (0 votes)
40 views

Nesneye Dayalı Programlama

This document contains code snippets demonstrating various C++ programming concepts like data types, input/output, conditional statements, loops, functions, arrays, and more. The snippets show how to declare and use variables of different types, take user input, print output, implement if/else statements, for, while and do-while loops, define and call functions, create and access array elements, and sort arrays.

Uploaded by

Osman Aydar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Nesneye Dayalı Programlama

This document contains code snippets demonstrating various C++ programming concepts like data types, input/output, conditional statements, loops, functions, arrays, and more. The snippets show how to declare and use variables of different types, take user input, print output, implement if/else statements, for, while and do-while loops, define and call functions, create and access array elements, and sort arrays.

Uploaded by

Osman Aydar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 15

#include<iostream>

using namespace std;

int main(){
cout<<"Hello World"<< endl;
int tamSayi;
float kesirliSayi1;
double kesirliSayi2;
bool anahtar;
char karakter;

tamSayi = 20;
kesirliSayi1 = 10.45;
kesirliSayi2 = 30.78;
anahtar = true;// True (1) veya False (0) olabilir.
karakter = 'A';

cout << "Tam Sayi: " << tamSayi << endl;


cout << "Float Sayi: " << kesirliSayi1 << endl;
cout << "Double Sayi: " << kesirliSayi2 << endl;
cout << "anahtar: " << anahtar << endl;
cout << "Karakter: " << karakter << endl;

cout << "Kapasite: " << sizeof(bool) << endl;


return 0;
}

#include <iostream>
#include <locale.h>
using namespace std;

int main()
{
setlocale(LC_ALL,"Turkish");
string isim;
int yas;
cout << "Adini gir: ";
cin >> isim;
cout << "Yasini gir: ";
cin >> yas;
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int sayi;
cout << "Sayi gir: ";
cin >> sayi;
sayi = sayi+1; // sayı 1 arttırıldı
sayi += 1; // sayı 1 arttırıldı
sayi++; // sayı 1 arttırıldı
cout << "Yeni Sayi: " << sayi << endl;
return 0;
}
// a += b; // a = a + b;
// a -= b; // a = a - b;
// a *= b; // a = a * b;
// a /= b; // a = a / b;

#include <iostream>
using namespace std;

int main()
{
float yaricap, cevre, alan;
cout << "Dairenin yaricapini giriniz: ";
cin >> yaricap;
cevre = 2*3.14*yaricap;
alan = 3.14*yaricap*yaricap;
cout << "Dairenin Cevresi: " << cevre << " " << "Dairenin alani: " << alan
<<endl;

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int sayi = 10;
cout << "İlk Sayi: " << ++sayi << endl;
cout << "Son Sayi: " << sayi++ << endl;
cout << sayi << endl;
return 0;
}
// sayi++ önce sayiyi ekrana yazdırır sonra sayiyi arttırır.
// ++sayi önce sayiyi arttırır sonra sayiyi ekrana yazdırır.

#include <iostream>
using namespace std;

int main()
{
int sayi;
cout << "Sayi gir: " << endl;
cin >> sayi;
if(sayi > 0){
cout << "Sayi pozitiftir."<< endl;
cout << "Deneme";
}
else if (sayi == 0)
cout << "Sayi sifirdir.";
else
cout << "Sayi negatiftir.";

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int islem;
cout << "İsleminizi seciniz: ";
cin >> islem;
switch(islem){
case 1:
cout << "1.Durum secildi.";
break;
case 2:
cout << "2.Durum secildi.";
break;
case 3:
cout << "3.Durum secildi.";
break;
default:
cout << "Uygun durum secilmedi.";

}
return 0;
}

#include <iostream>
using namespace std;

int main()
{
char islem;
cout << "İsleminizi seciniz: ";
cin >> islem;
int a = 10, b = 5;
switch(islem){
case '+':
cout << "Toplam: " << a+b;
break;
case '-':
cout << "Fark: " << a-b;
break;
case '*':
cout << "Carpim: " << a*b;
break;
case '/':
cout << "Bolum: " << a/b;
break;
default :
cout << "Uygun islem secilmedi. ";

}
return 0;
}

#include <iostream>
using namespace std;

int main()
{
for (int i=0;i<10;i++){
cout << i << " ";
cout << "merhaba" <<endl;
}
return 0;
}

#include <iostream>
using namespace std;

int main()
{
for (int i=0; i<3;i++)
for (int j=0; j<3;j++)
cout << i << " " << j << endl;
return 0;
}

#include <iostream>
using namespace std;

int main()
{
for (int i=0; i<3;i++)
cout << "i: " << i << " " << endl;
for (int j=0; j<3;j++)
cout << "j: " << j << endl;
return 0;
}

#include <iostream>
using namespace std;

int main()
{
for (int i=0; i<3;i++){
cout << "i: " << i << " ";
for (int j=0; j<3;j++)
cout << "j: " << j << endl;
}
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int a = 5;
while(a!=0){
cout << a << " ";
a--;
}
return 0;
}

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int sayi1 = 5, sayi2 = 2;
double sayi3 = 3.46;
cout << "Buyuk: " << max(sayi1,sayi2) << endl;
cout << "Kucuk: " << min(sayi1,sayi2) << endl;
cout << "Karekok: " << sqrt(sayi1) << endl;
cout << "Logaritma: " << log(sayi1) << endl;
cout << "Us Alma: " << pow(sayi1,sayi2) << endl;
cout << "Buyuge Yuvarlama: " << ceil(sayi3) << endl;
cout << "Kucuge Yuvarlama: " << floor(sayi3) << endl;
cout << "En Yakına Yuvarlama: " << round(sayi3) << endl;
return 0;
}
#include <iostream>
using namespace std;

int main()
{
int i = 0;
do{
cout << "Merhaba ";
i++;
}while(i<5);

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int sayi;
do{
cout << "sayi gir: ";
cin >> sayi;
if (sayi == 0) break;
}while(1);

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int sayi = 0;
while(sayi<100){
if(sayi%5==0)
cout << sayi << " ";
sayi++;
}

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int dizi[5] = {1,3,5,7,9};
cout << dizi[0] << endl;
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int dizi[5] = {1,3,5,7,9};
for(int i=0;i<5;i++)
cout << "Dizinin " << i+1 << ". elemani:" << dizi[i] << endl;
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int sayi [5];
for(int i=0;i<5;i++){
cout << "Dizinin " << i+1 << ". elemanini gir: ";
cin >> sayi[i];
}
for(int i=0;i<5;i++)
cout << "Dizinin " << i+1 << ". elemani: " << sayi[i] << endl;
return 0;
}

#include <iostream>
using namespace std;

int main()
{
char dizi[5] {'A','B','C'};
//dizi[0] = 'A';
//dizi[1] = 'B';
//dizi[2] = 'C';
for (int i=0;i<3;i++)
cout << dizi[i] << " ";
return 0;
}
//char tek bir karakter tutar.Dikkat 'A'
//string birden fazla karakteri de tutar.Dikkat "Adam"
#include <iostream>
using namespace std;

int main()
{
string dizi[] = {"Enes","Furkan","Yahya"};
for (int i=0;i<3;i++)
cout << "Dizinin " << i+1 << ".elemani: " << dizi[i] << endl;

return 0;
}

#include <iostream>
using namespace std;

int main()
{
int dizi[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
cout << dizi[2][0];
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int dizi[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for (int i=0;i<3;i++)
for(int j=0;j<3;j++)
cout << dizi[i][j] << " ";
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int dizi[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for (int i=0;i<3;i++){
cout << endl;
for(int j=0;j<3;j++)
cout << dizi[i][j] << " ";
}
return 0;
}
#include <iostream>
using namespace std;

int main()
{
int dizi[] = {56,32,45,78,12,4,96,5,21,62};
int temp;
for(int i=0;i<9;i++)
for (int j=0;j<9;j++){
if (dizi[j]>dizi[j+1]){
temp = dizi[j];
dizi[j] = dizi[j+1];
dizi[j+1] = temp;
}
}
for(int i=0;i<10;i++)
cout << dizi[i] << " ";
return 0;
}
//son sayiyi tempe almadığımız için 9 a kadar yazıyoruz

#include <iostream>
using namespace std;

int main()
{
char dizi[10];
for(int i=0;i<10;i++)
dizi[i] = i+65;
for(int i=0;i<10;i++)
cout << dizi[i] << " ";
return 0;
}

#include <iostream>
using namespace std;

// Veri almayan ve veri gondermeyen fonksiyon


//g++ dosyaismi.cpp -o isim
void yazdir(){
cout << "Merhaba Dunya! " << endl;
}

// Ana fonksiyondan (main) veri alan fakat geri veri göndermeyen fonksiyon
void noyazdir(int no){
cout << no << endl;
}

// Ana fonksiyondan (main) birden fazla veri alan fakat geri veri göndermeyen
fonksiyon
void topla(int a, int b){
cout << "Toplam: " << a+b << endl;
}

int main()
{
yazdir();
noyazdir(100);
topla(6,4);
topla(20,11);
topla(500,250);
return 0;
}

#include <iostream>
using namespace std;

void topla(int a, int b){


cout << "Toplam'in sonucu: " << a+b << endl;
}

void cikar(int a, int b){


cout << "Cikarma'nin sonucu: " << a-b << endl;
}

int main()
{
int x, y;
cout << "1.sayi: ";
cin >> x;
cout << endl;
cout << "2.sayi: ";
cin >> y;
cout << endl;
topla(x,y);
cout << endl;
cikar(x,y);

return 0;
}
// int main fonksiyonundaki deger ile void topla fonksiyonundaki değer birbirinden
farklı
// Her fonksiyon kendi degiskenini bilir.Diger degiskenlerden haberdar degildir

#include <iostream>
using namespace std;

int topla(int a,int b){


return a+b;
}

int main()
{
int gelen = topla(5,7);
cout << gelen;
return 0;
}

#include <iostream>
using namespace std;

int topla(int a, int b){


return a+b;
}

int cikar(int a, int b){


return a-b;
}

double bolme(int a, int b){


return (double)a/b;
}

int carp(int a, int b){


return a*b;
}

int main()
{
char secim;
int a,b;
cout << "1.sayiyi giriniz: ";
cin >> a;
cout << "2.sayiyi giriniz: ";
cin >> b;
cout << "Secim yap: ";
cin >> secim;
switch(secim){
case '+':
cout << "Toplam: " << topla(a,b);
break;
case '-':
cout << "Fark: " << cikar(a,b);
break;
case '/':
cout << "Bolum: " << bolme(a,b);
break;
case '*':
cout << "Carpim: " << carp(a,b);
break;

default :
cout << "Uygun islem secilmedi." << endl;
}
return 0;
}
#include <iostream>
using namespace std;

int arttir(int a, int b){


a++;
b++;
return a,b;
}
int main()
{
int a=3, b=8;
cout << arttir(a,b);
return 0;
}
// Bir fonksiyon normal şartlarda(pointer veya işarteci kullanılmayan
durumlarda)sadece tek bir değer geriye döndürür.

#include <iostream>
using namespace std;

int main()
{
int sayi, sonuc = 1;
cout << "Sayı gir: ";
cin >> sayi;
if(sayi >= 0){
for(int i=1;i<=sayi;i++)
sonuc = sonuc*i;
}
else
cout << "Sifir ya da eksi sayi girdiniz! " << endl;
cout << "Faktoriyel: " << sonuc << endl;
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int sayi, i=1,sonuc=1;
cout << "Sayi gir: ";
cin >> sayi;
while(i<=sayi){
sonuc*=i;
i++;
}
cout << "Faktoriyel: " << sonuc << endl;

return 0;
}
#include <iostream>
using namespace std;

int topla(int k){


if(k>0){
return k+topla(k-1);
}
else
return 0;
}

int main()
{
cout << topla(3);

return 0;
}
// 3 + topla(2)
// 3 + 2 + topla(1)
// 3 + 2 + 1 + topla(0)
// 3 + 2 + 1 + 0 = 6

#include <iostream>
using namespace std;

int faktoriyel(int k) {
if (k > 1)
return k * faktoriyel(k - 1);
else if (k == 0)
return 1;
else
return k;
}

int main() {
int sayi;
cout << "Sayi gir: ";
cin >> sayi;
cout << "Sayinin faktoriyeli: " << faktoriyel(sayi) << endl;
return 0;
}

#include <iostream>
using namespace std;

int fibo(int n){


if((n==0) || (n==1))
return n;
else
return fibo(n-1) + fibo(n-2);
}

int main()
{
cout << fibo(3);

return 0;
}
// fibo(2) + fibo(1)
// fibo(1) + fibo(0) + 1
// 1 + 0 + 1 = 2

#include <iostream>
using namespace std;

int fibo(int n){


if((n==0) || (n==1))
return n;
else
return fibo(n-1) + fibo(n-2);
}

int main()
{
int n;
cout << "n sayısı: ";
cin >> n;
for(int i=1;i<=n;i++)
cout << fibo(i) << " ";

return 0;
}

#include <iostream>
using namespace std;

int main() {
int i,k;
i = 10;
int *ptr;
ptr = &i;
cout << i << endl; //i nin degeri
cout << ptr << endl; //i nin adresi
cout << "1 sonraki adres: " << ptr+1 << endl;
cout << "1 sonraki adres: " << ++ptr << endl;
cout << "2 sonraki adres: " << ptr+2 << endl;
cout << "i degeri: " << i << endl;
cout << "i nin isaretcisi: " << *ptr << endl;
cout << "i adresi: " << ptr << endl;
cout << "1 sonraki adresteki degeri: " << *(ptr+1) << endl;
return 0;
}
// &k ile adres tanımlayabiliyorsak neden int *ptr (pointera) ihtiyaç var?
// Cevap = Pointer aritmetiği olduğundan dolayı
//Dereferencing = Isaretciler

#include <iostream>
using namespace std;

int main() {
int i,k;
i=10;
k=25;
int *ptr1,*ptr2;
ptr1 = &i;
ptr2 = &k;
cout << "i adresi: " << ptr1 << endl;
cout << "k adresi: " << ptr2 << endl;
cout << "i değeri: " <<*(ptr2+1) << endl;
cout << "k değeri: " <<*(ptr1-1) << endl;
return 0;
}
// ilk k'yı yerlestirmis sonra i'yi
// <-- sagdan sola okunuyor
// &ptr adresi
// *ptr ptr'nin gosterdigi adresteki degeri gösterir!
// ptr isaret ettigi adres

You might also like