C Programming Code Examples
C > For Loops and While Loops Code Examples
C program to find product of digits of a number
/* C program to find product of digits of a number
Write a C program to input a number from user and calculate product of its digits. How to find product of digits of a number using loop in C programming.
I have divided the logic to calculate product of digits in three steps.
Extract last digit of the given number.
Multiply the extracted last digit with product.
Remove the last digit by dividing number by 10.
Input a number from user. Store it in some variable say j.
Initialize another variable to store product i.e. product = 1. Now, you may think why I have initialized product with 1 why not 0? This is because we are performing multiplication operation not summation. Multiplying a number with 1 returns same, so as summation with 0 returns same. Hence, I have initialized product with 1.
Find last digit of number by performing modulo division by 10 i.e. lastDigit = j % 10.
Multiply last digit found above with product i.e. product = product * lastDigit.
Remove last digit by dividing the number by 10 i.e. j = j / 10.
Repeat step 3-5 till number becomes 0. Finally you will be left with product of digits in product variable. */
#include <stdio.h>
int main()
{
int n;
long long product=1ll;
/* Input number from user */
printf("Enter any number to calculate product of digit: ");
scanf("%d", &n);
/* Repeat the steps till n becomes 0 */
while(n != 0)
{
/* Get the last digit from n and multiplies to product */
product = product * (n % 10);
/* Remove the last digit from n */
n = n / 10;
}
printf("Product of digits = %lld", product);
return 0;
}
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Relational Operators are the operators used to create a relationship and compare the values of two operands. For example, there are two numbers, 5 and 15, and we can get the greatest number using the greater than operator (>) that returns 15 as the greatest or larger number to the 5. Following are the various types of relational operators in C. Equal To Operator (==) is used to compare both operands and returns 1 if both are equal or the same, and 0 represents the operands that are not equal. Not Equal To Operator (!=) is the opposite of the Equal To Operator and is represented as the (!=) operator. The Not Equal To Operator compares two operands and returns 1 if both operands are not the same; otherwise, it returns 0.
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.
Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.
The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.
The Switch Case statement is used when we have multiple options & we need to perform a different task for each option. Case doesnt always need to have order 1, 2, 3 and so on...
C Language Program which will accept string from the user. Pass this string to the function. Calculate the 'Length of String' using pointer. gets() is used to accept string with spaces. Str
Logic to print Natural Number in reverse in a given range. If you got above logic, then you can easily modify to print natural numbers in reverse in given range. We need to make two
C Program implements cocktail sort. Cocktail sort is a variation of bubble sort that is both a stable sorting algorithm & a comparison sort. The algorithm differs from a "Bubble Sort" in
C Programming Language Function call by value is the default way of calling a function. Before we discuss function call by value, lets understand the terminologies that we will...