Temel C kodları: Bölüm 1

Hazırlık sınıfını bitirmemle beraber bu sene bilgisayar mühendisliği bölümünde ilk yılım. Bu yazı dizisinde ilgilenenler için CSE101 Introduction to Programming (Programlamaya Giriş) dersinin pratik saatlerinde yazdığımız basit C programlarının kaynak kodlarını paylaşacağım. Zaten kodlar çok temel olduğundan ayrıca anlatıma gerek duymadım, kodlardaki yorumlar yeterli olacaktır. Anlaşılmayan kısımlar için yorum yazbilirsiniz. İyi çalışmalar.

Mil olarak mesafeyi kilometreye çevirir


/* Converts distance in miles to kilometers. */

#include <stdio.h> /* printf, scanf definitions */
#define KMS_PER_MILE 1.609 /* conversion constant*/

int main(void)
{
double miles, /* input - distance in miles. */
kms; /* output - distance in kilometers */

/* Get the distance in miles. */
printf("Enter the distance in miles> ");
scanf("%lf", &miles);

/* Convert the distance to kilometers. */
kms = KMS_PER_MILE * miles;

/* Display the distance in kilometers. */
printf("That equals %f kilometers.\n", kms);

return (0);
}

Dairenin çevresini ve alanını hesaplar


/* Calculates circumference and area of circle */

#include <stdio.h> /* printf, scanf definitions */
#define PI 3.14 /* conversion constant */

void main()
{
double radius; //input
double circum; //output
double area; //output

/* Get the radius */
printf("Enter the radius> ");
scanf("%lf", &radius);

/* Calculate the circumference */
circum = PI * radius * 2;

/* Calculate the area*/
area = PI * radius * radius;

/* Display the circimference */
printf("Circumference is %.4f \n", circum);

/* Display the area */
printf("Area is %.4f \n" , area);
}

Basit hesap makinesi


/* Calculates sum, difference, product and quotient of two integer */

#include <stdio.h>
void main()
{
int x; //input
int y; //input
int result; //output

/* Get the first integer */
printf("Please enter the first integer:");
scanf("%d", &x);

/* Get the second integer */
printf("Please enter the second integer:");
scanf("%d", &y);

/*Display the sum*/
printf("%d+%d=%d\n", x,y,result=x+y);

/*Display the difference*/
printf("%d-%d=%d\n", x,y,result=x-y);

/*Display the product*/
printf("%d*%d=%d\n", x,y,result=x*y);

/*Display the quotient*/
printf("%d/%d=%d\n", x,y,result=x/y);
}

Bazı değerleri gösterir - Değişken tanımlama


/* Displays some values */

#include <stdio.h> /* printf, scanf definitions */
void main()
{
char char1; //input
int int1; //input
double double1; //input
char char2='?'; //output
int int2=-34; //output

/* Get a character */
printf("Please enter a character:");
scanf("%c", &char1);

/* Get a integer */
printf("Please enter a integer:");
scanf("%d", &int1);

/* Get a double */
printf("Please enter a double:");
scanf("%lf", &double1);

/*Display the values*/
printf("%c %d %.2lf %c %d\n", char1, int1, double1, char2, int2);
}

Etiketler: , , ,