C :: Determine Number Of Binary Palindromes In Given Range?
Oct 26, 2013
Write a program to determine the number of binary palindromes in a given range [a;b]. A binary palindrome is a number whose binary representation is reading the same in either forward or reverse direction (leading zeros not accounted for). Example: the decimal number 5 (binary 101) is palindromic.
View 2 Replies
ADVERTISEMENT
Mar 1, 2013
I've pretty much finished the entire program, except for the actual calculation part.
"Given a range of values determine how many integers within that range, including the end points, are multiples of a third value entered by the user. The user should be permitted to enter as many of these third values as desired and your output will be the sum of total multiples found."
I've defined functions to take user input for the low range, high range and a do-while loop to take as many third inputs as the user wants (terminated by entering -1, as requested by the question)
To actually calculate if they're divisible, I found out that if A%B = 0, then they are divisible, so I thought I would create a loop where each value in the range between A and B is checked against the third value to see if they output a zero.
What I need to end up with is a program that tells the user how many integers are divisible by the numbers in the range, i.e: "Enter the low range value: 335 Enter the high range value: 475 Enter a value to check within the range: 17 Enter a value to check within the range: -1 There are 8 total values that are divisible by the numbers in the range." Going back to my original question, how would I create a loop or something to "check" how many values are equal to zero, and consequently increment a variable for each instance? (This is how I think it should be done)
Code:
#include <stdio.h>
//GLOBAL DECLARATIONS
int getlowR();
int gethighR(int);
[Code].....
View 4 Replies
View Related
Oct 17, 2014
I have to make a program to read a file with strings and determine if they are palindromes. The problem is that the program says that they're all palindromes.
int main() {
ifstream inFile;
ofstream outFile;
inFile.open("in.data");
outFile.open("out.data");
if (!inFile || !outFile) //to display error if input files are invalid
[code]....
View 3 Replies
View Related
Mar 7, 2013
You are given an integer, perhaps a very long long integer, composed of only the digits 1 and/or 2. You have the ability to change a 1 digit into a 2 and a 2 digit into a 1 and must determine the min. number of changes that you can make resulting in no 2 digits remaining in the number that are in a position(in terms of powers of ten) higher than any 1 digit.
example:
2222212 number of changes:1
1111121 1
2211221 3
1122112 2
no negative numbers.
how to get started. Also I'm not allowed to use anything related to arrays or sorting.
View 6 Replies
View Related
Jan 21, 2014
In a fashion similar to that in Fig. 3.11(shown below), write a short program to determine the smallest number, xmin, used on the computer you will be employing along with this book. Note that your computer will be unable to reliably distinguish between zero and a quantity that is smaller than this number.
fig311.png
View 2 Replies
View Related
Oct 21, 2013
Write a program that determines the day number (1 to 366) in a year for a date that is provided as input data. As an example, January 1st, 1994, is day 1. December 31, 1993, is day 365. December 31, 1996 is day 366, since 1996 is a leap year. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is divisible by 40. Your program should accept the month, day, and year as integers. Include a function leap that returns 1 if called with a leap year, 0 otherwise. Extend the requested solution so that your program continues to prompt the user for new dates until a negative year is entered. This is what I have so far.
Code:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int d,m,y;
int days=0;
int k;
[Code] .....
I am unsure how to make this a loop so that it keeps asking for dates?
View 9 Replies
View Related
Jan 16, 2015
I seem to be having a logical error but can not find the sources.
#include <iostream>
using namespace std;
int main() {
int student = 0;
int adult = 0;
[Code] ....
View 1 Replies
View Related
Aug 19, 2014
How do you convert a number float in a range of -10.0f to 17.0f to a eqivalent number in the range of 0.0f to 1.0f?The code does not work well. floaty is the float to change.
//change range to 0..1
diamond[x][y] = (floaty - minY) / (maxY - minY);
View 19 Replies
View Related
Feb 19, 2015
I'm trying to determine the number of times I have to change each specific character in a string to make it a palindrome. You can only change a character one at a time from the end.
Example: "abc" -> "abb" -> "aba" should print 2. "aba" will print 0 because it's already a palindrome. "abcd" -> "abcc" -> "abcb" -> "abca" -> "abba" will print 4 because it took 4 changes to make a palindrome.
I'm not too sure how to approach this - I figured out the case where if it's a palindrome (if reversed string is the same) then it'll print out a 0.
int main() {
int number;
cin >> number; //expecting a number for first line user input
for (int i = 0; i < number; i++) {
string str;
[Code] ....
View 1 Replies
View Related
May 3, 2014
Write an instruction to generate a random number in the range -501 to + 50 inclusive.
View 4 Replies
View Related
Jun 23, 2014
it will not run and im not sure why. I have a couple of errors, but I'm not sure why.
Here is my code.
//Reads input from user to determine different discounts by number of units sold
#include <iostream>
#include <string>
using namespace std;
int main() {
//Declaration and Initialization of variables
int quantity;
double discount,price = 99.00,totalCost;
[Code] ....
View 1 Replies
View Related
Jul 6, 2013
Code: Complete the program below which converts a binary number into a decimal number. Sample outputs are shown belowComplete the program below which converts a binary number into a decimal number. Sample outputs are shown below.
Sample Output 1:
8-bit Binary Number => 11111111
Decimal Number = 255
Sample Output 2:
8-bit Binary Number => 10101010
Decimal Number = 170
Sample Output 3:
8-bit Binary Number => 101010102
Number entered is not a binary number
#include <iostream>
using namespace std;
int main()
{
int num;
[code]....
View 2 Replies
View Related
Jul 5, 2013
Here's the part of the codes where I tried to use boolean expression:
Code:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "8-bit Binary Number=";
cin >> num;
[Code] .....
How can I get started with the body?
View 7 Replies
View Related
Jul 5, 2013
Here's the part of the codes where I tried to use boolean expression:
#include <iostream>
using namespace std;
int main()
[Code].....
May I know that how can I get started with the body?
View 5 Replies
View Related
Apr 11, 2013
i am writing a program that accepts a decimal number from the user and convert it to binary numbers. After the conversion, i should count the number of 1's and 0's in the said binary number. I got upto converting and counting 1's using Brian Kernighan’s Algorithm. But, i can't seem to get it to count the number of 0's.
#include <iostream>
#include<bitset>
using namespace std;
int main() {
int num,count=0,Zero,count1 =0;
cout<<"Enter the number:";
cin>>num;
string binary;
[code].....
View 7 Replies
View Related
Mar 30, 2014
I am very new to programming and have been working on a program that can receive decimals or binary numbers and convert them. The decimal --> binary works fine. For some reason I cannot figure out I cannot get the "BinaryToDecimal" function to perform. By putting a "printf" into the for-loop.
Code:
#include <stdio.h>#include <string.h>
#include <math.h>
char* ReverseString (char _result[]) {
int start, end, length = strlen(_result);
char swap;
for (start = 0, end = length-1; start < end; start++, end--)
[code]....
View 2 Replies
View Related
Nov 11, 2013
For example if we have 101010 First half is 101 which you get by multiplying 101010 * 10 ^-3
now how do you get the second half (010) ??
View 2 Replies
View Related
Sep 10, 2014
I have a code and am asked to modify it so that it will take as input as unsigned binary number up to 16 digits in length and convert it into its equivalent decimal number and output that decimal number.
All I know is that I use library function strlen() in <cstring> to calculate the length of the input string.
I also know I have to use something called pow(2,4);
//pow (); is found in cmath
I was told to use sum = sum >>16-l; (l is the length of />/>
#include <iostream>
using namespace std;
int main() {
[Code]....
View 4 Replies
View Related
May 31, 2013
how to take binary number as an input, generate partial products by bit-wise multiplication and in last step to add all the partial products to generate final products".
View 5 Replies
View Related
Dec 7, 2014
I had an exercise that required me to convert a number to binary (base 2) which as simple enough.
Code:
#include <iostream>#include <iomanip>
#include <cmath>
using namespace std;
void Conversion (int n);
int main () {
[Code] .....
I now have a follow on exercise that requires me to convert to binary from ant base up to 10, i thought this would just be replacing the 2 with a variable obtained form the user, but i am having problems as within the function i am getting an error that i haven't passed enough arguments and i cant see why i get this. I did the following:
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
float Conversion (int n, int b);
[Code] ....
View 2 Replies
View Related
Jun 8, 2013
I have to do a code for a encoder, decoder and converter for binary number and hexadecimal...
View 1 Replies
View Related
Feb 24, 2014
The following piece of code is supposed to output the binary representation of a given integer and it does exactly that. However, if the given integer is 2, then output is 01. Is there a way to make the program output 0001. I am working on a C program that outputs 4-bit gray code.
#include <stdio.h>
#include <math.h>
int main(void) {
long int n=2;
while (n) {
if (n & 1)
printf("1");
[Code] ......
View 2 Replies
View Related
Sep 21, 2014
I recently wrote a program to convert numbers to binary in c++, Well here it is:
#include <iostream>
void recur(int convert) {
if(convert == 0) //if input is 0 , return nothing. {
return;
}
recur(convert/2); // divide convert by 2, get only a 1 or 0
[Code] ....
View 13 Replies
View Related
Sep 22, 2013
Write a program to print out the binary value of a 16 bit number.
Create integers i, count, and mask.
Set 'i' to a hex value of 0x1b53.
Set mask to a value of 0x8000. Why?
print a line to show the hex value of i and then the leader for the binary value like this: Hex value = 1b53 Binary=
Use a for loop to loop 16 times and print 16 digits, using count as the loop counter
To test for each digit value, bitwise and 'i' with 'mask'
when the result for the bitwise and is true, print the number '1'
when the result for the bitwise and is false, print the number '0'
then shift mask one place to the right
print a new line and then quit
Use prtscrn and make a hard copy of the code with the console output.
Extra: use the modulus of count and print a space after every 4th digit to make the binary easier to read
The output should look like this: Hex value = 1b53, Binary= 0001 1011 0101 0011
so far this is what i have
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, count, mask;
// 1B53 0001 1011 0101 0011
// 8000 1000 0000 0000 0000
i = 0x1b53;
[Code] ....
it is telling me that there is an "else" without previous "if", also is the program that I wrote correct?
View 3 Replies
View Related
Sep 3, 2013
Basically i need to make a number guessing game where user thinks of a numbver from 1 - 100 and the machine will try to guess it in the least number of times. Once it guesses the number it will also say how many tries it took to guess.
My code so far is
#include<iostream>
using namespace std;
const int MAX = 100;
int main() {
char ch;
cout << "Think of an integer number between 0 and " << MAX<<endl;
cout << "Write it down on a piece of paper then hit a key to continue"<<endl<<endl;
cin.get(ch);
[Code] ....
View 2 Replies
View Related
Nov 29, 2013
If I have a number 117, represented in binary as : 01110101 and I wanted to grab the top nibble. What would be the decimal value I would be extracting?
Would it be 0111 or 0101 decimal values 112 or 5 or is my understanding completely wrong?
View 11 Replies
View Related