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.
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
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;
}
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 7 years ago.
Improve this question
I need a function that gets input in the form of int1, int2, ..., intn.
I neeed to store the values of these integers in an array. A separate function is used to get the number of integers to be read. How can I make the two functions work?
If it's not clear, it's something like this:
function1 gets an integer to get the number of input to be read. Then the function2 will read that input plus one but the input must be in a single line and must be separated by a comma and/or a white space.
Function1 gets, for example 5. function2 will want to read input like: 3, 21, 5, 1, 5, 2 and store it into a separate array for later use.
Can anyone help? Thanks. I thought of using loops but I remembered that the input must be in one line. Maybe scanf? With [^,]? But how do I make it work with the first function?
Try this:
#include <stdio.h>
void getInput(int sizeOfInput, int arr[]) {
int i = 0;
printf("IN");
for(; i < sizeOfInput - 1; ++i) {
scanf("%d, ", &arr[i]);
}
scanf("%d", &arr[i]);
printf("OUT");
}
main(){
int sizeOfInput = 0;
printf("Enter how many numbers do you want to enter?");
scanf("%d", &sizeOfInput);
int arr[sizeOfInput];
getInput(sizeOfInput, arr);
}
Sorry I am lazy but for you to learn it would be the best to figure out what this code does before you use it, that is also a reason why I did not comment it.
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.