C language How to take the whole

Source: Internet
Author: User
Number: QA003792
Date established: January 18, 2001 Last Modified Date: August 22, 2005
Category: C + +-other aspects
Operating system: Win98
Programming Tools: TC 2.0
Problem: Handle each of the 4-digit numbers, each digit plus 7, then divide by 10 and take the remainder.
My idea is to use this four-digit ABCD. Divided by 1000, get a A, then the remainder except 100 get B, the remainder except 10, get C,
.......
But I don't know how to do the rounding. Can you help me, please?
Level: Just getting Started (cflanker)
The C language has the following several rounding methods:
1, the direct assignment to the integer variable. Such as:
int i = 2.5; or i = (int) 2.5;
This method uses a fractional part that can be used for your problem.
2. The integer division Operator "/" in C + + has its own rounding function (Int/int), and the return value of the rounding function described below is double. Integer division to the positive number of the rounding off the decimal part, can be used for your problem. But integer division is concerned with the rounding result of negative numbers and the C compiler used.
3, use the floor function. Floor (x) returns the largest integer less than or equal to X. Such as:
Floor (2.5) = 2
Floor (-2.5) =-3
4, use the Ceil function. Ceil (x) returns the smallest integer greater than X. Such as:
Ceil (2.5) = 3
Ceil (-2.5) =-2
Floor () is rounded to negative infinity, floor ( -2.5) = -3;ceil () is rounded to positive infinity, ceil (-2.5) =-2. The floor function can be used for your problem.

Hyh's Opinion:
int x,a,b,c,d;
a=x/1000;
b=x%1000/100;
C=X%100/10;
d=x%10;

Guo Li: Several experts explain the floor and ceil two functions can only be applied in TC 2.0, I tried in VC + +, do not recognize this function, is not to include a special header file AH. Please advise the experts, I am a novice. Thank you, sir.
Answer: These two functions can also be used in VC + +, but you need to include MATH.H files when you use them. You can add the following code at the beginning of your program:
#include <math.h>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.