Type Casting Explorer

Type Casting Explorer

Medium C Variables 31 views
Explanation Complexity

Problem Statement

Cast the float value 9.7 to a float in different ways: implicit, explicit, using floor, using cel. What will be the result in each case and why?

Input Format

A float value: 9.7

Output Format

Result of conversion using:

• implicit cast

• explicit cast

• floor()

• ceil()

Example

9.7
Implicit cast  : 9
Explicit cast  : 9
floor(9.7)     : 9
ceil(9.7)      : 10

Constraints

• Floating point value

• Conversion to integer result

Concept Explanation

Different methods convert 9.7 to an integer in different ways.

Step-by-Step Explanation

1. Implicit casting

• Float is automatically converted to int.

• Decimal part is discarded.

• Result: 9

2. Explicit casting

• Manually convert using (int)9.7.

• Decimal part is discarded.

• Result: 9

3. floor()

• Returns the largest integer less than or equal to the value.

• floor(9.7) → 9

4. ceil()

• Returns the smallest integer greater than or equal to the value.

• ceil(9.7) → 10

Concept Explanation

Different methods convert 9.7 to an integer in different ways.

Step-by-Step Explanation

1. Implicit casting

• Float is automatically converted to int.

• Decimal part is discarded.

• Result: 9

2. Explicit casting

• Manually convert using (int)9.7.

• Decimal part is discarded.

• Result: 9

3. floor()

• Returns the largest integer less than or equal to the value.

• floor(9.7) → 9

4. ceil()

• Returns the smallest integer greater than or equal to the value.

• ceil(9.7) → 10

Input / Output Format

Input Format
A float value: 9.7
Output Format
Result of conversion using:

• implicit cast

• explicit cast

• floor()

• ceil()
Constraints
• Floating point value

• Conversion to integer result

Examples

Input:
9.7
Output:
Implicit cast : 9 Explicit cast : 9 floor(9.7) : 9 ceil(9.7) : 10

Example Solution (Public)

C
#include <stdio.h>
#include <math.h>

int main() {
    float a = 9.7;
    int x, y, z, w;

    /* Implicit casting */
    x = a;

    /* Explicit casting */
    y = (int)a;

    /* Using floor() */
    z = floor(a);

    /* Using ceil() */
    w = ceil(a);

    printf("Original value: %.2fnn", a);

    printf("Implicit casting result = %dn", x);
    printf("Explicit casting result = %dn", y);
    printf("Using floor() result    = %dn", z);
    printf("Using ceil() result     = %dn", w);

    return 0;
}

Official Solution Code

#include <stdio.h>
#include <math.h>

int main() {
    float a = 9.7;
    int x, y, z, w;

    /* Implicit casting */
    x = a;

    /* Explicit casting */
    y = (int)a;

    /* Using floor() */
    z = floor(a);

    /* Using ceil() */
    w = ceil(a);

    printf("Original value: %.2fnn", a);

    printf("Implicit casting result = %dn", x);
    printf("Explicit casting result = %dn", y);
    printf("Using floor() result    = %dn", z);
    printf("Using ceil() result     = %dn", w);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.