Header Ads Widget

Write a C program to receive a five-digit no and display as like 24689: 2 4 6 8 9

 Write  a C program to receive a five-digit no and display as like 24689: 2 4 6 8 9

Given a five digit number , write a  program to receive a five-digit no and display as like 24689: 2 4 6 8 9 .

Sample input

24986

Sample output

6 8 9 4 2

 Write  a C program to receive a five-digit no and display as like 24689: 2 4 6 8 9

The objective of the code is to print the digits of the number by space separated .

Algorithm:

  1. Take input a 5-digit number from the user using scanf().
  2. Initialize a loop counter variable to 0.
  3. Divide the input number by 10 and store the remainder in a variable. This will give the last digit of the number.
  4. Print the last digit, followed by two spaces.
  5. Divide the input number by 10 and store the quotient in the input number variable.
  6. Increment the loop counter variable.
  7. Repeat steps 3 to 6 until the loop counter variable is less than 5.

Code(C):-

##include<stdio.h>
int main()
{
int num, remainder, i;
printf("Enter a five-digit number: ");
scanf("%d", &num);
printf("%d: ", num);
for (i = 0; i < 5; i++)
{
remainder = num % 10;
printf(" %d ", remainder);
num = num / 10;
}
return 0;
}

Output:

Enter a five-digit number: 24986:
6 8 9 4 2

Explanation:

  1. The program starts by taking input from the user using the scanf() function and storing it in the variable num.
  2. The program then prints the input number along with a colon and two spaces using the printf() function.
  3. The program then initializes a loop counter variable to 0.
  4. The program then divides the input number by 10 and stores the remainder in the variable remainder. This gives the last digit of the number.
  5. The program then prints the last digit, followed by two spaces, using the printf() function.
  6. The program then divides the input number by 10 and stores the quotient back in the input number variable.
  7. The program then increments the loop counter variable.
  8. Steps 4 to 7 are repeated until the loop counter variable is less than 5, which means that all digits have been printed.

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-

Post a Comment

0 Comments