void main()
{
int u, t, h ;
printf("\n Enter a number (with 3 digits) \n");
printf("\n Enter the unit digit number \n");
scanf("%d",&u);
printf("\n Enter the tenth digit number \n");
scanf("%d",&t);
printf("\n Enter the hundredth digit number \n");
scanf("%d",&h);
}
I want them in order like if user input u as 1, t as 2 and h as 3, then after concatenation it should print 321 together as one integer number.
With this solution the final string stored in str is portable throughout your program.
char str[16];
sprintf(str, "%d", (h*100) + (t*10) + u);
printf("%s\n", str);
Why not simply multiply them?
printf( "%d\n", 100 * h + 10 * t + u );
Take into account that as only one digit is read for each number you could write for example
scanf( "%1d", &u );
^^
Doesn't this work?
printf("%d%d%d", h,t,u);
Related
Write a program that will accept N even integers as input and print:
the number of numbers given as input
the average of multiples of 3
the product of multiples of 4
the product of negative numbers
The will only use the repeating while structure. Create a variant of the program that will only use the while structure.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i, N, P=0, M=0, T=1, X=-1;
printf("Enter ending value: \n");
scanf("%d", &N);
while (P<=N){
if(i%2==0){
scanf ("%d", &i);
P= P + 1;
if(i%3==0){
M = M + i;
}
if (i%4==0){
T = T * i ;
}
if (i < 0){
X = X * i;
}
M = M / 2;
}
}
printf ("The sum of the numbers is %d \n", P);
printf ("The average of multiples of 3 is: %d \n", M);
printf ("The product of multiples of 4 is: %d \n", T);
printf ("The product of negative numbers is: %d \n", X);
return 0;
I tried this but i dont know how to check if a number is a multiple of 4 and 3 . If it is a multiple of 3 the sum of the digits % 3 gives 0, but i dont know how to check that. For the multiples of 4 ,if the last two digits are divisible by 4. If you halve the tens and ones part of a number and the answer is even, then it is a multiple of 4 but i also dont know how to check that.
I have started an intro to programming course that uses the C language and we have an assignment to make a program that takes a 5 digit number from the user such as 12345 and it prints it out as 1 2 3 4 5.
I tried to google around for help but all the answers given used code way too complicated for my understanding considering the course just started and we have only learned printf and scanf, if and switch statements and while and for loops.
I tried putting all the numbers given into separate int variables which made the program stop and then tried to put them into chars but the testing program said it was wrong since we are supposed to use int.
Is there a simple way to do this?
EDIT:
What I have tried:
#include <stdio.h>
int main(void) {
int num1,
num2,
num3,
num4,
num5;
printf("Give 5 digit number > ");
scanf("%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5);
printf("Seperated number is %d %d %d %d %d", num1, num2, num3, num4, num5);
return (0);
}
Also tried that code but with char variable type but that wasn't allowed it has to be int.
The testing program gives an expected output which for 00001 is Given number 1 seperated is 0 0 0 0 1 or for -12321 is Given number -12321 seperated is -1 -2 -3 -2 -1
You can read user input into a char array using scanf.
Then using for loop you can loop through each char (1,2,3,4,5)
Use printf in the loop to print from array and a space. printf("%c ", input_from_user[i]);
I suppose the point here is understanding how C stores things. You're reading in characters (that is a set of 8-byte values that are commonly displayed as letters) and converting each one to an integer (commonly a 32-bit value that stores a number)
So you want to read the input using scanf to get a character string of your 5 digits, then use a for loop to go over each one, reading a single character from the string and converting it into an integer variable. then printf that variable plus a space, and move on to repeat the next digit.
If you read the digits in one by one then its even easier as you only need the for loop to read, convert, and print each digit as its read.
According to the question, you would be given a number of 5 digits and then you have to print the digits of that number as well.
So, in your program, you have taken five digits individually as input, to me it seems wrong. I am giving my solution below:
#include <stdio.h>
int main(void)
{
int num,idx=0,i=0;
int arr[5]; // for storing the five digits;
scanf("%d",&num);
while(num){ //loop will terminate when the num = 0
int digit = num%10;
num = num/10;
arr[idx] = digit;
idx++;
}
//Printing from the last, as the digit are stored in reverse order.
for(int i=4;i>=0;i--){
printf("%d ",arr[i]);
}
return 0;
}
I don't really understand the purpose of this exercise. If you are learning C, the last function you should learn is scanf. It boggles the mind that so many people teach it early. Try very hard not to use scanf at all until you understand the language well enough to realize that you probably never need it. That said, I suppose the point of the exercise is to do something like:
#include <stdio.h>
int
main(void)
{
unsigned num;
char buf[6];
printf("Give 5 digit number > ");
if( scanf("%u", &num) != 1 || num > 99999 ) {
fprintf(stderr, "Invalid input\n");
return 1;
}
printf("Separated number is");
snprintf(buf, sizeof buf, "%d", num);
for( int i = 0; buf[i]; i++ ) {
printf(" %c", buf[i]);
}
putchar('\n');
return 0;
}
Although likely beyond OP's stage, code can use recursion to print the value from most significant to least.
#include <stdio.h>
void print_digit(int n, int depth) {
if (depth > 1) print_digit(n/10, depth - 1); // print "left" digits first
printf(" %d", n%10);
}
void print_5(int n) {
// To do: checking for correct spelling
printf("Given number %d seperated", n); // or separated
print_digit(n, 5);
printf("\n");
}
int main(void)
{
print_5(1);
print_5(-12321);
return 0;
}
Output
Given number 1 seperated 0 0 0 0 1
Given number -12321 seperated -1 -2 -3 -2 -1
You could use your approach by limiting the number of bytes you read for each number:
#include <stdio.h>
int main(void) {
int num1, num2, num3, num4, num5;
printf("Give 5 digit number > ");
if (scanf("%1d%1d%1d%1d%1d", &num1, &num2,&num3, &num4, &num5) == 5) {
printf("Separated number is %d %d %d %d %d\n", num1, num2, num3, num4, num5);
}
return 0;
}
Note however that this is probably not what you are expected to do. You should instead read a single number and decompose it into its five digits:
#include <stdio.h>
int main(void) {
int num;
printf("Give 5 digit number > ");
if (scanf("%d", &num) == 1 && num >= 10000 && num <= 99999) {
printf("Separated number is %d %d %d %d %d\n",
num / 10000, num / 1000 % 10, num / 100 % 10, num / 10 % 10, num % 10);
}
return 0;
}
Im trying to take 4 digit number from user in c language.
1) I have tried using scanf("%d%d%d%d",&a,&b,&c,&d);
When I compile the code i write the numbers (1234) and enter the code does not execute after pressing enter.
2) I tried :
a=getche();
b=getche();
c=getche();
d=getche();
but when using getche a,b,c,d saving as char not integer. And the code didn't work properly again.
What should i do? How can i take 4 numbers from user and save every digit in different integer?
There is another way just enter the entire numbers and they will be separated:
int number, result;
printf("Please, enter four numbers: ");
scanf("%d", &number);
while (number > 0) {
result = number % 10;
number /= 10;
printf("%d\n", result);
}
printf("\n");
The result:
Please, enter four numbers: 1234
4
3
2
1
How can i take 4 numbers from user and save every digit in different integer?
Because there are some good comments about your problem I'll stick to the Question.
If you need to inform the User about its wrong input like:
1f
or:
g2
Something like this could be a good choice:
#include<stdio.h>
int checkInput(void);
int main(void){
int a = checkInput();
int b = checkInput();
int c = checkInput();
int d = checkInput();
printf("\nYour numbers are:\n");
printf("A = %d\n", a );
printf("B = %d\n", b );
printf("C = %d\n", c );
printf("D = %d\n", d );
return 0;
}
int checkInput(void){
int option,check;
char c;
do{
printf("Please type a number:\t");
if(scanf("%d%c",&option,&c) == 0 || c != '\n'){
while((check = getchar()) != 0 && check != '\n');
printf("\tI sayed a Number please\n\n");
}else{
break;
}
}while(1);
return option;
}
Output:
Please type a number: 2
Please type a number: 1f
I sayed a Number please
Please type a number: 1
Please type a number: g2
I sayed a Number please
Please type a number: 3
Please type a number: 4
Your numbers are:
A = 2
B = 1
C = 3
D = 4
I'm a newbie!
I'm supposed to get 2 integers from the user, and print the result(sum of all numbers between those two integers).
I also need to make sure that the user typed the right number.
The second number should be bigger than the first one.
And if the condition isn't fulfilled, I have to print "The second number should be bigger than the first one." and get the numbers from the user again until the user types right numbers that meet the condition.
So if I programmed it right, an example of the program would be like this.
Type the first number(integer) : 10
Type the second number(integer) : 1
The second number should be bigger than the first one.
Type the first number(integer) : 1
Type the second number(integer) : 10
Result : 55
End
I think that I have to make two loops, but I can't seem to figure out how.
My English is limited, to help your understanding of this quiz, I'll add my flowchart below.
I tried many different ways I can think of, but nothing seems to work.
This is the code that I ended up with now.
But this doesn't work either.
#include <stdio.h>
void main(void)
{
int a = 0;
int b = 0;
int total_sum = 0;
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
while (a > b) {
printf("The second number should be bigger than the first one.\n");
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
}
while (a <= b) {
total_sum += a;
a++;
}
printf("Result : \n", total_sum);
}
Instead of using loop to sum the numbers, we can use mathematical formula.
Sum of first N integers= N*(N+1)/2
#include <stdio.h>
int main(void)
{
int a = 0;
int b = 0;
int sum;
//Run infinite loop untill a>b
while(1)
{
printf("Type the first number : ");
scanf("%d", &a);
printf("Type the second number : ");
scanf("%d", &b);
if(a>b)
{
printf("The second number should be bigger than the first one.\n");
}
else
{
break;
}
}
//Reduce comlexity of looping
sum=((b*(b+1))-(a*(a-1)))/2;
printf("Result : %d " , sum);
return 0;
}
After corrections your code should run. The community has pointed out many mistakes in your code. Here's an amalgamated solution:
#include <stdio.h>
int main(void)
{
int a = 0;
int b = 0;
int correctInput=0;
int total_sum = 0;
do
{
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
if(a<b)
correctInput=1;
else
printf("The second number should be bigger than the first one.\n");
}
while (correctInput ==0) ;
while (a <= b) {
total_sum += a;
a++;
}
printf("Result : %d \n" , total_sum);
return 0;
}
Factorials are used frequently in probability problems. The factorial of a positive integer n (written n! and pronounced "n factorial") is equal to the product of the positive integers from 1 to n: n! = 1 x 2 x 3 x x n Write a program that takes as input an integer n and computes n!.
I want my code to prompt the user to input a phone number of form 1(xxx)-xxx-xxxx and then sum the digits of the number. However I do not know what is wrong with my code. See below
printf("Enter a phone number in 1(xxx)-xxx-xxxx format: \n");
scanf(" %*c%*c%d %d %d %*c%*c%d %d %d %*c%d %d %d %d", &i, &j, &k, &l, &m, &n, &o, &p, &q, &r);
sum = (i + j + k + l + m + n + o + p + q + r);
realsum = sum + 1;
printf("The sum of the digits = %d \n\n", realsum)
;
Can anyone help? It seems to be assigning the first part of the number (xxx) entirely to i, and j is zero. How do I get it to assign each digit to each variable one by one?
The problem is that %d keeps reading until it finds a character that can't be part of a decimal number. So if the user enters 1(123)-456-7890 then the first %d will set i to 123.
The solution is to use %1d. That tells scanf to read a one-digit decimal number.
btw: you should verify that the return value from scanf is correct. In this example, the correct return value is 10. Any other number indicates that the user did not enter a valid phone number.
You did account for the non-integer characters that the user enters, but integers are read as a whole, so 123 is not read as1 then 2 then 3 but rather as 123.
scanf(" %*c%*c%d %*c%*c%d %*c%d ", &i, &j, &k);