Skip to content

Commit

Permalink
C exercises
Browse files Browse the repository at this point in the history
Answers of CodeForWin C exercises
bcalagoz committed Sep 21, 2022
1 parent 81a9f2e commit 15b4503
Showing 27 changed files with 352 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added codeforwin_c_exercises/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
19 changes: 19 additions & 0 deletions codeforwin_c_exercises/basic_programming_exercises/dnm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>

int main()
{
float cm, meter, km;

/* Input length in centimeter from user */
printf("Enter length in centimeter: ");
scanf("%f", &cm);

/* Convert centimeter into meter and kilometer */
meter = cm / 100.0;
km = cm / 100000.0;

printf("Length in Meter = %.2f m \n", meter);
printf("Length in Kilometer = %.2f km", km);

return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Write a C program to demonstrate input and output of all basic
and derived types. How to use scanf() and printf()
function perform input and output on primitive types in C programming.
*/
#include <stdio.h>

int main()
{
char charVal;
unsigned char uCharVal;

short shortVal;
unsigned short uShortVal;

int intVal;
unsigned int uIntVal;

long longVal;
unsigned long uLongVal;

long long longLongVal;
unsigned long long uLongLongVal;

float floatVal;
double doubleVal;
long double longDoubleVal;

printf("Enter a character: ");
charVal = getchar();
getchar();

printf("Enter another character: ");
uCharVal = getchar();
getchar();

printf("Enter a signed short value: ");
scanf("%hi", &shortVal);

printf("Enter an unsigned short value: ");
scanf("%hu", &uShortVal);

printf("Enter an signed integer value: ");
scanf("%d", &intVal);

printf("Enter an unsigned integer value: ");
scanf("%lu", &uIntVal);

printf("Enter a signed long value: ");
scanf("%ld", &longVal);

printf("Enter an unsigned long value: ");
scanf("%lu", &uLongVal);

printf("Enter a signed long long value: ");
scanf("%lld", &longLongVal);

printf("Enter an unsigned long long value: ");
scanf("%llu", &uLongLongVal);

printf("Enter a float value: ");
scanf("%f", &floatVal);

printf("Enter a double value: ");
scanf("%lf", &doubleVal);

printf("Enter a long double value: ");
scanf("%Lf", &longDoubleVal);



printf("\nYou entered character: '%c' \n", charVal);
printf("You entered unsigned character: '%c' \n\n", uCharVal);

printf("You entered signed short: %hi \n", shortVal);
printf("You entered unsigned short: %hu \n\n", uShortVal);

printf("You entered signed int: %d \n", intVal);
printf("You entered unsigned int: %lu \n\n", uIntVal);

printf("You entered signed long: %ld \n", longVal);
printf("You entered unsigned long: %lu \n\n", uLongVal);

printf("You entered signed long long: %lld \n", longLongVal);
printf("You entered unsigned long long: %llu \n\n", uLongLongVal);

printf("You entered float: %f \n", floatVal);
printf("You entered double: %lf \n", doubleVal);
printf("You entered long double: %Lf \n", longDoubleVal);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Write a C program to input number of days from user
and convert it to years, weeks and days. How to convert
days to years, weeks in C programming. Logic to convert
days to years, weeks and days in C program.
*/


Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Write a C program to input two numbers from user
and calculate their sum. C program to add two numbers
and display their sum as output. How to add two numbers in C programming.
*/
#include<stdio.h>

int main(){

int num1,num2;
int sum;

printf("Enter two integers:");
scanf("%d %d",&num1,&num2);

sum = num1 + num2;

printf("Sum:%d\n",sum);


return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Write a C program to input two numbers and perform all arithmetic operations.
C program to find sum, difference, product, quotient and modulus of two given numbers.
*/

#include<stdio.h>

int main(){

int num1,num2;
int sum,dif,product,mod;
float quotient;

printf("Enter two numbers:");
scanf("%d %d",&num1,&num2);

sum = num1 + num2;
printf("Sum:%d\n",sum);

dif = num1 - num2;
printf("Difference:%d\n",dif);

product = num1 * num2;
printf("Product:%d\n",product);

quotient = (float)num1 / num2;
printf("Quotient:%.2f\n",quotient);

mod = num1 % num2;
printf("Mod:%d\n",mod);

return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Write a C program to input length and width of
a rectangle and calculate perimeter of the rectangle.
How to find perimeter of a rectangle in C programming.
Logic to find the perimeter of a rectangle if length
and width are given in C programming.
*/

#include<stdio.h>

int main(){

int legth,width;
int perimeter;

printf("Enter legth:");
scanf("%d",&legth);

printf("Enter width:");
scanf("%d",&width);

perimeter = (2*legth)+(2*width);

printf("Perimeter is %d\n",perimeter);


return 0;
}

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Write a C program to input length and width of
a rectangle and find area of the given rectangle.
*/

#include<stdio.h>

int main(){

int legth,width;
int area;

printf("Enter legth:");
scanf("%d",&legth);

printf("Enter width:");
scanf("%d",&width);

area = legth * width;

printf("Area is %d\n",area);


return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Write a C program to input radius of a circle from user
and find diameter, circumference and area of the circle.
How to calculate diameter, circumference and area of
a circle whose radius is given by user in C programming.
Logic to find diameter, circumference and area of a circle in C.
*/

#include<stdio.h>
#define PI 3.14
int main(){

float r;
float R,circ,area;

printf("Enter radius:");
scanf("%f",&r);

R = 2 * r;
circ = 2 * PI * r;
area = PI * r * r;

printf("Diameter:%.1f\n",R);
printf("Circumference:%.1f\n",circ);
printf("Area:%.2f\n",area);

return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Write a C program to input length in centimeter
and convert it to meter and kilometer. How to convert length
from centimeter to meter and kilometer in C programming.
Length conversion program in C from centimeter to meter and centimeter to kilometer.
*/

#include<stdio.h>

int main(){

int length;

printf("Length (cm):");
scanf("%d",&length);

printf("%dcm is %.2fm\n",length,(length/100.0));
printf("%dcm is %.2fkm\n",length,(length/100000.0));


return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Write a C program to input temperature in Centigrade
and convert to Fahrenheit. How to convert temperature from degree
centigrade to degree Fahrenheit in C programming. Logic to convert
temperature from Celsius to Fahrenheit in C.
*/
#include<stdio.h>

int main(){

float tempC,fah;

printf("Enter temperature in Celsius:");
scanf("%f",&tempC);

fah = (tempC * 1.8) + 32;

printf("Temperature in Fahrenheit:%.2f\n",fah);


return 0;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Write a C program to input temperature in degree Fahrenheit
and convert it to degree Centigrade. How to convert temperature
from Fahrenheit to Celsius in C programming. C program for
temperature conversion. Logic to convert temperature from
Fahrenheit to Celsius in C program.
*/

#include<stdio.h>

int main(){

float celsius,fahrenheit;

printf("Enter temperature in degree Fahrenheit:");
scanf("%f",&fahrenheit);

celsius = (fahrenheit-32)*5/9;

printf("%.2f Fahrenheit = %2.f Celsius",fahrenheit,celsius);

return 0;
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Write a C program to input any number from user and check whether
the Least Significant Bit (LSB) of the given number is set (1) or not (0).
How to check whether the least significant bit of a number is set
or unset using bitwise operator in C programming. C program to get
the status of least significant bit of a number.
*/

#include<stdio.h>

int main(){

int x;

printf("Enter any integer:");
scanf("%d",&x);

if (x & 1)
{
printf("LSB of %d is is set (1)\n", x );
}
else{
printf("LSB of %d is is unset (0)\n", x );
}



return 0;
}

0 comments on commit 15b4503

Please sign in to comment.