C++ :: Highest Digit In A Number (recursive Function)
Dec 23, 2013
I have this example problem in my school coursebook. How this program works? It determines the highest digit in a number.
#include <iostream>
using namespace std;
int m(int n) {
int a,b;
[Code] ....
View 4 Replies
ADVERTISEMENT
Oct 26, 2014
Write a function which will take 3 arguments. The function needs to return a new number which is formed by replacing the digit on a given position in the number with a digit which is carried as an argument (the position in the number is counted from right to left, starting with one). Write a main program which will print the newly formed number.
Examples:
A function call of 2376, 3 and 5 should return the number 2576
A function call of 123456, 4 and 9 should return the number 129456
What I succeeded to do so far:
Figure out the logic for swapping the digit and write working code for it (in the main function).
What I failed to do so far:
Write a function which will return the desired result.
What is my problem:
I tried writing a function to do this, but as you see from my calculations, my result is divided in 3 parts. I don't know how to return more variables from a function.
Code:
#include <stdio.h>
int main() {
int inputNumber, swapPosition, swapDigit;
scanf("%d%d%d", &inputNumber, &swapPosition, &swapDigit);
int i, numberPart1 = inputNumber;
for (i = 1; i <= swapPosition; i++)
[Code] ...
View 8 Replies
View Related
Oct 27, 2014
I can't find the highest digit within c-string. Ithe user was to enter 1234. the highest shall be 4 but can't get that number.
int Hnum(char* str) {
//int size = 0;
int H = 0, remainder, div =0;
int intstr = atoi(str);
[Code] ....
View 2 Replies
View Related
Feb 18, 2015
Code:
int exploder(int number,int array[]) {
int functi = 0;
int digit = number % 10;
while (number > 0) {
[Code] ....
View 2 Replies
View Related
May 18, 2013
I am supposed to write a recursive function which find the partition of a number n ,, for example if n=3 , it should print 1 1 1 , 1 2 , 3
I wrote the program but i am getting the two partition 1 2 and 2 1 which are the same ,, how can i avoid that ?
this is the code :
void PrintPartition( int n , int A[] , int j ) {
if( n<=0 ) {
printArray( A, j );
return ;
} for( int i=1 ; i<=n ; i++ ) {
A[j]=i;
PrintPartition( n-i , A ,j+1 );
} }
the first call of the function is : PrintPartition( n , A , 0 ) ;
View 3 Replies
View Related
Apr 9, 2013
I am trying to create a recursive function that i can call on in order to take a user inputed base and exponent and give final answer this is what i have but im completely lost after this i dont even know how to continue. What i have so far
#include <iostream>
using namespace std;
int Exp(int x,int y){
if(base <= 1 || exp == 0) return 1;
if(exp == 1) return base;
int main(){
int number, exp;
[Code] .....
After i set the base situations im not sure how to get the function to make the function take the base to the exponent recursively.
View 3 Replies
View Related
Nov 14, 2013
Why my program is returning a negative number at the end...attached is the program:
/*Write a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of 1 element.*/
#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
float recursiveMinimum (int ARRAY[], int n);
[Code] .....
View 2 Replies
View Related
Mar 14, 2015
I'm having an issue with the following assignment:
Program is supposed to take in an unsigned number and afterwards return the 'n-th' member of the following recursive function:
f(1) = 1, f(2) = 2; f(3) = 3; f(n+3) = f(n+2) + f(n+1) + f(n), n>0;
When I worked with Fibonacci it was pretty easy since I just had to decrement the next member for each step. I used the following:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int fib(int n) {
static int memorize[MAX] = {1,1};
if(memorize[n])
return memorize[n];
[Code] ....
My main problem is that I have no visual of the current function, as well as the fact that it takes the f(n+3) = f(n+2) + f(n+1) + f(n), whilist I've only got f(n) to begin with.
View 5 Replies
View Related
Jul 31, 2014
My while loop is the problem. I want to use a non-recursive function to make the triangle number the sum of all whole numbers from 1 to N.
#include "stdafx.h"
#include <iostream>
using namespace std;
int triangle(int t);
[Code] ....
View 2 Replies
View Related
Apr 16, 2013
Task: Write a recursive function `seven_up` which takes a number and returns that number with all the sevens turned into eights.
- Example
cout<<seven_up(777)<<endl; //prints 888
cout<<seven_up(1234567890)<<endl; //prints 1234568890
cout<<seven_up(50)<<endl; //prints 50
- Hint: It's like removeFirst, except:
0. Base Case: If the number is one digit long, we don't want to erase it (by returning 0). Instead, riddle me this: When I have a one-digit number, what happens if I change all the 7's into 8's? Well, if the number is 7, it becomes 8, but otherwise it's unchanged.
1. Recursive Call: If the number is longer, then we strip off the last digit, figure out what the answer for the rest is (the recursive call on n/10), and then put the last digit back on the number when you're done.
Hint: You probably need to store the least digit (the n%10) and check if it's 7 separately.
#include <iostream>
using namespace std;
int removeFirst(int n) {
if(n<10)
return 0;
return 10*removeFirst(n/10)+n%10;
[Code] ......
I tried other algorithms but i don't get it.
View 14 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
Sep 10, 2014
I'm having trouble converting a 4 digit number into a BCD number, in the program I did below I was able to convert a 2 digit number into BCD, but I do not know how to convert a 4 digit number or how to start it.
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
[code]....
View 2 Replies
View Related
Dec 9, 2014
I wanted to find all the prime until a specified limit in C. I use the Sieve of Eratosthenes. But when I define the limit to anything more than a 7 digit number the program terminates.
Code:
#include<stdio.h>
#define limit 1000000000
int main(void)
{
unsigned long long int i,j;
int primes[limit] = {0};
//int count =0;
for(i=2;i<limit;i++)
}
[code]....
I believe that this might be because the size cannot be declared array cannot be more than the a 7 digit number. I think so. how to store a 10 digit number in C?And can't unsigned long long hold a 10 digit?
View 1 Replies
View Related
Apr 14, 2013
I have an array join[], some of its elements are single digit and some are numbers. I want to print the output in single digit form.Like in below code
Code:
int join[3]= {12,0,3};
int split;
int j;
for (j=0;j<3; j++) {
[Code] ....
My code won't consider array element "0". How can i fix it for that too.
Code: I want output should be like
1
2
0
3
View 4 Replies
View Related
Dec 18, 2013
I am trying to remove the first digit so if the user enters 12345 it should output 2345 the code i have works only for removing the last digit how would i go about removing the first one?
#include <iostream>
using namespace std;
int removeFirst(int n);
int main(){
int n, m;
cout << "enter number" << endl;
[Code] ....
View 4 Replies
View Related
Sep 14, 2013
what is the data type of a variable to store 50 digit number in c? and what is the data type specifier for that?
View 5 Replies
View Related
Oct 26, 2013
i just started learning programming and i just wanna know how come when i try to print the last digit of a number the output always become 6?
View 4 Replies
View Related
May 8, 2014
So I have to ask the user to enter a positive 12 digit number, and it has to be 12 digits exactly. I thought I'd do
Code:
unsigned long long int x;
do{...}
while(!(x>99999999999 && x<1000000000000));
This would obviously be fine on my 64 bit machine, but the code will be ran on a 32 bit one, where unsigned long long is, if I'm not mistaken, 32 bits. Which has a max value of 4 billion and something.
View 13 Replies
View Related
Jun 23, 2014
How to get all the possible combinations for 4 digits from a 5 digit number. I need a pair that has both 5 digits and four digits. their sum must be equal to a five digit user input. i.e.
user input : 14690
output:
14690 has pairs 12345 + 2345
2345 came from 12345
lets say that x = 12345 and y =2345
besides y == x%10000
other formula can i have since if i use % and / i will have a lot of declarations....
View 2 Replies
View Related
Aug 4, 2013
How to produce a code that will satisfy the following constraints. I don't really know C++
- Produces a random three number positive integer between 001 - 504.
- Changes the output number every 24 hours on a website.
View 2 Replies
View Related
Apr 4, 2014
I want to encrypt Mac address to 4 digit number which can be decrypted to original MAC address.
View 9 Replies
View Related
Aug 22, 2014
I am trying to write up something to have a user to enter a four digit number. Only four digits, Ex: 0001, 0116, or 9999. There is no getting around the selection 0001 or 0002. I understand if not done correctly, the first three 0's will be ignored.
I've been just playing around with what I have below but I just don't remember how to do it nor can I find a good example online to figure it out. I know it is not correct just typing to try to remember. I am aware that it is gibberish right now, this is just me brainstorming.
int number;
cout<<("Please enter the four digit number(Ex: 0001):
");
cin>> setw(2) >> number;
cout<<)"Please enter four digit date. Two digits for month and two digits for year:
");
cin>> date;
if (number< || number > 30)
cout << "Invalid choice. Try again." << endl;
cin.clear();
View 2 Replies
View Related
Jan 24, 2015
Given an integer, find the sum of all the digits in the number until the sum becomes a single digit. E.g. sum of digits of 9264 = 21. Then sum of 21 = 3.
View 2 Replies
View Related
Nov 8, 2013
//using bloodshed dev c++ compiler
//How can I output the name and a two digit number next to it
//example would be Indiana Jones 20
#include<iostream>
#include<string>
using namespace std;
class Customer {
[Code] .....
View 3 Replies
View Related
Aug 12, 2014
after staring at this for awhile, I can't figure out why it won't work. It prints out numbers from 0 to 100, but will print out an absurdly high number as the highest number, like 790 or 640. I can't see why.
Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int find_highest(int array[]);
int find_highest (int array[], int size) {
int highest_num;
for (int i = 1; i < size; ++i) {
if (array[i] > array[i-1]) {
[code]....
View 1 Replies
View Related
Feb 17, 2013
Basically, I am suppose to compute 10 numbers and find their highest prime number for each of them. I've already have the inputs working, and set up 2 arrays to store inputs and their highest prime number. But I am uncertain on how to find and store their highest prime number. How to start it off?
View 7 Replies
View Related