C++ :: Function Which Take An Integer And Return Number Of Digits In It

Jan 31, 2013

I want to write a function which take an integer and return the number of digits in it i.e

int i = 123456
func(i) {
some code
}

output
the number of the digits are 6

View 9 Replies


ADVERTISEMENT

C++ :: Function That Should Return Number Of Digits In Integer Returns Last Digit

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

C++ :: Input Integer Then Output Both Individual Digits Of The Number And Sum Of Digits

Oct 11, 2014

My problem needs to prompt the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. An example would be entering 8030 and it spits out 8 0 3 0 as well as 8+0+3+0=11 and it needs to work with negative numbers.

Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int base;

[Code] ....

Now I don't know if any of this is right a hint my professor gave us is that to get the fourth digit you would do base mod 10 and to get the first digit you do base divided 1000...

Code:

{
int power;
int counter=0;
int value=1;
cout << "Enter the power of 10 you want: ";

[Code] ....

View 2 Replies View Related

C :: Function To Generate A Number - How To Find Same Digits

Nov 15, 2014

I use rand function to generate a number which consists of 3-5 digits(e.134,1435,73463..). The user decides whether he wants a 3 digit,4 digit or 5 digit number.After that,the user tries to guess the number.Its like mastermind game.The user will enter a number (with the same amount of digits) and the program will calculate how many digits from the secret number he has found and also how many digits he has found in the correct position(e.if the generatir produces the number 32541 and the user tries the number 49581 the program should tell him that he found 3 digits (5,1,4) and 2 digits in the correct position(5,1)) so that after some tries he finds the secret number.My problem is with the functions so that i can compare the digit of each number,find the amount of same digits and the amount of digits in same position.

View 5 Replies View Related

C++ :: Function To Count Digits Of Entered Number Doesn't Work

Feb 19, 2013

I wrote a program with function my own function which count the digits of entered number. The problem is whatever i type it shows 0 digits.Why is that?

Code:
#include <iostream>
using namespace std;
int cikCipari (int skaitlis, int cipars);
int main()

[Code] .....

View 7 Replies View Related

C/C++ :: How To Return Array Of Integer From A Function

Apr 2, 2014

The problem is how to return an array of integer from a function?

I'm writing a C program code to return an array of integers ( grades of students ) to the main function!

View 9 Replies View Related

C :: Function To Round A Number To Give Integer Number That Is Closer To Real Value

Oct 9, 2013

I was told to use a round function to round a number to give an integer number that is closer to the real value. (for example if the number is 114.67 I need to print an int value of 115 instead of 114)

I am not exactly sure how a round function works, but I am told to include math.h library. What I try doesn't seem to work.

View 1 Replies View Related

C :: Program That Adds All Digits Of Integer

May 5, 2013

Just wanted to share a program I made. It was the answer to one of the end chapter exercises in the C programming book I'm using, asking the reader to create a program that adds all the digits of an integer.

Code:

/* Program to calculate the sum of the digits in an integer */
#include <stdio.h>
int main () {
int number, right_digit, sum = 0;

[code]....

View 7 Replies View Related

C++ :: Converting Integer To String Based On Digits

Apr 23, 2013

I want to convert the integer into a string.

int x = 693;
char chararr[max];

In my homework, x is unknown. but don't worry, I wont ask for the full code. I just need the part where you change the int into a string/array of char.

I'm thinking in circles from where to start?

View 2 Replies View Related

C++ :: Turning Digits Of Integer Into Array Elements?

Nov 19, 2013

I have an integer that the user enters. I need each digit of the integer to be set as an element of an array. the integer could also be entered as an array, but I need the user not to have to enter each element and press ENTER.

View 5 Replies View Related

C++ :: Make A Function To Return Placement Number

Mar 25, 2014

I have a class called items and stored in a vector as vector<items*> item.now I want to make a function to return the placement # that the item is stored at with a unsigned int. but if I don't find the item is there like a null unsigned int that can be returned.

unsigned int someFunc(vector<items*> item)
{
unsigned int size = item.size();
for (unsigned int i = 0; i < size; ++i)
{
if (item[i]->getItemName() == "sword")
{
return i;
}
}
//i need code here incase the item is not there
}

because the item might not be there and i would like to do a line if(unsigned int) do this code

View 19 Replies View Related

C++ :: Separate Input Integer Into Its Individual Digits And Print

Apr 18, 2013

Write a full C++ program that inputs three-digit integer, separates the integer into its individual digits and prints the digits separated from one another. For example, if the user types 549, the program should print;

5 4 9

View 5 Replies View Related

C/C++ :: Take In Unsigned Number And Afterwards Return N-th Member Of Recursive Function

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

C++ :: Universal Order / Spacing Of Character Encoding For Integer Digits?

Feb 7, 2012

I have a char *pch that points to an integer digit 1 or 2 or ... 9. To get the character that's 1 less, instead of converting to int, minus 1, then converting back to char, I tried (*pch -1) and that seemed to work. I suppose that's because the particular character encoding on my system is such that the digits are encoded in the same order and spacing as the integers they represent. So the question is does this "convenience" feature hold true for all character encoding systems?

View 10 Replies View Related

C++ :: Recursive Function - Take Number And Return With All Sevens Turned Into Eights

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

C++ :: Create A Function With Integer Number As Parameter

Jun 20, 2013

I must create a function with a integer number as parameterThis function use the parameter for a loop...example

n = 1
output
1
2
3

n = 2
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3

View 1 Replies View Related

C++ :: Function That Stores Number Into Integer Array?

Apr 30, 2013

I'm writing a function that stores a number into an array. If the number is greater that the lowest number in the array then it replaces it. The array size is 10. When the number is stored in the array. The lowest number must then be remove.

View 3 Replies View Related

C/C++ :: Program To Find Number Of Digits In Number?

Aug 20, 2014

In the c pgm to find number of digits , if I am giving 001 as the input number ,why I am not getting the no. of digits as 3?

View 2 Replies View Related

C++ :: Remove Digits In Number

Jul 18, 2013

I know how to remove digits in number from right to left.For example: the number 319. If I do (number /= 10), I get 31.how can I remove digits in number from left to right.For example: the number 319. If I will do something, I will get the number 19.

View 15 Replies View Related

C++ :: Reversing Five Digits Number

Mar 9, 2013

//Program to reverse 5 digit number

#include <stdio.h>
void main() {
int num, i = 0;
char rev[5];
printf("Enter any 5 digit number

[Code] ....

WHILE DISPLAYING i am not getting correct output. whats wrong in rev[] array.

View 7 Replies View Related

C :: Input Number - How To Store 500 Digits

Oct 30, 2013

"Every number has at most 500 digits" under input. 500? How am I supposed to store that? And what if someone multiplies 999 ...(500 times) * 999 ... (500 times) ? I seriously doubt that my computer can store that! Is that just some huge value used to scare people off or is there some sneaky trick that I am unaware of?

I am done with the program, but won't post it, Lets not ruin the fum for ohers...Does that 500 limit also applies to result of operation?So max length of an input number is ~22 digits ? But still, how do I store 500 digits? Array would be a lot of wastage of memory (though I do have 256MB available).

View 2 Replies View Related

C++ :: All Possible Combinations For 4 Digits From A 5 Digit Number

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

C++ :: Printing Permutations Of Digits Of A Number?

Feb 8, 2013

I have been trying to write a recursive function to print the permutations of a n-digit number. This is the logic that i am trying to use.

1.call the permutation function,

2.keep the first digit in the first place and send the remaining 2 digits back to the permutation function recursively.

3.keep one of the two digits received and send the remaining digit to the permute function.

4. keep doing this till only one digit remains. this is the smallest solution so return it.

View 3 Replies View Related

C++ :: Storing Different Digits Of A Number Into Array Of Int

Oct 9, 2013

The aim is : If i have a natural number [max 5 digits] (say) n=13579. Then a[4]=1, a[3]=3, a[2]=5. a[1]=7. a[0]=9.

I start with a[4]= n/10000 .. then n= n - a[4]*10000;

then a[3]= n/1000. .. n= n - a[3]*1000;

i.e a[i]= n/10^i .... n= n - a[i]*10^i;

To reduce code size i used a for loop. The code is free from any syntax errors.

Its the "output" that is weird. I enter 13579 and what the program process is:
a[4]=1 a[3]=3 a[2]=5 a[1]=8 a[0]=5

Same goes for other numbers too.. leaving the most significant digit , the rest digits are just out of the logic!!

I am using CodeBlocks 12.11 and compiler is GNU GCC compiler.

Here is the output window: [URL] ....

Here is the code:

#include<iostream>
#include<math.h>
using namespace std;
int main() {
int n,a[5],i,t,dec,divisor; // n: storing number; t: copy of n,

[Code] .....

View 2 Replies View Related

C/C++ :: How To Count The Number Of Digits In A File

Jan 15, 2014

write a program that counts the number of digits in a file?

For example the file contains:

5 6 7 -8 9obvsefs
6 7 8i uhbnm
8 8

And the program returns: "The number of all digits is 10"

View 3 Replies View Related

C :: Program That Reads Number Of Digits From File?

Jan 16, 2014

I've been working on a program that displays the number of digits in each line of a file, but I feel stuck. Take for example a file that contains these characters:

6347aaa9
54j
811111
6a
709

And I'm trying to display a result like this

1 //that's the number of the line 5 //the number of digits
2 2
3 6
4 1
5 3

Here's what I've written so far:

Code:
#include<stdio.h>
int main() {
char a=0;
int number_of_digits=0, linescount=0, num, number_of_digits_per_line=0;
FILE *inputFile;
if(!(inputFile=fopen("C:TestTest.txt","r")))

[Code]..

I also thought of using fgets and strlen but I am not very good with them and couldn't get the program to work correctly. It did work but it displayed all characters, letters included, not only digits.

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved