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;
}
Related
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]);
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 .
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 have credit fictitious credit card numbers stored as char array and long long. In order to check if the card is VISA, MASTER or AMEX, I have to check first two digits either of this long long or of this string.
MasterCard numbers all start with 51, 52, 53, 54, or 55
American Express numbers all start with 34 or 37
Visa numbers all start with 4
Any idea how to do so?
I've tried to put in two separate integers and check later with if/else, but I guess there might be a better way to solve this.
Thanks for the help.
You could use a function on the char array like:
inline int starts_with(char *string, char *begin)
{
return strncmp(string, begin, strlen(begin));
}
And just check against constants
#define MASTER_1 "51"
...
#define MASTER_5 "55"
with
if (starts_with(string, MASTER_1) || starts_with(string, MASTER_2) || etc. )
{
...
}
else if (...) {
...
}
etc etc.
If the cases you mentioned are the only ones though, you could just switch on the first digit (since 5 => master, 3 => american express, 4 => visa)
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 years ago.
Improve this question
C Programming
Ask user to enter a random number between 1 and 100. Then ask how many numbers he wants to display that precedes first number he enters.
Let’s say if user enter 9 and wants 3 numbers that precedes 9, your program should display this:
• 6 7 8 9
Have no idea need some help.
For asking the user a question, you can use something like printf or puts.
To request numbers from the user, scanf is probably the best approach for the level you're working at.
By way of example, here's a complete program that asks the user for a number then gives them the next number:
#include <stdio.h>
int main (void) {
int num;
printf ("Enter a number: ");
if (scanf ("%d", &num) != 1) {
puts ("That wasn't a valid number");
return 1;
}
printf ("The next number is %d\n", num + 1);
return 0;
}
Analysing that code, and what it does when it runs, should be enough to get you started.
For your specific project, the following pseudo-code should help:
print "Enter the ending number: "
input endnum
print "Enter the count of preceding numbers: "
input count
num = endnum - count
do:
print num
num = num + 1
while num <= endnum
That's the algorithm you can use, I won't provide it as C code since you'll become a better coder if you do that yourself. In any case, those pseudo-code lines all pretty much have a one-to-one mapping with C statements so it should be relatively easy to get something going.
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 8 years ago.
Improve this question
This C program helps to find the largest number in given numbers but it is not working. I have highlighted the line where the problem is.
#include<stdio.h>
int main(){
int n,num,i;
int big;
printf("Enter the values of n: ");
scanf("%d",&n);
printf("Enter %d Numbers :",n);
scanf("%d",&big);
for(i=2;i<=n;i++){
scanf("%d",&num); //here is the problem..
//what it is reading as `num` without asking me to entering any thing ?
if(big<num)
big=num;
}
printf("Largest number is: %d",big);
return 0;
}
If you need the program to ask for input in a user-readable way, you can put
printf("Enter number #%d:",i+1);
before that line with scanf.
Anyway, the program will do its job just the same if you remove all printf's (and so, print no prompts, only wait for user input). They are only for users' convenience.
Your first scanf command only reads in your first number from stdin. The one on line 14 reads the rest, one per time around the loop. Then each one gets compared to variable "big", and replaces it it necessary.