Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

C programming examples

Operations with 2 integers


int main(void)
{
int first, second, add, subtract, multiply;
float divide;

printf("Please enter two integers\n");


scanf("%d%d", &first, &second);

add = first + second;


subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting

printf("Sum : %d\n",add);
printf("Difference : %d\n",subtract);
printf("Multiplication : %d\n",multiply);
printf("Division : %.2f\n",divide);

return 0;
}
Find Odd or Even (μονά-ζυγά)

#include<stdio.h>

int main(void)
{
int n;

printf("Please give an integer\n");


scanf("%d",&n);

if ( n%2 == 0 )
printf("Integer is Even\n");
else
printf("Integer is Odd\n");

return 0;
}
Παραγοντικό (factorial)(no recursion)

#include <stdio.h>

int main(void)
{
int i, n, fact = 1;

printf("Please enter a positive integer\n");


scanf("%d", &n);

for (i = 1; i <= n; i++)


fact = fact * i;

printf("%d! = %d\n", n, fact);

return 0;
}
Μέγιστο στοιχείο πίνακα με n στοιχεία
#include <stdio.h>

int main(void)
{
int temparray[50], max, size, c, arr_index;

printf("Enter the number of elements in the array\n");


scanf("%d", &size);
printf("Enter %d integers\n", size);

for (c = 0; c < size; c++)


scanf("%d", &temparray[c]);

arr_index = 0;
max = temparray[0];

for (c = 1; c < size; c++)


{
if (temparray[c] > max)
{
max = temparray[c];
arr_index = c;
}
}
printf("Maximum array element is found at location %d with value %d.\n", arr_index, max);
return 0;
}
Μετατροπή θερμοκρασίας Celsius to Fahrenheit

#include<stdio.h>

int main(void)
{
float celsius,fahrenheit;

printf("Enter temperature in Celsius : \n");


scanf("%f",&celsius);

fahrenheit = (1.8 * celsius) + 32;


printf("Temperature in Fahrenheit : %f",fahrenheit);

return 0;
}
Προπαίδεια
#include <stdio.h>

int main()

{
int num, i = 1;
printf("\n Enter an integer:");
scanf("%d", &num);
printf("Multiplication table of %d: \n", num);
while (i <= 10)
{
printf("\n %d x %d = %d", num, i, num * i);
i++;
}
return 0;
}
Πυραμίδα αριθμών
#include<stdio.h>

int main(void)
{
int i,j,num;

printf("Enter an integer :");

scanf("%d",&num);

for(i=0;i<=num;i++)
{
for(j=0;j<i;j++)
printf("%d ",i);
printf("\n");
}
return 0;
}
Μπορούμε να καλέσουμε τον compiler
του VS κατευθείαν από το cmd?

You might also like