Here I have a C++ code with for loops.
for(int A=1;A<=3;A++)
{
cout<<A*2;
for(int B=1;B<=A;B++)
cout<<"*";
cout<<endl;
}
and it gives me this output.
2*
4**
6***
I need to do the same thing using while loops. so I convert the above code to this one.
while(A<=3)
{
cout<<A*2;
while(A>=B)
{cout<<"*";
B++;}
cout<<endl;
A++;
}
but this code give me the output
2*
4*
6*
Can someone tell me what I'm doing wrong there in my while loop.
int A = 1;
int B;
while (A <= 3)
{
cout << A * 2;
B = 1;
while (B <= A)
{
cout.put('*');
++B;
}
cout.put('\n');
++A;
}
ioccc-style:
#include <iostream>
int main()
{
int A{1};while(!(A>>2)&&std::cout.put((A<<1)|0x30)){
int B{A++};while(std::cout.put((!B)["*\n"]),B--);}
}
You are not giving A or B initial values.
void func()
{
int A = 1;
while (A<=3)
{
std::cout << A*2;
int B = 1;
while(A>=B)
{
std::cout << "*";
B++;
}
std::cout<<endl;
A++;
}
}
Your second snippet doesn't show where you declared A or B. I'm assuming it's outside of the outer while loop which won't work. You have to declare B inside the first while or reinitialize it every loop.
int A = 1;
while(A <= 3)
{
cout << A * 2;
int B = 1;
while(A >= B)
{
cout << "*";
++B;
}
++A;
cout << endl;
}
Result.
You should use B as local variable, alson change the while comparation to less and equal.
int A = 1;
while(A<=3) {
cout<<A*2;
int B = 1; // B as local variable
while(B<=A) {
cout<<"*";
B++;
}
cout<<endl;
A++;
}
Output:
2*
4**
6***
int A = 1;
while(A <= 3)
{
cout<<A*2;
int B = 1;
while(A >= B){
cout<<"*";
B++;
}
cout<<"/n";
A++;
}
output :
2*
4**
6***
Related
a beginner at coding here.
I was practising loops(c++) when I stumbled upon this problem:-
Write a program in C++ to find the perfect numbers between 1 and 500. (6,28 and 496)
Perfect number: It is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3.
I wrote the following code:-
#include <iostream>
using namespace std;
int main() {
int n=2; //test numbers from 2 to 500.
int div=1; //divisor for n.
int sum=0; //sum of divisors which divide n.
while (n<=500) {
while (div<n){ //if div divides n, then it will added to sum and incremented, else only incremented.
if (n%div==0){
sum=sum+div;
div++;
} else{
div++;
}
}
if (sum==n){
cout<<n<<" is a perfect number."<<endl;
n++;
} else{
n++;
}
}
return 0;
}
The code is supposed to print that 6, 28 and 496 are perfect numbers.
But instead, it's not printing anything. Haven't been able to find the error yet after checking for 30+ minutes.
Could anyone point out the error?
You forget to re-initialize some variables in your loop.
for seems more appropriate than while here.
Create sub function also help to "identify" scope.
#include <iostream>
bool isPerfectNumber(int n)
{
int sum = 0;
for (int div = 1; div != n; ++div) {
if (n % div == 0) {
sum += div;
}
}
return sum == n && n > 0;
}
int main()
{
for (int i = 2; i != 501; ++i) {
if (isPerfectNumber(i)) {
std::cout << n << " is a perfect number." << std::endl;
}
}
return 0;
}
#include<iostream>
using namespace std;
bool perfect_num(int x);
int main() {
int m, n, x;
cout << "input the range m, n: " << "\n";
cin >> m >> n;
for (x = m; x <= n; ++x) {
if (perfect_num(x)) {
cout << x << " ";
}
}
return 0;
}
bool perfect_num(int x) {
bool flag = false;
//initialize
int sum = 0, i;
//loop 1 to x
for (i = 1; i < x; ++i) {
//judge whether is the factor
if (x % i == 0) {
sum += i;
}
}
//update flag
flag = (sum == x);
return flag;
}
#include<iostream>
using namespace std;
//judge function
bool isPerfectNum(int num){
int tmp = 0;
for (int i = 1; i < num; ++i) {
if (num % i == 0) {
tmp += i;
}
}
return tmp == num;
}
int main(){
cout << "Perfect Number contains: ";
for (int i = 1; i <= 500; ++i){
if (isPerfectNum(i)) {
cout << i << " ";
}
}
cout << "\n";
return 0;
}
at the end of your first loop, you should bring back div and sum to their default value.
int main() {
int n=2; //test numbers from 2 to 500.
int div=1; //divisor for n.
int sum=0; //sum of divisors which divide n.
while (n<=500) {
while (div<n){ //if div divides n, then it will added to sum and incremented, else only incremented.
if (n%div==0){
sum=sum+div;
div++;
} else{
div++;
}
}
if (sum==n){
cout<<n<<" is a perfect number."<<endl;
n++;
} else{
n++;
}
div = 1; // you should bring them back here.
sum = 0;
}
return 0;
}
I want the pyramid to look like this if the input was 6
0
12
345
6789
01234
567890
Here's my code
void HalfPyramid(int num)
{
for (int a=0; a<num; a++)
{
for (int b=0; b<num-a; b++)
{
cout << " ";
}
for (int c=0; c<a; c++)
{
cout << a;
}
cout << endl;
}
}
I'm getting
1
22
333
4444
55555
Not sure how to show the numbers as increasing throughout, I tried outputting a and a+1.
You need another variable. That variable needs to start at 0 and increment every time you print it. Then since you need to to wrap back to 0 once you print 9 we will use the modulus operator to constrain the output to the range of [0, 9]. With all that you get
void HalfPyramid(int num)
{
int output = 0;
for (int a=1; a<num+1; a++)
{
for (int b=0; b<num-a; b++)
{
cout << " ";
}
for (int c=0; c<a; c++)
{
cout << output++ % 10;
}
cout << endl;
}
}
void HalfPyramid(int num)
{
int cur = 0;
for (int a=0; a<num; a++)
{
for (int b = 1; b < num - a ; b++)
{
cout << " ";
}
for (int c=0; c < a + 1; c++)
{
cout << cur;
cur = (cur+1)%10;
}
cout << endl;
}
}
The other answers already provide working versions of HalfPyramid. This answer, hopefully, makes you think of the logic and functionality a bit differently. I like to create small functions that capture the essence what I am trying to do rather than using the language to just do it.
bool isSpace(int num, int a, int b)
{
return ((a + b) < (num - 1));
}
int getNextDigit(int in)
{
return (in+1)%10;
}
void HalfPyramid(int num)
{
int digit = 0;
for (int a = 0; a < num; ++a)
{
for (int b = 0; b < num; ++b)
{
if ( isSpace(num, a, b) )
{
cout << " ";
}
else
{
cout << digit;
digit = getNextDigit(digit);
}
}
cout << endl;
}
}
I want to have a program that returns true when there exist such positive integer numbers a and b such that a*a+b*b=n*n.
My code is:
bool c(int n){
int b=1;
int a=1;
for (a=1; a<=n; a++) {
for (b=a; b<=n; b++) {
if (a*a + b*b == n*n) {
return true;
else
return false;
}
}
}
However this code does not return what i want it to. What can i do to fix that?
Please everybody before you down vote please explain what could be improved in this question. I have noticed that sometimes people have a tendency to down vote questions without any explanation.
Your program doesn't compile. You probably intended to write this:
bool c(int n) {
int b = 1;
int a = 1;
for (a = 1; a <= n; a++) {
for (b = a; b <= n; b++) {
if (a*a + b*b == n*n)
return true;
else
return false;
}
}
}
which compiles, but which is wrong.
But you probably want this:
bool myfunction(int n) {
int b = 1;
int a = 1;
for (a = 1; a <= n; a++) {
for (b = a; b <= n; b++) {
if (a*a + b*b == n*n)
return true;
}
}
return false;
}
int main() {
if (myfunction(5)) {
printf("myfunction(5) returned true\n");
}
}
bool c(int n){
int b=1;
int a=1;
for (a=1; a<=n; a++) {
for (b=a; b<=n; b++) {
if (a*a + b*b == n*n) {
return true;
else
return false;
}
}
}
The code you have written will check for a=1 and b=1 and that evaluates to 1 + 1 equals to 2.
So,all that your code does is compare 2 to n.
The return false statement should be outside both the for loops.
This means that for all combination of a and b there exists no pair (a,b) such that aa + bb = n*n.
I'm trying to print all common multiples of two integers smaller than a certain limit(100 in my case). However, when I call my function, it does nothing. This is my code:
void com_mul(int a, int b)
{
int original = b;
for(int i = 1; a <= 100; i++)
{
a *= i;
b = original;
for(int j = 1; b <= a; j++)
{
b *= j;
if(a == b)
cout << b << ", ";
}
}
}
You can solve this problem much simpler, using a single loop.
In a for loop iterate over potential divisors d from 1 to 100. If d divides both a and b, print d.
You can tell if a number divides another number by applying the % operator, and checking the result for zero:
if (a%d == 0 && b%d == 0) {
cout << d << endl;
}
Tested with a = 4, b = 2, max = 100 on my machine. And it outputs 4.
This is because of the line for (int j = 1; b <= a; j++). j can only go upto 'a'
I think this would do.
#include <iostream>
#include <string>
int main()
{
int a, b, max;
std::cin >> a >> b >> max;
for (int i = a; i <= max; i++)
{
if (i%a == 0 && i%b == 0)
std::cout << i << std::endl;
}
return 0;
}
How could I find the biggest common divisor of 2 numbers using array? I tried to solve it using 2 arrays and I couldn't finish it. How could I improve this program?
#include <iostream>
using namespace std;
int main()
{
unsigned int A[2][10], B[2][10], a, b, c_exp, d, i1, P, x;
bool apartine = false;
cout << "a="; cin >> a;
cout << "b="; cin >> b;
P = 1;
c_exp = 0;
i1 = 0;
while (a % 2 == 0)
{
c_exp++;
a = a/2;
}
if (c_exp != 0)
{
A[i1][0] = 2;
A[i1][1] = c_exp;
i1++;
}
d = 3;
while (a != 1 && d <= a)
{
c_exp=0;
while (a % d == 0)
{
c_exp++;
a = a/d;
}
if (c_exp!=0)
{
A[i1][0] = d;
A[i1][1] = c_exp;
i1++;
}
d = d+2;
}
cout << "\nMatricea A contine:";
for (int i = 0; i < i1; i++)
{
cout << "\n";
for (int j = 0; j < 2; j++)
cout << A[i][j] << ",";
}
c_exp = 0;
i1 = 0;
while (b % 2 == 0)
{
c_exp++;
b = b/2;
}
if (c_exp != 0)
{
B[i1][0] = 2;
B[i1][1] = c_exp;
i1++;
}
d = 3;
while (b != 1 && d <= b)
{
c_exp = 0;
while (b % d == 0)
{
c_exp++;
b = b/d;
}
if (c_exp != 0)
{
B[i1][0] = d;
B[i1][1] = c_exp;
i1++;
}
d = d+2;
}
cout << "\nMatricea B contine:";
for (int i = 0; i < i1; i++)
{
cout << "\n";
for (int j = 0; j < 2; j++)
cout << B[i][j] << ",";
}
return 0;
}
From now on I have to find if the first number of first array exist in the second array and after this I have to compare the exponents of the same number of both array and the lowest one I have to add it to product. After this I have to repeat the same proccess with the second number to the last one of the first array. The problem is that I don't know how to write this.I have to mention that this program isn't complete.
Any ideas?
If you need better solution then you can avoid array and use the below logic.
int main()
{
int a =12 ,b = 20;
int min = a>b ? a:b; // finding minimum
if(min > 1)
{
for (int i=min/2; i>1; i--)//Reverse loop from min/2 to 1
{
if(a%i==0 && b%i==0)
{
cout<<i;
break;
}
}
}
else if(min == 1)
{
cout<<"GCD is 1";
}
else
cout<<"NO GCD";
return 0;
}
You can also check the working example Greatest Common Divisor
I am not quite sure what you are trying to achieve with your code. It looks over complicated. If I were to find the biggest common divisor of two numbers I would do something like the following:
## This is not a correct implementation in C++ (but close to it) ##
Read the two integers **a** and **b**
int max_div(int a, int b){
int div = a > b ? a : b;
while (div != 1 && (a%div != 0 && b%div != 0)){
div--;
}
return div;
}
This function starts with the minimum of a and b as the highest possible common divisor and then works its way backwards until one of two possible outcomes:
It finds a common divisor (a%div == 0 and b%div == 0)
It reaches one (always a common divisor)
EDIT : Now returns one if no higher divisor is found. (Was returning zero which made no sense)