| | |
muliplying without using a * operator!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hello Everyone,
This is my first day here. I've not browsed the site yet. But I wanted to know the answer of these questions. I'd be glad if anyone of you can help me.
1.Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;
2.Exchange two numbers without using a temporary variable.
3.Find if the given number is a power of 2.
4.Multiply x by 7 without using multiplication (*) operator.
This is my first day here. I've not browsed the site yet. But I wanted to know the answer of these questions. I'd be glad if anyone of you can help me.
1.Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;
2.Exchange two numbers without using a temporary variable.
3.Find if the given number is a power of 2.
4.Multiply x by 7 without using multiplication (*) operator.
Last edited by sara.rythm; Aug 24th, 2005 at 10:32 pm. Reason: spelling mistake
1. could be a trick question ...
2. assuming we are talking integers, use a macro like ...
Edit: Eating crow again. Sorry, change the "of any type" to "integers only"!
3. assuming we are talking integers, whittle the number down with a series of odd integers (1,3,5,7,...) until a zero match is reached ...
Edit: put that in here because the tone went splenetic later in this thread.
4. Multiple addition will do ...
... as long as you don't multiply by a float!
C++ Syntax (Toggle Plain Text)
std::cout << "numbers from 1 to 100 and 100 to 1";
2. assuming we are talking integers, use a macro like ...
C++ Syntax (Toggle Plain Text)
// macro to swap two arguments of any type #define SWAP(x, y) ((x) ^= (y) ^= (x) ^= (y))
3. assuming we are talking integers, whittle the number down with a series of odd integers (1,3,5,7,...) until a zero match is reached ...
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void isPowerOf2(int x) { int y; y = 1; // start of odd number sequence 1, 3, 5, 7 ... while(x > -1) { cout << x << endl; // test x = x - y; // subtracting a sequence of odd numbers y = y + 2; // next odd number if (x == 0) { cout << "Bingo, we have a power of 2 number" << endl; break; } else if (x < 0) cout << "Gee, this number fails the power of 2 test" << endl; } } int main() { isPowerOf2(98); isPowerOf2(81); isPowerOf2(1); isPowerOf2(-8); // test cin.get(); // wait return EXIT_SUCCESS; }
4. Multiple addition will do ...
C++ Syntax (Toggle Plain Text)
y = x+x+x+x+x+x+x;
May 'the Google' be with you!
•
•
•
•
Originally Posted by vegaseat
2. use a macro like ...
C++ Syntax (Toggle Plain Text)
// macro to swap two arguments of any type #define SWAP(x, y) ((x) ^= (y) ^= (x) ^= (y))
http://www.eskimo.com/~scs/C-faq/q10.3.html
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Ouch, I have forgotten how meticulous C is. Sorry, change the "of any type" to "integers only".
May I kindly suggest you start learning Python, then a simple ...
... will do. Where a and b is any objet.
May I kindly suggest you start learning Python, then a simple ...
C++ Syntax (Toggle Plain Text)
a, b = b, a
May 'the Google' be with you!
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
the answer to number 3 (assuming the number is an integer) is to use the modulous operator, %.
C++ Syntax (Toggle Plain Text)
if(!(numbername%2)) cout<<numbername<<" is a power of 2."<<endl;
Now you got me started, for swapping either integers or floats this will work too ...
Edit: Just an interrogative alternate, this swap is actually about five times slower then the usual temp variable swap.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int a = 1; int b = 7; cout << "a = " << a << " b = " << b << endl; // swap a with b a = a + b; b = a - b; a = a - b; cout << "a = " << a << " b = " << b << endl; cin.get(); // wait return EXIT_SUCCESS; }
May 'the Google' be with you!
•
•
•
•
Originally Posted by vegaseat
Now you got me started, for swapping either integers or floats this will work too ...
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <limits.h> using namespace std; template<class T> swapm(T &a,T &b) { a = a + b; b = a - b; a = a - b; }; int main() { int a = 1; int b = 7; cout << "a = " << a << " b = " << b << endl; swapm(a,b); cout << "a = " << a << " b = " << b << endl; float c = 1.23F; float d = 2.34F; cout << "c = " << c << " d = " << d << endl; swapm(c,d); cout << "c = " << c << " d = " << d << endl; cin.get(); // wait return 0; }
•
•
•
•
Originally Posted by Drowzee
the answer to number 3 (assuming the number is an integer) is to use the modulous operator, %.
C++ Syntax (Toggle Plain Text)
if(!(numbername%2)) cout<<numbername<<" is a power of 2."<<endl;
As for #1:
First of all, "if" is not a loop. I don't know what got this idea into your head. But let's not use 'if' anyway.
C++ Syntax (Toggle Plain Text)
#include <iostream> void cout_nums_and_back(int low, int high) { (high < low) || ( std::cout << low << std::endl, cout_nums_and_back(low + 1, high), std::cout << low << std::endl ); return; } int main() { cout_nums_and_back(1, 100); return 0; }
![]() |
Similar Threads
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
Other Threads in the C++ Forum
- Previous Thread: ifstream help?
- Next Thread: Taking address of constructors??
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





