Temel C kodları: Bölüm 3

Programlamaya Giriş (Introduction to Programming) temel C kodlarına son bölümümüz devam ediyoruz.

Modülo işlemi ile sayı çözümleme


/*This program prints the ones, tens and hundreds of a three digit number*/

#include <stdio.h>

void main()
{
int number; // input
int modulo; //other variables
int ones, tens, hundreds; // outputs

//Get the three digit number
printf("Please enter a three digit number:");
scanf("%d", &number);

//Calculate the hundreds
hundreds = number / 100;

//Calculate the tens
modulo = number % 100;
tens = modulo / 10;

//Calculate the ones 
ones = modulo % 10;

//Display the ones, tens and hundreds
printf("Ones:%d\nTens:%d\nHundreds:%d\n", ones, tens, hundreds);
}

Toplam parayı dolar olarak gösterir


/*Value of a collection of coins*/

#include <stdio.h>

void main()
{
int pennies, nickels; //inputs
int dimes, quarters; //inputs
char x, y, z; //inputs for initials
int total; //other variables
int dollars, cents; //outputs

//Get the 3 initials
printf("Type in 3 initials and press return > ");
scanf(" %c %c %c", &x, &y, &z);

//Display the initials and get the number of pennies
printf("Welcome %c%c%c, please enter numper of\npennies:",x,y,z);
scanf("%d", &pennies);

//Get the number of nickels
printf("nickels:");
scanf("%d", &nickels);

//Get the number of dimes
printf("dimes:");
scanf("%d", &dimes);

//Get the number of quarters 
printf("quarters:");
scanf("%d", &quarters);

//Calculate the total cents
total = pennies + (nickels * 5) + (dimes * 10) + (quarters * 25);
dollars = total / 100; //Calculate the dollars
cents = total % 100; //Calculate the cents

//Display the value of a collection of coins
printf("\nYou have total %d dollars and %d cents.\n", dollars, cents);
}

Tek mi çift mi?


/* Odd or Even */

#include <stdio.h>

void main()
{
int number; //input

//Get the integer
printf("Please enter an integer:");
scanf("%d", &number);

if(number % 2 == 0) //if it is true number is even
printf("%d is an even number.\nBye bye...\n", number);
else //otherwise number is odd
printf("%d is an odd number.\nBye bye...\n", number);
}

Hangi sayı büyük?


/*which number is larger or are these equal?*/

#include <stdio.h>

void main()
{
int num1, num2; //inputs

//Get the two numbers
printf("Please enter two integers:");
scanf("%d %d", &num1, &num2);

if(num1 > num2) //if number1 larger than number2
printf("%d is larger.\n", num1);
if(num1 < num2) //if number2 larger than number1
printf("%d is larger.\n", num2);
if(num1 == num2) //if the numbers are equal
printf("These numbers are equal.\n");
}

Verilen bir sayının faktörünü yazdırır


/*This program prints the factor of given number*/

#include <stdio.h>

void main()
{
int number; //input
int count = 1, modulo; //other variables

//Get the number
printf("Please enter a number:");
scanf("%d", &number);
//Display the result text
printf("Factors of %d are:\n", number);

while(count < number) //as long as number is greater than count
{
modulo = number % count;
if(modulo == 0) //if it is true count is a factor
{
printf("%d, ", count); //print a factor
} //end of if
count = count + 1; 
} //end of while
printf("%d\n", number); //print last factor (itself)
}

Fibonacci Sayıları


#include <stdio.h>

void main()
{
int a = 1, b = 1;

while(b < 100)
{
printf("%i %i ", a, b);
a = a + b;
b = a + b;
}
}

Etiketler: , , ,