1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Gre4nikov [31]
3 years ago
15

How to solve a program that accepts a number as input and prints just the decimal portion

Computers and Technology
2 answers:
Mrrafil [7]3 years ago
7 0

Answer:

We can extract the decimal part of a number by casting the variable

Explanation:

Type casting is the process of  converting one datatype into another one. Basically, most of the languagues support type casting and we can use it in advantage to get the decimal part of a number.  Let assume you have a decimal number stored in your code, then let's do the following:

int integer_part = (int) float_number;

This is  a typlical way to cast a float number into a integer one. By doing so, you are getting the integer part of a float number while the decimal portion  is discarded.

Now you have a float number  and a variable with its integer part. Decimal portion is computed as follows:

float decimal_part = float_number - integer_part;

You can follow this scheme in any programming language with strong typing (datatypes  required to define a variable) and it will work ok.

lara31 [8.8K]3 years ago
3 0

<u>Answer:</u>

<em>There are 2 ways to do extract the decimal part: </em>

<u>Explanation:</u>

  • <em>First method using number </em>

<em>int main() { </em>

<em>  double doublenum = 24.535; </em>

<em>  int integerpart = (int)doublenum; </em>

<em>  double decimalpart = num - integerpart; </em>

<em>  printf(""Num = %f, integerpart = %d, decimalpart = %f\n"", doublenum, integerpart, decimalpart); </em>

<em>} </em>

  • <em>Second method using string: </em>

<em>#include <stdlib.h> </em>

<em>int main() </em>

<em>{ </em>

<em>    char* inStr = ""193.789"";          </em>

<em>    char* endptrvar;                      </em>

<em>    char* loc = strchr(inStr, '.'); </em>

<em>    long mantissaval = strtod(loc+1, endptrvar); </em>

<em>    long wholenum = strtod(inStr, endptrvar); </em>

<em>    printf(""whole: %d \n"", wholenum);      </em>

<em>    printf(""mantissa: %d"", mantissaval);   </em>

<em>} </em>

You might be interested in
What does FLUX do when soldering an electrical joint?
Strike441 [17]

Answer:

Flux is an acidic blend that makes a difference evacuate oxides from the range of the joint and so makes a difference the patch stream effectively over the joint and frame a great bond. The flux can be seen as a brown fluid as a patch is warmed, and it in some cases gives off a impactful smoke that can act as a aggravation.

3 0
3 years ago
Read 2 more answers
Implement a container class Stat that stores a sequence of numbers and provides statistical information about the numbers. It su
olga2289 [7]

Answer:

See explaination

Explanation:

Definition of Class 1:

class Stat:

def __init__(self, li):

self.li = li

def add(self, value):

self.li.append(value)

def __len__(self):

return len(self.li)

def min(self):

try:

return min(self.li)

except:

return "EmptyStatError: empty Stat does not have a min"

def max(self):

try:

return max(self.li)

except:

return "EmptyStatError: empty Stat does not have a max"

def sum(self):

return sum(self.li)

def mean(self):

try:

return float(sum(self.li))/float(len(self.li))

except:

return "EmptyStatError: empty Stat does not have a mean"

def __getitem__(self):

return self.li

def clear(self):

del self.li[:]

Definition of Class 2:

class intlist:

def __init__(self, li):

self.li = li

def append(self, value):

if type(value) == int:

self.li.append(value)

else:

print "NotIntError: Input is not an Integer."

def insert(self, index,value):

if type(value) == int:

self.li.insert(index, value)

else:

print "NotIntError: Input is not an Integer."

def extend(self, value):

i = 0

for temp in value:

if type(temp) == int:

i = i

else:

i = i+1

if i==0:

self.li.extend(value)

else:

print "NotIntError: Input is not an Integer."

def __setitem__(self, index, value):

self.insert(index, value)

def __getitem__(self, index):

return self.li[index]

def odds(self):

lis = []

for temp in self.li:

if temp%2 == 1:

lis.append(temp)

return lis

def evens(self):

lis = []

for temp in self.li:

if temp%2 == 0:

lis.append(temp)

return lis

Class 1 call:

s = Stat([])

s.add(2.5)

s.add(4.7)

s.add(78.2)

print len(s)

print s.min()

print s.max()

print s.sum()

print s.mean()

print s.li

s.clear()

print s.li

print len(s)

print s.min()

print s.max()

print s.mean()

print s.sum()

Class 2 call:

intl = intlist([])

print intl.li

intl = intlist([1,2,3])

print intl.li

intl.append(5)

print intl.li

intl.insert(1,99)

print intl.li

intl.extend([22,44,66])

print intl.li

print intl.odds()

print intl.evens()

print intl.li

intl[2] = -12

print intl[4]

6 0
4 years ago
A ____ is a copy that is kept in case values need to be restored to their original state.
Zepler [3.9K]
A backup is the copy made in this case
5 0
4 years ago
Which ie tab is used to configure how to manage cookies?
Virty [35]
Hmm I think it's the favorite bar
4 0
3 years ago
01110111 01101000 01100001 01110100 00100000 01101001 01110011 00100000 00110001 00101011 00110001
Lera25 [3.4K]
Same lol thesis so me right now
7 0
4 years ago
Other questions:
  • Travis completes his assignments on a word processor. He wants to make sure that his documents are free from spelling or grammat
    9·2 answers
  • Programmers often author which type of information to guide test runs?
    10·1 answer
  • A hub transmits all incoming messages to all of its ports except the port where the messages came in. A switch usually sends mes
    14·2 answers
  • Explain how the operating system controls the software and hardware on the computer?
    5·1 answer
  • Which category of management information systems (MIS) combines the forecasting capability of decision support software with the
    12·1 answer
  • Knowing and understanding the targeted audience is a main compnent to creating a website
    15·1 answer
  • Which feature is used to help identify the appropriate content for particular form fields?
    9·1 answer
  • Your ISP connects to the core routers of the Internet via a O backbone O satellite O cable mode Ospine​
    9·1 answer
  • Look over the image in the top section and choose which piece from the bottom should be placed in the question mark (?) that wou
    11·1 answer
  • Say true or false. a)ICT in education is used as teaching learning aid
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!