Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to write an algorithm which can show me the number I enter in the mirror. I mean, if I enter 173, it should show me that the number in the mirror is 371. I wrote the code, but something it's not working and I can't figure what. Thanks a lot !
#include <iostream>
using namespace std;
void Citire(int &n)
{
cout << "\nda numarul:";
cin >> n;
while (n <= 0)
{
cout << "ai gresit, da natural:";
cin >> n;
}
}
int Oglinda(int &n)
{
int Og = 0;
int UltCif;
while (n > 0)
{
UltCif = n % 10;
Og = Og * 10 + UltCif;
n = n / 10;
}
return Og;
}
int main()
{
int n;
int Og;
Citire(n);
Oglinda(Og);
cout << endl << Og << " este oglinda numarului " << n << endl;
return 0;
}
I think you mean:
Og = Oglinda(n);
And that function's signature should be:
int Oglinda(int n);
Otherwise, you're modifying n in the function - which you don't want to do since you want to print its original value at the end.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
The problem is to find sum of first and second digit of a number.
If number is smaller than 10 then print number itself.
I wrote this code but i don't know why output is wrong.
#include <iostream>
using namespace std;
int main()
{
int t,a,n,l;
cin>>t;
while(t--)
{
cin>>n;
if(n<10)
{
cout<<n;
}
else
{
a=n%10;
l=n/10;
cout<<a+l<<endl;
}
}
return 0;
}
The reason your solution is wrong because n/10 won't give you the first digit :)
If this is for a question for competitive programming(since you're taking in t test cases), I'll suggest taking input as a string assuming that input is always a valid int.
std::string s;
cin >> s;
if (s.size() == 1) std::cout << s << std::endl;
else std::cout << int(s.front() - '0') + int(s.back()-'0') << std::endl;
extracting of required digits is wrong in your solution.
Please find solution for adding first and last digit of a number at
https://onlinegdb.com/Byro9hCMI or below:
its small and hence pasting it here as well:
#include <iostream>
using namespace std;
int main()
{
int first, last, number;
cin>>number;
first = number % 10;
while((number = number/10)>0)
{
last = number;
}
cout << endl << first+last << endl;
return 0;
}
Two Add first two digit (unit and tens)at https://onlinegdb.com/SJXNThAzI or below:
#include <iostream>
using namespace std;
int main()
{
int first, second, number;
cin>>number;
first = number % 10;
number = number/10;
second = number %10;
cout << endl << first+second << endl;
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I wrote code to evaluate the expression above. It executes, but I suspect that the expression is not evaluated correctly.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
setlocale(0, "");
// Задание 1
cout << "Задание 1" << endl;
cout << "" << endl;
double m, x, k, k1, k2;
int n = 18;
cout << "Определите m: ";
cin >> m;
cout << "Определите x: "; // вероятно, в задании ошибка. Если не положу ничего в x - там будет
//единица, ответы будут примерно одинаковые.
cin >> x;
do // начало цикла do while
{
k2 = pow(x, 3*x+1);
k1 = k2 * (8*x);
k = k1 - 785;
}
while (n < m); // конец цикла do while
cout << "Результат вычисления, k = " << k << endl;
system("pause");
}
If you want to use a loop instead of calling std::pow, you have to fix its condition and iteration expression. The calculations inside the body are also different from the posted formula. Note the following changes:
double k = 1.0;
for (int i = n; i <= m; ++i)
{ // ^^^^^^^^^^^^^^^
k *= pow(x, 3.0 * x + 1.0) + 8.0 * x;
// ^^ ^^^
}
k -= 785.0; // <- This should be outside
Note that the body of the for loop doesn't depend on i, so that there are plenty of ways to refactor this code in more efficient ways.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am new to c++ and cant seem to figure out how to simply get an integer from the user and make sure it is between 0-15. Here is my code so far:
When I run the code it only prints Hello world
int main()
{
int greetAndGet();
cout << "Hello";
return 0;
}
int greetAndGet()
{
int i;
cout << "\nPlease give an integer in [1,15]" << endl;
cin >> i;
cout << endl;
}
int greetAndGet(); is a forward declaration of a function, not a call.
Write greetAndGet(); instead.
Note further that a function should be defined/declared before any call to it. So either place the function definition before main, or write
int greetAndGet(); // forward declaration
int main()
{
greetAndGet();
cout << "Hello";
return 0;
}
...
As pointed out in another answer, int greetAndGet() is a forward declaration that you probably intended to be a call; though you do want to forward declare it before main. As for testing the range of the entered value, you could use a loop to check if it is in the range. I think what you want is this:
int greetAndGet();
int main()
{
int num = greetAndGet();
cout << "Hello";
return 0;
}
int greetAndGet()
{
int i;
cout << "\nPlease give an integer in [1,15]" << endl;
do {
cin >> i;
if(i < 1 || i > 15)
{
cout << "Number not in [1,15], please try again" << endl;
}
} while(i < 1 || i > 15);
cout << endl;
return i;
}
I'm not sure what you want to do with the number, but this should get you the entered number.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have faced the problem the formula of percentage doesn't work properly
#include <iostream>
using namespace std;
int main() {
int tmarks,intermarks, passmarks;
float per;
cout << "Enter Your Inter Marks:\n";
cin >> intermarks;
cout << "Enter Your Total Marks:\n";
cin >> tmarks;
cout << "Enter Your PassMarks:\n";
cin >> passmarks;
per = (intermarks/tmarks) * 100;
cout << "percentage:" << per;
if (per >= 45 && passmarks >= 50) {
cout << "Welcome To Uni\n";
} else {
cout << "Improve Your Marks You are eligible\n";
}
}
If intermarks = 50 and tmarks = 75, then intermarks/tmarks will be 0. Since both are integers. You need to typecast before division operation. This way float(intermarks) / float(tmarks) will be 0.67 and per will be 67
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi I'm doing a prime number work for class, and I'm running into a problem. Could someone please help me a little bit?
With some numbers it will work but other numbers it won't:
#include<iostream>
#include "cmath"
#include "ctime"
using namespace std;
int main(){
int num;
int i = 2;
int result;
cout << "What is the prime you want to enter";
cin >> num;
for(; i < num; i++){
result = num / i;
}
if (num % i == 0) {
cout << "Your number is not prime\nIt is divisble by: " << i << endl;
}
if(num % i != 0){
cout << "your number is prime\n";
}
return 0;
}
this if (num % i == 0) { .... must be done inside the loop, otherwise you are going to check only the last value of i