Write a C program to print the given number pattern using loop. How to print the given triangular number pattern series using for loop in C programming. Logic to print the given number pattern using C program.
Example:
Input any number: 24165
Output:
Before I discuss the logic of this program you must know how to find first digits of any number and how to count digits in a given number. Logic to print the given pattern is.
![Number pattern in C]()
Happy coding ;)
Example:
Input any number: 24165
Output:
Required knowledge
Basic C programming, LoopLogic to print the given number pattern
The given number pattern is similar to the previous number pattern. Also logic of this pattern would be very much similar to the previous pattern.Before I discuss the logic of this program you must know how to find first digits of any number and how to count digits in a given number. Logic to print the given pattern is.
- Print the value of num (where num is the number entered by user).
- Remove the first digit of the number, as it isn't needed anymore.
- Repeat steps 1-3 till the number is greater than 0.
Program to print the given number pattern
/**
* C program to print the given number pattern
*/
#include <stdio.h>
#include <math.h>
int main()
{
int num, firstDigit, digits, placeValue;
printf("Enter any number: ");
scanf("%d", &num);
while(num > 0)
{
printf("%d\n", num);
digits = (int) log10(num); //Gets total number of digits
placeValue = (int) ceil(pow(10, digits)); //Gets the place value of first digit
firstDigit = (int)(num / placeValue); //Gets the first digit
num = num - (placeValue * firstDigit); //Remove the first digit from number
}
return 0;
}
Output
Enter any number: 24165
24165
4165
165
65
5
24165
4165
165
65
5
Screenshot
Happy coding ;)