1

I'm working on a project where I need to categorize a given character. The issue that I'm having right now is that I've managed to have it categorize the character as either an uppercase or lowercase letter. However, the professor also requires that, if the given character is a digit, I have the program print out the square root of the function. I can't seem to perform the square root function properly with a digit stored as a character, and it keeps returning the wrong value Thank you in advance for your help.

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


  if ((input >= '0') && input <= '9'){
    float squareRoot = sqrt(input);
    printf ("%c is a digit.\nThe square root of %c is %0.2lf", input, input, squareRoot);
  }


  

  return 0;

In this situation, input is stored as a character and I have included the math.h library.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    Take a look at the `int atoi(const char *str)` function, it converts characters into integers. – Zois Tasoulas Sep 22 '21 at 02:22
  • If you have only single digit numbers then you could also do calculations based on ASCII values. That might look as a more hacky solution. Keep in mind that every character is represented by an ASCII number. With some really simple arithmetic calculation and by casting to an integer, you can do the conversion. – Zois Tasoulas Sep 22 '21 at 02:25
  • 2
    Does this answer your question? [How to convert a single char into an int](https://stackoverflow.com/questions/439573/how-to-convert-a-single-char-into-an-int) – Zois Tasoulas Sep 22 '21 at 02:25
  • 1
    @zois That's what I ended up doing. I used the stored digit and subtracted the Ascii value of '0' to get the digit, stored it as an integer, and then used it in the sqrt function. Thank you for the idea! – Jesse Proctor Sep 22 '21 at 02:34

0 Answers0