Sum the digits of a phone number using scanf - c

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);

Related

I have this as an exercise and i want to check the input numbers that are divisible by 3 and 4, is there a specific condition or what?

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.

Stuck on multiplication table in C

I'm new to C programming. I was trying to write a program that accepts an integer from user and displays its multiplication table up to 10 multiples.
This is my program:
#include <stdio.h>
int main ()
{
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf(" %i ", &number);
while (count <=10)
{
sum = number * count;
printf("%i x %i = %i\n", number, count, sum);
count += 1;
}
return 0;
}
Compilation successfully completes, but when I execute the output file, nothing happens, the terminal is stuck at nothing, i've to press ctrl+c to get out..
This is due to the spaces used in your scanf command.
If you replace that with
scanf("%i", &number);
you get an instant response.
With your scanf format " %i ", the scanf function will read (and skip) possible leading spaces because of your leading space in the format.
Then it will read the integer.
Then, due to the trailing space, it will read and discard space until it find a non-space input.
Since there's no non-space input afterward, then scanf will block until you give some non-space input.
Solve simply by not having any spaces in the format. Or by entering some extra dummy input (followed by Enter).
The problem resides with the scanf.
Just replace
scanf(" %i ", &number);
with:
scanf("%i", &number);
and it will work.
#include <stdio.h>
int main (){
int number;
int count = 1;
int sum;
printf("Enter a number to display its table: ");
scanf("%d", &number);
while (count <=10){
sum = number * count;
printf("%d * %d = %d\n", number, count, sum);
count += 1;
}
return 0;
}
Note: You can use both %d or %i where %d specifies signed decimal integer while %i specifies integer.
Problem: The problem of your code was using a whitespace before %i.
Wrong:
scanf(" %i ", &number); //Wrong
Right:
scanf("%i", &number); //Right.

Trying to make scanf not go to a new line in a for loop (C Programming)

Sorry if my English is poor. What I'm trying to do is get the scanf to be entered on the same line. For example Enter value: 1 6 8 9 4 1 2 8 5 and it to be separated by a space. Then the numbers to be stored in an array. This is my code:
#include <stdio.h>
int main(void)
{
int a[10], smallest, i;
printf("Random\n");
for (i = 0; i < 9; i++)
scanf("%d", &a[i]);
smallest = a[0];
for (i = 0; i < 9; i++)
{
if (a[i] < smallest)
{
smallest = a[i];
}
}
printf("\nSmallest Element : %d\n", smallest);
}
Thanks for any help!
Edit: I'm trying to make the user enter 9 numbers which are stored in the array using scanf but when entering the numbers the scanf goes to a new line for example:
> 5
> 6 and so on what I want is for them to enter the number numbers on the same line with a space in between like this Enter value: 1 6 8 9 4 1 2 8 5
Scanf will await for a complete line. I suggest you take your input as a string then use strtok to extract the values and then assign.
Edit: You could use the scanf like that:
scanf( "%d %d ...", &a[0], &a[1]...); //as many values you're to assign
However, I prefer the method I proposed initially. Keep in mind scanf is derived from "scan formatted". You'll also have to handle the result from the scanf, it returns the number of values successfully filled.
you can scanf all numbers in one statement: scanf("%d %d %d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]);
This will let you scan 10 numbers with spaces between them, without the need to hit enter every time
note that I scanned 10 ints, because your array can store 10 ints, while your loop only scans 9... (to fix it, change i < 9 to i < 10 as Sourav Ghosh suggested)

How to combine three integer numbers in C

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);

selective input using scanf

From an input of n numbers(value of n is known) separated by spaces,
eg(here n is 6):
3 5 8 9 13 2
If I wish to accept only the 2nd and the 5th number and ignore the rest, how do I do it using scanf?
I found accepting the numbers in an array and using only the required ones a bit redundant, so I'm looking for a smarter alternative.
If the knwon number n is a fix number (n=6 always)
then you can use the following scanf
int a2, a5;
scanf("%*d %d %*d %*d %d %*d", &a2, &a5 );
scanf("%*d%d%*d%*d%d%*d", &firstNumber, &secondNumber);
Try this.
%*d reads the value, but ignores it in the name of good will.
Try this
int num,number1,number2;
for(int i = 0; i < 6; i++)
{
scanf("%d", &num);
if(i == 1)
number1 = num;
if(i == 4)
number2 = num;
}
I would read every number using scanf. Read not required ones in some temp variable and others in array or any variable (as required)
scanf("%*d %d %*d %*d %d %*d", &i, &j)
int v[6];
scanf( "%d %d %d %d %d %d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5] );
v[1] and v[4] contain the answers from your example.

Resources