1. Common task: Given the four-digit number (for example 5678), display the numbers in reverse order of which is the number of member. That is, we should see on the screen 8765. Tip: to take from among the individual numbers, should be applied to the modulo 10.
Show code
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intmainNumber=5678;
cout<<"Дано целое число: "<<mainNumber<<endl;
cout<<"Число наизнанку: ";
// остаток от деления четырехзначного числа 5678 на 10
cout<<mainNumber%10;// 5678 % 10 = 8
// далее делим mainNumber на 10 и записываем в переменную
// так как тип переменной int, дробная часть отбросится
// и mainNumber будет равен 567 (а не 567,8)
mainNumber/=10;
// показываем остаток от деления 567 на 10 на экран
cout<<mainNumber%10;
mainNumber/=10;
cout<<mainNumber%10;
mainNumber/=10;
cout<<mainNumber%10;
mainNumber/=10;
cout<<endl<<endl;
return0;
}
Result:
2. The site of almost any commercial bank, you can find the so-called Deposit calculator, which allows people to, not wishing to go into the formula for calculating interest rates, to know how much they will receive. To do this, they just fill in certain fields, press the button and see the result. This is a simple program, which has already been able to write each one of you. So, a task: The user enters the amount of the deposit and the number of months of keeping money in the bank. It is necessary to calculate and show the screen profit from the deposit in a month, for the entire term of the deposit, and the total amount payable at the end of the period. Currency let it be – U.S. dollar. Interest rate – 5% APR. The formula for calculating percent per month– SumDeposit * (interest rate / 100) / daysperyear * dayspermonths.
You do not need to batter theory… And it is necessary to solve the problem, tackle and solve. What if “nothing” does not exceed, you need to specifically to ask questions – what there exits.
The idea, that you not a number, a symbolic representation of the number, character string. And in this case, the decision is correct.
But if the number is entered, and even as a string, and, for example, calculated, is here already, this approach does not work.
This decision really is steeper and more compact. Only good, If the user is direct in the performance provided the opportunity to enter a number, type: …… char number[ ] = “0”; cin>>number; …… cout << "Это же число в обратном порядке: " << number[3] << number[2] << number[1] << number[0] << endl; ……
[code] int main() { setlocale(LC_ALL, “rus”); int a = 0; cout <> a; int b = 10; int c = a % b; int d = a / b; int e = d % b; int f = d / b; int g = f % b; int h = f / b; cout << "в обратном порядке цифры из которых сосотит это число – " <<c<<e<<g<<h<< endl; cout << "\n"; return 0;
} [/code]
#include
using namespace std; int main() { setlocale(LC_ALL,”rus”); int first = 5678; int second = 3087; cout << " Result " << first + second << endl;
formula, which forms a new number X:
X = X * 10 + (a % 10), where a – original number.
#include
using namespace std;
int main() {
int x = 5678;
int reverse_x = 0;
while (x) {
reverse_x = reverse_x * 10 + x % 10;
x /= 10;
}
cout << reverse_x << endl;
return 0;
}
And I have nothing turns out I just did not understand
I do not know how much to chisel theory
//—————————————————————————
#include
#pragma hdrstop
#include
#include
#include
//—————————————————————————
using namespace std;
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
double sum, res, srok;
cout<<"5% prozentna stavka v RIK!!!"<<endl;
cout<>sum;
cout<>srok;
res=sum*0.05*(srok / 12);
cout<<"Zarobitok za vesy termin = "<<res<<"$"<<endl;
cout<<"Zarobitok za misyaz = "<<res / srok<<"$"<<endl;
cout<<"Zarobitok za misyaz = "<<res+sum<<"$"<<endl;
system("pause");
return 0;
}
//—————————————————————————
You do not need to batter theory…
And it is necessary to solve the problem, tackle and solve.
What if “nothing” does not exceed, you need to specifically to ask questions – what there exits.
P.S. Discuss the details here uncomfortable (due to engine), but it is possible to forum:
http://rus-linux.net/forum/viewforum.php?f=31
#include
using namespace std;
int main()
{
char number[] = "5678";
setlocale(LC_ALL, "rus");
cout << "Данное число: " << number << endl;
cout << "Это же число в обратном порядке: " << number[3] << number[2] << number[1] << number[0] << endl;
system("pause");
return 0;
}
I prefer this solution)
Or in the sense it was, to be performed using modulo?
The idea, that you not a number, a symbolic representation of the number, character string. And in this case, the decision is correct.
But if the number is entered, and even as a string, and, for example, calculated, is here already, this approach does not work.
This decision really is steeper and more compact.
Only good, If the user is direct in the performance provided the opportunity to enter a number, type:
……
char number[ ] = “0”;
cin>>number;
……
cout << "Это же число в обратном порядке: " << number[3] << number[2] << number[1] << number[0] << endl;
……
[code]
int main()
{
setlocale(LC_ALL, “rus”);
int a = 0;
cout <> a;
int b = 10;
int c = a % b;
int d = a / b;
int e = d % b;
int f = d / b;
int g = f % b;
int h = f / b;
cout << "в обратном порядке цифры из которых сосотит это число – " <<c<<e<<g<<h<< endl;
cout << "\n";
return 0;
}
[/code]
#include
using namespace std;
int main()
{
setlocale(LC_ALL,”rus”);
int first = 5678;
int second = 3087;
cout << " Result " << first + second << endl;
system("pause");
return 0;
}
how could
My solution to the first problem.
It works with different numbers of characters including.
unsigned int iNumber(0);
unsigned int iCurrent_digit;
unsigned int iNumber_temp;
int iBit (0);
cout <> iNumber)
{
if (iNumber >= 1 && iNumber <= 4294967295) break;
else cerr << "\nЧисло не из указанного диапазона. Введите ещё раз\n";
}
cout < 0; iBit++)
{
iNumber_temp = iNumber_temp / 10;
}
// Выводим числа в обратном порядке
for ( ; iBit > 0 ; iBit--)
{
iCurrent_digit = iNumber % 10;
if (iCurrent_digit == 0) break;
cout << iCurrent_digit;
iNumber /= 10;
}
strangely, but the parser cut the part of the code and with brackets and << I worked. sorrow ;).
This problem has been solved. Add the code in the code between the comments and / code in brackets
The first task,as an option.
[code]
#include
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “rus”);
int a = 5678;
int b;
cout << "Данное число: " << a << endl;
cout << "Число наоборот: " ;
for (int i(0); i < 4 ; i )
{
b = (a%(int)(pow(10,i+1)))/(int)pow(10,i);
cout << b;
}
_getch();
return 0;
}
[/code]
How to share modulo I honestly did not think .
But from the deposit calculating sort of made it even easier =)
Задано ціле число A. Перевірити істинність висловлення: «Число
A є непарним та негативним» Как сжелать этот код в с ++?