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


ADVERTISEMENT

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 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++ :: 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++ :: Find Sum Of All Digits In Number Until Sum Becomes A Single Digit

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

C++ :: Return The Digit At User Specified Index Of Integer

Oct 10, 2014

Return the digit at the user specified index of an integer. If the integer has n digits and the index is NOT in the range 0 <=index <n return -1 Start the digit numbering at 0. Example, if user input is 4 (index) and the integer equals 123456790 the return value for the function is 5 (start index at 0) ; if user input is 40 (index) and the integer equals 123456790 the return value for the function is -1

#include <iostream>
#include <istream>
#include <cstdlib>
#include <cassert>
#include <string>
using namespace std;
int getIndex(int, int);

[Code] .....

View 4 Replies View Related

C/C++ :: Swap A Digit From A Number With Another Digit Using Function

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

C++ :: Input Validation For 5-digit PIN Number Read From Integer

Oct 22, 2014

The problem states that i need to accept any pin number between "00000" and "99999". I need to read the PIN the user enters as an integer. The problem is that if the PIN is read as an integer, the PIN "01111" will be "1111" which is invalid and the pin "00001" would be read as "1" which is also invalid. How would I go about fixing this problem for PIN numbers that start with a "0"? Again I cannot read the PIN as a char array or string.

View 2 Replies View Related

C :: Program That Return Value Of A Function That Returns Price

Oct 24, 2013

I'm writing a function that is to return the price of something.. What would be the most appropriate return type for this? Like in Java it would be a double...

View 6 Replies View Related

C++ :: Function That Returns Integer Value And Displays It

Mar 8, 2013

Is it possible to create a function that can both return and display a value. I was trying to make a program that computes and prints terms of the Fibonacci series using a recursive function.

View 3 Replies View Related

C++ :: Function That Accepts Integer And Returns A String?

May 5, 2014

How to go about making a function that accepts an integer and returns a string with any one of 5 strings containing the name of the object. For example object number 3 might be "Pen". Object 4 might be "Paper".

To do this would I just use an array?

View 4 Replies View Related

C :: Write A Function That Finds Integer In Array And Returns Its Corresponding Index

Mar 20, 2013

I have an assignment which requires me to do the following:

Required to write a function that finds an integer in an array and returns its corresponding index. The function must be called findNumber. It must have FOUR parameters:

- The first parameter is the array to be searched
- The second parameter is the integer to be found within the array
- The third parameter is the size of the array
- The fourth parameter is an integer that indicates whether the array is sorted. A value of 1 means the array is sorted; a value of zero means the array is not sorted.

Since a function can only return one value(To return the position of a required integer in an array in this instance) I have tried to make use of pointers to try and return a value stating whether the array is sorted or not.This is my code : (It compiles perfectly but it does not produce any outputs)

Code:

#include <stdio.h>
#define SIZE 10
size_t findNumber(int *sort, const int array[],int key,size_t size);
int main(void){
int a[SIZE];
size_t x;

[code].....

View 1 Replies View Related

C++ :: Function That Returns Max Of A Variable Number Of Scalars

Oct 23, 2014

I need to create a function that takes as an input a variable number of scalars and returns the biggest one. Just like std::max() does for 2 elements, but I need it for an undefined number of elements, can be 2, or 5, or 10 etc.. How to approach this?

What I need it for: I'm working with a bunch of vectors, maps, etc. and I need to find out which has the most elements. So I was thinking that I should end up with something like

int biggest = max(vector1.size(), vector2.size(), map1.size(), ...);

View 8 Replies View Related

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 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++ :: Repeated Digit In Integer

Oct 19, 2013

this code :

#include <cstdlib>
#include <iostream>
#define TRUE 1
#define FALSE 0
using namespace std;
typedef int Bool;

[Code] ....

Gives repeated digits in an integer but only in one condition : Only if the repeated digit is the result of n%10 where n is the integer the user writes. If the repeated digit is not the result of n%10 , then the compiler gives a wrong result.

so the question is : how to make this code gives the repeated digit in an integer (regardless the fact that the repeated digit is the result of n%10 or not and especially with making the minimum of changes on the code)????????? ?????

View 3 Replies View Related

C :: How Program Works If Integer Is More Than One Digit

Mar 15, 2013

Assignment: Take an integer keyed in from the terminal and extract and display each digit of the integer in English. Ex. 932 --> nine three two

Code:

/*This program takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English.*/
#include<stdio.h>
int main(void)
{
//DECLARE VARIABLES
int num;
}

[code]....

I don't know how the program works if the integer is more than one digit.

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++ :: Create Program That Lists Off Each Digit Of Integer?

Mar 20, 2014

I need to create a program that lists off each digit of an integer and then display the sum off all the digits in that integer. I know that sepereatly the sum function i wrote works. But the first part which i try to list off the digits work but in reverse order which i dont know how to correct. and for some reason that i cant figure out this is affecting the sum output.

#include <iostream>
using namespace std;
int digcount (int x) {;

[Code]....

View 5 Replies View Related

C++ :: Read (three Digit) Integer Representing Value To Be Encrypted

Nov 3, 2013

I have been working on some C++ code that doesn't seem to be going right. I'm wanting it to read a (three-digit) integer representing the value to be encrypted, a (one-digit) integer representing the encryption key, encrypt the value and print the encrypted value. The encrypting method used is that each digit in the given number is replaced by ((the sum of that digit plus key) modulo 10) then the first and last “encrypted” digits are swapped.

For example, if the number entered was 216 and the key given was 7, after applying the encryption procedure described the first digit (2) would become 9, the middle digit (1) would become 8 and the last digit (6) would become 3. The first and last encrypted digits are then swapped. The program displays the encrypted number: that is 389 in this case.

#include <iostream>
using namespace std;
int main() {
int isolateDigits();
int replaceDigits();
int swapDigit1withDigit3();

[Code] ....

View 1 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++ :: Convert Positive Integer Into Its English Name Equivalent To Digit?

Jul 13, 2014

convert a positive integer code into its english name equivalent for digit. A valid code is of size between four (4) to six (6) digits inclusive. A zero is not allowed in the code.

example : if the input is 234056 the output is : INVALID CODE (PRESENCE OF ZERO)
if the input is 23456 the output is : TWO THREE FOUR FIVE SIX
if the input is 9349 the output is : NINE THREE FOUR NINE
if the input is 245 the output is : INVALID CODE (3 DIGITS)
if the input is 2344567 the output is : INVALID CODE (7 DIGITS)

step 1 : input code
step 2 : count the number of digits in the code
step 3 : if there is a zero in the code, "INVALID CODE (PRESENCE OF ZERO)" go to step 4
step 4 : if number of digits is mode or equal than 4 and less or equal than 6, go to step 5 else display the following message "INVALID CODE (<number of digits> DIGITS)
step 5 : call a function called digit_name to convert each digit into its
equivalent english name. display the result
step 6 : print the digits in reverse order
eg; if input is 13453, reverse order is 35431

View 8 Replies View Related







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