0% found this document useful (0 votes)
1 views11 pages

C Coding Examples

Problems and solutions for C Coding

Uploaded by

morearnayem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views11 pages

C Coding Examples

Problems and solutions for C Coding

Uploaded by

morearnayem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

P1. Read and print an integer, a float, and a character on separate lines.

/* Assignment: 1 | Problem: P1 | Name: Araf at Rahman Nayem


Concept tested: Input, Output and Data Types
Short explanation of approach: Using scanf to read three dif f erent data types and printf to display them.*/

#include <stdio.h>

int main()
{
int a; // int: To store a integer without decimals.
f loat b; // f loat: To store numbers with f ractional parts.
char c; // char: To store a single character

printf ("Enter an integer: ");


scanf ("%d", &a); // %d: Format specif ier f or 'int'.
/*'int' type because it represents a signed base-10 integer.*/

printf ("Enter a f loat: ");


scanf ("%f ", &b); //%f : Format specif ier f or 'f loat'.
/*'f loat' because it interprets input as a decimal f ractional number.*/

printf ("Enter a character: ");


scanf (" %c", &c); //%c: Format specif ier f or 'char'.
/*'char' type because it tells the computer to process the next available
single byte as a character.*/

// Printing the results on separate lines


printf ("%d\n", a);
printf ("%f \n", b);
printf ("%c\n", c);

return 0;
}
P2. Read two integers and print their sum, difference, product, and quotient.

/* Assignment: 1 | Problem: P2 | Name: Araf at Rahman Nayem

Concept tested: Arithmetic Operators


Short explanation of approach: Perf orming basic math on two given integers.*/

#include <stdio.h>

int main() {
int num1, num2; // int: To store a whole number f or arithmetic operations.

printf ("Enter two integers: ");


scanf ("%d %d", &num1, &num2); // %d: Format specif ier f or 'int'

// Calculating and printing results


printf ("Sum: %d\n", num1 + num2);
printf ("Dif f erence: %d\n", num1 - num2);
printf ("Product: %d\n", num1 * num2);

/* Integer division truncation: In C, dividing two ints results in an int.


7/2 results in 3. This is because the f ractional part (.5) is truncated (dropped),
not rounded. 7.0/2 = 3.5 because r643543using a f loat prevents truncation.*/
printf ("Quotient: %d\n", num1 / num2);

return 0;
}
P3. Read a temperature in Celsius and convert to Fahrenheit. Print with 2
decimal places.

/ * Assignment: 1 | Problem: P3 | Name: Araf at Rahman Nayem


Concept tested: Temperature Conversion and Float
Short explanation of approach: Taking Celsius as a f loat and applying the conversion f ormula to get
Fahrenheit.*/

#include <stdio.h>

int main()
{
f loat celsius; // f loat: To store temperature (which of ten requires decimal precision)
f loat f ahrenheit; // f loat: To store the calculated result (which of ten requires decimal precision)
printf ("Enter temperature in Celsius: ");

scanf ("%f ", &celsius); // %f : Format specif ier f or 'f loat'.


/*It matches the 'f loat' type to read the input as a decimal number.*/

/* Why use f loat?


Temperatures are rarely exact whole numbers. Using 'int' would cause
signif icant precision loss during the 9/5 division (which would truncate to 1).*/

/* Where does the f ormula F = (C * 9/5) + 32 come f rom?


1. The f reezing point of water is 0�C and 32�F (hence the +32).
2. The boiling point of water is 100�C and 212�F.
3. The range between f reezing and boiling is 100 units in Celsius and 180 units in Fahrenheit.
4. The ratio of these scales is 180/100, which simplif ies to 9/5.*/

f ahrenheit = (celsius * 9 / 5) + 32;

printf ("Temperature in Fahrenheit: %.2f ", f ahrenheit); // %.2f : Format specif ier f or 'f loat'.
/*The '.2' modif ier tells printf to round the output to 2 decimal places.*/

return 0;
}
P4. Read a character and print its ASCII value. Then read an integer and print its
ASCII character.

/* Assignment: 1 | Problem: P4 | Name: Arafat Rahman Nayem


Concept tested: Type Casting
Short explanation of approach: Using printf format specifiers to implicitly cast, and showing explicit casting between
char and int.*/

#include <stdio.h>

int main()
{
char char1; // char: Chosen to store a single character. Internally, C stores this as a small integer.
int Ascii1; // int: Chosen to store a whole number representing a character's code in the ASCII table.

// Task Part 1: Char to ASCII


printf("Enter a character: ");
scanf(" %c", &char1);// %c: Format specifier for 'char'.
/*'char' because it captures a single character from the input stream.*/

printf("ASCII value: %d\n", char1);// %d: Format specifier for 'int'.


/*'int' because to see the numeric representation of the character's memory value.*/

// Task Part 2: ASCII to Char


printf("Enter an ASCII integer: ");
scanf("%d", &Ascii1); //%d: Format specifier for 'int'.
/*'int' because it reads the input as a base-10 signed integer.*/

printf("Character: %c\n", (char)Ascii1); // %c: Format specifier for 'char'.


/*Used here to display the character that corresponds to the numerical ASCII code.*/

/* What does casting between char and int do?


Casting tells the compiler to temporarily treat one data type as another.
Since 'char' is essentially a 1-byte integer, casting an 'int' to a 'char'
simply looks up that number on the ASCII table.

What does (char)65 mean?


It is an explicit cast that converts the integer 65 into its character
equivalent. In ASCII, 65 represents the uppercase letter 'A'.*/

return 0;
}
P5. Read a 3-digit integer. Extract and print its hundreds, tens, and units
digits using only arithmetic.
/* Assignment: 1 | Problem: P5 | Name: Araf at Rahman Nayem
Concept tested: Digits separation by hundreds, tens and units
Short explanation of approach: Separating digits using division and modulo operators.*/

#include <stdio.h>

int main()
{
int number, hundreds, tens, units; // int: To store the integer inputs.

printf ("Enter a 3-digit integer: ");


scanf ("%d", &number); /* %d: Format specif ier f or 'int'.
'int' because it instructs the program to read a signed decimal integer.*/

/* How does integer division get hundreds?


When divided an integer by 100 (number / 100), C language perf orms integer division,
which discards the remainder. For example, 352 / 100 = 3.52, but the
decimal (.52) is truncated, leaving exactly the hundreds digit.*/

hundreds = number / 100;

/* To get the tens digit:


First, (number / 10) removes the last digit (e.g., 352 becomes 35).
Then, % 10 takes the remainder of 35 / 10, which is 5.*/

tens = (number / 10) % 10;

/* How does % get units?


The modulo operator (%) returns the remainder of a division.
Dividing any number by 10 (number % 10) leaves the last digit
as the remainder (e.g., 352 / 10 = 35 with a remainder of 2).*/

units = number % 10;

printf ("Hundreds: %d\n", hundreds);


printf ("Tens: %d\n", tens);
printf ("Units: %d\n", units);// %d: To display the extracted whole-number digits. */

return 0;
}
P6. Swap two variables WITHOUT using a third variable. Comment each step
showing how the math works.
/* Assignment: 1 | Problem: P6 | Name: Araf at Rahman Nayem
Concept tested: Variable Swapping
Short explanation of approach: Using addition and subtraction to swap two values in place.*/

#include <stdio.h>

int main()
{
int a, b; // int: To store a whole number.

printf ("Enter two integers (a and b): ");


scanf ("%d %d", &a, &b); /* %d: Format specif ier f or 'int'.
'int' because it instructs the program to read integers.*/

printf ("Bef ore swap: a = %d, b = %d\n", a, b);

/* Mathematical identity behind the trick:


Step 1: a = a + b (a now holds the sum)
Step 2: b = a - b (Sum minus original b leaves original a. Now b holds a's original value)
Step 3: a = a - b (Sum minus new b leaves original b. Now a holds b's original value)*/

a = a + b;
b = a - b;

a = a - b;

printf ("Af ter swap: a = %d, b = %d\n", a, b); // %d: Used here to print the updated whole-number values
of a and b.

return 0;
}
P7. Read radius of a circle. Print its area and circumference. Define pi using
#define.

/* Assignment: 1 | Problem: P7 | Name: Araf at Rahman Nayem


Concept tested: Def ining Constants
Short explanation of approach: Def ining PI as a constant and calculating area and circumf erence.*/

#include <stdio.h>
#def ine PI 3.14159

/* Why #def ine is used instead of a variable?


#def ine creates a symbolic [Link] a variable, it does not
occupy a memory location during program execution. Instead, the
preprocessor replaces every occurrence of 'PI' with '3.14159'
bef ore the code is actually compiled. This makes the code more
readable and ef f icient.*/

int main()
{
f loat radius, area, circumf erence; // f loat: To allow f or decimal input.

printf ("Enter the radius of the circle: ");


scanf ("%f ", &radius);/* %f : Format specif ier f or 'f loat'.
'f loat' because it reads the input as a f loating -point number. */

// Calculations
area = PI * radius * radius;
circumf erence = 2 * PI * radius;

printf ("Area: %f \n", area);// %f : Format specif ier f or 'f loat'.


printf ("Circumf erence: %f \n", circumf erence);// %f : Format specif ier f or 'f loat'.

return 0;
}
P8. Read a 4-digit year and print which century it belongs to.

/* Assignment: 1 | Problem: P8 | Name: Araf at Rahman Nayem


Concept tested: Calculating Century
Short explanation of approach: Dividing the year by 100 and adding 1 to f ind the century.*/

#include <stdio.h>

int main()
{
int year, century; // int: To store a whole number.

printf ("Enter a 4-digit year: ");


scanf ("%d", &year);// %d: Format specif ier f or 'int'.
/*'int' because it instructs the compiler to read a signed decimal integer. */

/* What does integer division by 100 do?


In C language, dividing an integer by 100 removes the last two digits (the tens and units).
Ex: 1995 / 100 = 19. This tells how many f ull hundred -years have been completed bef ore the current
one.*/

/* Why add 1 f or the century?


The scale starts at 1, not 0. The 1st century covers years 1 to 100.
If divided year 50 by 100, the result is 0. To correctly identif y it
as the "1st" century, we must add 1. This logic applies f orward:
1995 / 100 = 19 (completed centuries), + 1 = 20th century.*/

century = (year / 100) + 1;

printf ("The year %d belongs to the %d century.", year, century);// %d: To print the 'int' values f or the input

return 0;
}
P9. Read two floats. Print their sum, then print the integer part and decimal
part separately.

/* Assignment: 1 | Problem: P9 | Name: Araf at Rahman Nayem


Concept tested: Casting
Short explanation of approach: Casting a f loat sum to an int to truncate it, then subtracting to f ind the
decimal.*/

#include <stdio.h>

int main() {
f loat num1, num2, sum; // f loat: To store a decimal number
int int1; // int: To store only the whole number portion af ter truncation.
f loat deci1; // f loat: To store the remaining f ractional value.

printf ("Enter two f loating-point numbers: ");


scanf ("%f %f ", &num1, &num2);// %f : Format specif ier f or 'f loat'

sum = num1 + num2;

/* How does (int) casting truncate?


Type casting using (int) tells the compiler to treat the f loat value as an integer.
This process "truncates" the number, meaning it simply chops of f all digits
af ter the decimal point without rounding. Ex: 5.99 becomes 5. */

int1 = (int)sum;

/* What is the decimal part f ormula?


The f ormula is: Decimal Part = Original Value - Integer Part.
By subtracting the whole number f rom the total sum, only the f ractional
remainder remains. */

deci1 = sum - int1;

printf ("Sum: %f \n", sum);// %f : To print the f ull f loat value

printf ("Integer Part: %d\n", int1); //%d: Format specif ier f or 'int'.
/*'int' because it prints the truncated whole number as a decimal integer. */

printf ("Decimal Part: %f ", deci1);// %f : Format specif ier f or 'f loat'
/* %f : because the 'f loat' type of the calculated f ractional remainder. */

return 0;
}
P10. Read a 5-digit integer and print it in reverse order using only arithmetic .

/* Assignment: 1 | Problem: P10 | Name: Araf at Rahman Nayem


Concept tested: Reversing order of a number
Short explanation of approach: Repeatedly using modulo 10 to extract digits, and division by 10 to remove
them.*/

#include <stdio.h>

int main() {
int num, d1, d2, d3, d4, d5; // int: To store a number

printf ("Enter a 5-digit integer: ");


scanf ("%d", &num); // %d: Format specif ier f or 'int'.
/*'int' because it reads the input as a signed decimal integer. */

//Mathematical Process:

/* num % 10 isolates the 1st digit f rom the right (Units).


Ex: 12345 % 10 = 5.*/

d1 = num % 10;

/* (num / 10) shif ts the number right (removes the last digit).
% 10 then isolates the new last digit (Tens).
Ex: (12345 / 10) = 1234; 1234 % 10 = 4. */

d2 = (num / 10) % 10;

/* (num / 100) removes the last two digits.


% 10 isolates the new last digit (Hundreds).
Ex: (12345 / 100) = 123; 123 % 10 = 3. */

d3 = (num / 100) % 10;


/* (num / 1000) removes the last three digits.
% 10 isolates the new last digit (Thousands).
Example: (12345 / 1000) = 12; 12 % 10 = 2. */

d4 = (num / 1000) % 10;

/* (num / 10000) removes the last f our digits.


Since it's a 5-digit number, this leaves only the lef tmost digit (Ten-thousands).
Example: 12345 / 10000 = 1. */

d5 = num / 10000;

// Printing the digits in reversed sequence

printf ("Reversed: %d%d%d%d%d\n", d1, d2, d3, d4, d5); // %d: Format specif ier f or 'int'.

return 0;
}

You might also like