Modify the above program to display the two right-most digits of the integral part of the number

//E Balagurusamy, Programming in ANSI C,4E
//Exercise 3.3, p81

//Note:
//. comments with //scaff indicate scaffolding code to help during development of code
//. can be safely commented out/removed (in production code)
//HOW?
//1. extract integral part by explicit type casting to int
//2. use modulus (%) operater once to get the right-most portion
//3. repeat step 2 to get the next left digit
#include <stdio.h>
#include <conio.h>
int main(void)
{
    float number=0.0;

    int number_integral=0, number_integral_rightmost=0, number_integral_second_rightmost=0;
    printf("\nEnter a real number : ");
    scanf("%f", &number);
    //1. extract integral part by explicit type casting to int
    number_integral= (int)number;
    //2. use modulus (%) operater once to get the right-most portion
    number_integral_rightmost= number_integral%10; // why 10? Because base is 10 for decimal
    number_integral_second_rightmost= ((number_integral/10)%10);
    printf("\nThe right-most two digits of integral portion : %d%d", number_integral_second_rightmost, number_integral_rightmost);
    getch();
    return 0;
}

Comments