Addition of integers in C [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to create a program which asks to the user to type in two integers, from 0 - 10. The program then turns the integers into words and the result is also printed into words.
EXAMPLE:
Please enter two integers: 2 5
two + five = seven

You can create a simple array along the lines of (that ... isn't literal, I just couldn't be bothered typing out all the numbers):
char *nums[] = { "zero", "one", "two", ... "twenty" };
Then, for a given number n where 0 <= n <= 20, you can output the number with a simple:
printf ("%s", nums[n]);
So, other than the input part (which is almost certainly covered elsewhere on SO), the code would be:
int n1 = 2, n2 = 5;
printf ("%s + %s = %s", nums[n1], nums[n2], nums[n1 + n2]);

Related

How to merge multiple numbers into one number like 4,0,0 into 400 without stdlib [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to parse a file that has following data eg:
MAGICNUMBER 400
4 is = 0x34
0 is = 0x30
4
0
0
are different unsigned chars
what i want is those different chars to be converted into
unsigned int x = 400;
when parsing them into my program i want to merge them into one integer i tried bitshifting but it didn't work and i probably did it very wrong and got a very large number probably due misunderstanding of something, what i'm susposed to do to merge those numbers without string tricks and without using std but only using bitshift with a explanation how it works?
Each digit is c - '0'. When you get a new digit, you know that prior ones are one decimal place greater, so you multiply the current number by 10 and add the new digit:
char *s = "400";
int sum = 0;
while(*s >= '0' && *s <= '9') {
sum = 10 * sum + (*s - '0');
s++;
}

In C language, how can I print 1234567890 to create a position/location ruler? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
basically, I want to create a Position/location ruler which shows each numeric position starting at 1. let's say, if the characters in the above statement are 20 then in the next line 12345678901234567890 should be printed. similarly, if the characters are 30 then 123456789012345678901234567890 should be printed.
how can I do that?
Let's say n is the length you need to match
int n = 20; // or 30, or whatever
for (int i = 0; i < n; i++) printf("%d", (i + 1) % 10);
puts(""); // add newline

I want to print a name inputted by the user and some stars(inputted too) EXACTLY before and after the name [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to print stars before and after a name, like, ****Johannah****.
WITHOUT using the "for" loop, using total and complete logic.
The number of stars to be printed should be entered by the user and then the stars should be before and after the name in the code as the output.
For ex:" Enter the name: Johannah.
Enter the number of stars to be printed before and after the name: 5.
Name: *****Johannah*****
Please help. Thanks.
You Could Well Do This First Make a char array , Containing Number Of Maximum Stars that will be asked to print.
char stars[] = "***************************************";
Then Ask User Input for number of stars they want to print.
int a;
printf("Enter Number of Stars You Want To Print : ");
scanf("%d" , &a);
Then You print it using
printf("%.*s\n" , a , stars);
What is basically happening is that you are setting variable width by using %.*s , here * is taking a , for example if a = 5 then it turns out to be %.5s and then it print first 5 characters of string.
The catch here is you should know maximum number of stars that you would be asked to print so that you can initialize the character array according to your need.
Please See These Question To Know More How to print only certain parts of a string? and Set variable text column width in printf .

C program to find the number with most different digits [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The user puts n numbers from his keyboard and I should find, between them, the number with the most different digits.
Example:
Enter number: 12
Enter number: 123
Enter number: 5555
The number with the most different digits is 123 (3 different digits).
The problem is that I am not supposed to use vectors or arrays or something else.
I should use only loops and if clauses. I know how to find the number of digits in a given number but how am I supposed to compare all of them to find the number of different digits?
The easiest way is to iterate through each possible digit (0 through 9) and see if it is there.
Something like this to determine the unique digits in a number:
int FindNumberDigits(int number) {
int uniqueDigits=0;
for(int i=0;i<10;++i) {
int numberTemp=number;
while(numberTemp) {
if(numberTemp%10 == i) {
++uniqueDigits;
break;
}
numberTemp/=10;
}
}
return uniqueDigits;
}

Validation of space in C syntax [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to write syntax that can validate allow only one space input from user? Example, if the user input
"eat_food" it is VALID , when user enter "eat__food"(with 2 spaces) it would be INVALID..
and then show the appropriate error message to return right input?
This can be checked with strstr():
#define IS_VALID_INPUT(input) !strstr(input, " ")
This will result in 1 if there is not more than one space in the input, and 0 otherwise.
After the following code, n is the number of spaces in the string str.
Then you can check it.
int i, n = 0;
for (i = 0; str[i]!='\0'; i++)
n = (str[i] == ' ') ? (n+1) : n;

Resources