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.
// умножаем % за 1 месяц на весь срок депозита и записываем в profit
profit*=amountMonth;
cout<<profit<<" $"<<endl;
cout<<"Общая сумма выплаты в конце срока: "
<<sumDeposit+profit<<" $";
cout<<endl<<endl;
return0;
}
Result:
Perhaps you have any questions about the solution of tasks – ask them in the comments!
4.3
50
107 thoughts on “Tasks: arithmetic operations in C ++”
float stavka = 1.05; int term = 0; float deals = 0; float dohodVmes = 0; int deposit = 0; cout << "введите сумму вклада" <> deposit; cout << "введите срок депозита" <> srok; incomeIntermediate = (stavka*vklad-vklad) / 12; agreements = (vklad*srok*stavka)/12; cout << "ваша прибыль в месяц = " << incomeIntermediate << endl; cout << "сума к выплате в конце срока= " << agreements << endl;
Hello…sorry of course, I’m like this right away and with comments ))) My version is more correct. The whole point is, that your monthly profit is not calculated correctly. You are attached to the number of days, but it is different in different months! Therefore, the calculation will not be entirely correct in the end.. And banks count monthly. Your option will be considered correctly if you divide your monthly income into 12 months and take into account the number of days in each. If 1000 dollars under 5% put it for a year, then there will be profit 50 u.e )) ..whatever one may say ))
int main() { setlocale(LC_ALL , “Russian”); short int a, b, c, d, e; a=b=c=d=0; cout << "Введите 4 значительное число \t:" <> e ; d = e; c = e/10; b =e/10/10; a = e/10/10/10; cout << "Ваши цифры в обратном порядке – " << d << c << b << a << endl; return 0; }
No! This is not a site engine, and this is some kind of slaughter! :-(
By the way, in this problem (№1) шt is possible … and it would be nice “deceive” user. Deceive those, that cin initially inputs character data and only then converts it to the required type. You can simply not do any transformations at all: #include using namespace std;
int main() { string e; while( true ) { cout << "Введите любое положительное целое : "; getline( cin, e ); cout << "Введенные цифры в обратном порядке : "; for( string::const_reverse_iterator i = e.rbegin(); i != e.rend(); i++ ) cout << *i; cout << endl; } }
Explain the following point: If you calculate the bet like this – amountMonth = deposit * (5 / 100) / 365 * 31; then the values are equal 0. If you assign a value to a variable 5, and put in brackets, everything works… float procent = 5; amountMonth = deposit * (percent / 100) / 365 * 31; Why doesn't the first option calculate correctly??
float stavka = 1.05;
int term = 0;
float deals = 0;
float dohodVmes = 0;
int deposit = 0;
cout << "введите сумму вклада" <> deposit;
cout << "введите срок депозита" <> srok;
incomeIntermediate = (stavka*vklad-vklad) / 12;
agreements = (vklad*srok*stavka)/12;
cout << "ваша прибыль в месяц = " << incomeIntermediate << endl;
cout << "сума к выплате в конце срока= " << agreements << endl;
Hello…sorry of course, I’m like this right away and with comments )))
My version is more correct. The whole point is, that your monthly profit is not calculated correctly. You are attached to the number of days, but it is different in different months! Therefore, the calculation will not be entirely correct in the end.. And banks count monthly. Your option will be considered correctly if you divide your monthly income into 12 months and take into account the number of days in each.
If 1000 dollars under 5% put it for a year, then there will be profit 50 u.e )) ..whatever one may say ))
My version of the program. Rate.
#includeusing namespace std;
int main(void)
{
setlocale(LC_ALL, "Russian");
double deposit; //ваш депозит
double rate; //ваш процент
int month; //количество месяцев
int Num; //количество дней в месяце
cout << deposit;
cout << month;
cout << Num;
cout <<rate;
double S = (deposit*(rate/100)) / month;
cout << "Ваш месячный процент=" << S;
cout << "\n";
cout << "Ваш процент за год=" << S*month<<endl;
}
Didn't read the terms and conditions and did it according to my own formula….but everything works fine!!
#include#include
using namespace std;
int main()
{
setlocale(LC_ALL, "rus");
float deposit = 0;
float stavka = 0;
float kol_mes = 0;
float preb = 0;
cout << deposit;
cout << stavka;
cout << kol_mes;
preb = kol_mes * (deposit / 100) * (stavka / 12);
cout << "Ваша прибыль составит " << preb << endl;
_getch();
return 0;
}
1. Could be so :
#include
#include
using namespace std;
int main() {
setlocale(LC_ALL , “Russian”);
short int a, b, c, d, e;
a=b=c=d=0;
cout << "Введите 4 значительное число \t:" <> e ;
d = e;
c = e/10;
b =e/10/10;
a = e/10/10/10;
cout << "Ваши цифры в обратном порядке – " << d << c << b << a << endl;
return 0;
}
And I would do that:
int i=1234;
for(;i;i/=10) cout<<i; cin.get();
> 1. Could be so :
Could be so…
Only then already “based on your decision” it's better to do it like this:
#include
using namespace std;
int main() {
unsigned long long e;
while( true ) {
cout <> e;
cout << "Введенные цифры в обратном порядке : ";
for( ; e != 0; e /= 10 )
cout << e % 10;
cout << endl;
}
return 0;
}
And the number here is not 4 digits, and any bit depth, and the code is shorter.
No! This is not a site engine, and this is some kind of slaughter! :-(
By the way, in this problem (№1) шt is possible … and it would be nice “deceive” user. Deceive those, that cin initially inputs character data and only then converts it to the required type. You can simply not do any transformations at all:
#include
using namespace std;
int main() {
string e;
while( true ) {
cout << "Введите любое положительное целое : ";
getline( cin, e );
cout << "Введенные цифры в обратном порядке : ";
for( string::const_reverse_iterator i = e.rbegin(); i != e.rend(); i++ )
cout << *i;
cout << endl;
}
}
2-The deception here is that, that I managed to deceive this stupid site engine, who eats:
cin >> e;
If it follows cout …
#includeusing std::cout;
using std::cin;
using std::endl;
int main()
{
setlocale(0, "Russian");
float Deposit = 0;
int month = 0;
float procent = 5;
float result = 0;
cout << "Введите сумму депозита:" <> Deposit;
cout << "Введите кол-во месяцев:" <> month;
result = Deposit * (procent / 100) / 365 * 31;
cout << "Сумма по окончанию срока депозита:" << result + Deposit << endl;
return 0;
}
Here is my code, works, but I was too lazy to enter variables for the number of days
Explain the following point:
If you calculate the bet like this – amountMonth = deposit * (5 / 100) / 365 * 31;
then the values are equal 0.
If you assign a value to a variable 5, and put in brackets, everything works…
float procent = 5;
amountMonth = deposit * (percent / 100) / 365 * 31;
Why doesn't the first option calculate correctly??
#includeusing std::cout;
using std::cin;
int main()
{
float sumDeposit = 0, hranenie = 0, otvet = 0, procentStav = 0;
int dneyvMes9ce = 0, dneyvGody = 0;
cout<<dneyvGody;
cout<<dneyvMes9ce;
cout<<sumDeposit;
cout<<hranenie;
cout<<procentStav;
//formula rac4eta procentov v mesac:
otvet = sumDeposit * (procentStav / 100) / 365 * 30;
cout<<"============================================\n";
cout<<"Procent v mes9c po formule: "<<sumDeposit<<" * "<<procentStav<<"% / "<<dneyvGody<<" * "<<dneyvMes9ce<<" = "<<otvet<<"$";
cout<<"\nProcent za vec' srok: "<<otvet*hranenie+sumDeposit<<"$";
return 0;
}