Print number with zero on first place - c

How i can print for example?
int a = 0145236
When I want to use it, it always gives me a octal number.

Whenever you put zero in front of your number it is printed in octal only. That is the way c works.
However, if you take input from the user and supposing user entered 0123456 it is stored in your variable as 123456 so just don't add 0 in the beginning of your integer number when hard coding.
In case you need to add leading zeros in your number this may help Printing leading 0's in C?

Related

Can you give me some suggestions on my problem?

I want to solve the problem. The problem is to check whether the number is palindrome or not.
There has been a lot of solutions that exist online. But I am trying to solve this problem through my approach without seeing any solution from the internet. I am trying this way->
#include <stdio.h>
int main(){
//Declaring variables for further proceed
int number,reminder,quotient=1;
//Just take the input from the user
printf("Input : ");
scanf("%d",&number);
while(quotient!=0){
quotient=number/10;
reminder=number%10;
printf("%d",reminder);
number=quotient;
}
return 0;
}
The problem is : My code is work for displaying the reverse order of any given number. But I could not check with this reverse order with the given number. If you can then you are most welcome. Thank you in advance.
//Write a program to check the number whether it is palindrome or not
#include <stdio.h>
int
main(void){
//Put variables for the further proceed
int number, quotient=1, remainder,i=0;
//To declare a character array
char text[100];
//To show the message to the user
printf("Enter an integer number :");
//Taking input from the user
scanf("%d",&number);
//For finding escape the integer number in the reverse order specifically
int number_update=number;
//To find out the integer number in the reverse order
while(quotient!=0){
quotient=number_update/10;
remainder=number_update%10;
number_update=quotient;
text[i] = remainder + '0';//Converts integer to character and store to the array
i++;
}
//Converts the string to a whole integer
int result_of_reverse=atoi(text);
//Check the result of reverse order with the given integer number
if(result_of_reverse==number){
//To display the result
printf("This is a palindrome number");
}
else{
//To display the result
printf("This is not a palindrome number");
}
}
Eventually, I have solve my problem. Thank you all for your suggestions.
I applaud your decision to follow your approach through.
Allow me to start a step-by-step answer according to the compromise described here (applicable to homework, challenges and very disciplined self-learners like you):
How do I ask and answer homework questions?
Step 1:
You program is able to look at digit by digit of the number in the right order (you can output them). But it does not get an overview of them. You do not store them. Neither separatly nor as a whole (string or number). Consider how to change that.
Do you know a way to store several seperate digits?
Do you know a way to store a string of characters?
Aleternatively, if you do not want to store the reordered digits, i.e. if you want to continue looking at single digits, then you need to always look at two single digits, one pair after the other. Each pair needs to consist of one digit from the high end and one digit from the low end. Maybe you can think of a way to start the number from both ends while looping. More variables to store intermediate results might help with this.
Step 2:
You "know a little bit of the way how to store a string of characters", so do that. Store the characters which you output. If you do not know how to get from digit to character read up on sprintf(). This is a little complex, because the goal is to have a single string, not several strings with one digit each. So ...
Alternatively, to store single digits as their own integers, read up on "arrays". You specifically need an array of int.
If both seems to complicated do not forget the alternative from first step, to look at pairs of digits, from both ends of the number. For that try to print for example for the input "654321" the output "6:1, 5:2, 4:3". If you can do that, things get much easier.

Having hard time understanding how to us void printSp(int) in this given task,

I only really understand how to do equation in program I need help on how to approach this task, also I not really sure whats its asking of me to do.
Suppose you are given a function with the following declaration:
void printSp(int); /* prints specified number of spaces */
Write a function named printTri that takes a single argument, a character, and returns an integer value. If the character is not a capital letter (between 'A' and 'Z'), then the function simply returns 0. Otherwise, if it is a capital letter, the function will print a triangle of characters that looks like this: A ABA ABCBA ABCDCBA
NOTE: WIth a fixed-width font, the center letter in each row would line up. Write this out for yourself on paper, to figure out how many spaces should be printed on each line before the characters start. The bottom line has zero spaces. How many spaces should the top line have? The letter passed in becomes the highest letter in the triangle. For example, to print the triangle above, the caller passes in 'D'. After printing, the function returns the total number of non-space characters printed. For example, for the example triangle above, the function must return 16. You must call the printSp function, once per line, as part of your solution. (NOTE: Call printSp for every line, even when the number of spaces to print is zero.) History:
This is what I have so far I know its not much but its all I understand how to do.
if (x >='A' && x <= 'Z') printf(" A\n ABA\n ABCBA\nABCDCBA")
else return 0;
The function printSp() prints spaces.
You currently are outputting a hard coded number of spaces instead of using printSp(). Swap printf(" ") for printSp(3)
Reading the question, they want you to output a variable number of rows based on the letter provided. So for D you print the pattern you have hard coded which contains four rows and enough letters to output the D. For E you add another row.
I generally suggest students approach problems like this by starting with hardcoding, as you have. Ensure that you incorporate the required features, like printSP(). Then make the code more generic to handle other inputs.

String input into int array -- C

I'm trying to write a program that will convert a floating point number into its IEEE 754 single representation and vice versa. I'm stuck on how to take the input, which will be in the form "0xABCD123" or "2.83234e-2" (with the hex representation always preceded by "0x"), and take each element and put it into a char array.
For example, the input "0xABCD123" will be in a char array of size 10. I will remove the "0" and "x" elements, and then go through each element individually and convert into floating point from there with the use of a for loop.
Getting the argument from the command line is simple enough I think. The argument is stored in argv[1], then I save that into an initialized string variable called input. The problem is taking input and putting each element into a character array. The second problem is determining the length of the array if the input is not in IEEE format. I haven't found a function that determines the length of a string (removing decimals points and account for the 'e').
Also, a couple questions: Does C automatically recognize the "e-2" part of an input and translate that into the actual number? Is there some command I can use to do that?
How would type casting work here? If the input was "2.83234e3", and I wanted to save the part to the left of the decimal point as an int (because that would be easier to translate), and then the left as an int as well, how would I split up the number through type casting? Would it be something like input = (int)left;?

How to display output after entering some integers separated by a newline without the use of arrays?

Task:
t denotes the number of inputs, followed by t lines, each containing a single integer n.
For each integer n given at input, display a line with the value of n.
Sample input:
4
1
2
5
3
Sample output:
1
2
5
6
The output should appear after all lines of integer n are taken as input, i.e, it should not display output after each line of input.
How can this be done using a while loop without the use of array to store the input numbers?
while(i<t)
{
scanf("%d",&num);
printf("%d",&num);
i++;
}
This code is working fine if the input numbers n are separated by a space and appears on the same line. But when the input numbers are provided after a newline, it displays the corresponding output after each input value.
Generally such type of input output are used in coding competition where a user is expected to match the expected output to the actual output.
Try pasting the input using a command prompt or online compilers and check it. It is totally fine as the output is as expected.
How does it take if the total input is given at once.
First it reads the t and then it reads the num and prints the number but your printing is actually after the input. This is how the output is checked in a coding competition.
PS: If you want everything after input, use arrays.
If you want to this without using array, then the best way i could think of is recursion. But note, that internally, your values will be stored in the stack frames, and your values will be printed in reverse(because stack is LIFO). Here is how you can do,
void foo(i, t)
{
if(t==i)
return;
int num;
scanf("%d",&num);
foo(++i, t);
printf("%d\n",num);
}
Note, however, that the values will be printed in reverse, as stack is LIFO

print 0's before a variable

I know in c, c++, and all other kinds of programming code you can use the syntax "%4d" to print a total of 4 digits. If the variable is only one digit, it will print three 0's and whatever your variable is, if your variable is 2 digits, two 0's and so on. However in Octave it seems that it will print out spaces instead of 0's. Is there a way to force it to print out zeros but keep the specified length of 4 integers?
This is for naming files that the program is outputting so the leading zero's are needed to help organize the data that is outputted.
The place to look is the Matlab documentation: http://www.mathworks.co.uk/help/techdoc/ref/sprintf.html.
But the answer is something like:
sprintf('%04d', 42); % Prints "0042"

Resources