 # C Programming Code Examples

## C > For Loops and While Loops Code Examples

### C program to check whether a number is armstrong number or not

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/* C program to check whether a number is armstrong number or not Write a C program to input a number from user and check whether given number is Armstrong number or not. How to check Armstrong numbers in C program. What is Armstrong number? An Armstrong number is a n-digit number that is equal to the sum of the nth power of its digits. For example - 6 = 6^1 = 6 371 = 3^3 + 7^3 + 1^3 = 371 Input a number from user. Store it in some variable say j. Make a temporary copy of the value to some another variable for calculation purposes, say originalNum = j. Count total digits in the given number, store result in a variable say digits. Initialize another variable to store the sum of power of its digits ,say sum = 0. Run a loop till j > 0. The loop structure should look like while(j > 0). Inside the loop, find last digit of j. Store it in a variable say lastDigit = j % 10. Now comes the real calculation to find sum of power of digits. Perform sum = sum + pow(lastDigit, digits). Since the last digit of j is processed. Hence, remove last digit by performing j = j / 10. After loop check if(originalNum == sum), then it is Armstrong number otherwise not. C program to check Armstrong number */ #include <stdio.h> #include <math.h> int main() { int originalNum, j, lastDigit, digits, sum; /* Input number from user */ printf("Enter any number to check Armstrong number: "); scanf("%d", &j); sum = 0; /* Copy the value of j for processing */ originalNum = j; /* Find total digits in j */ digits = (int) log10(j) + 1; /* Calculate sum of power of digits */ while(j > 0) { /* Extract the last digit */ lastDigit = j % 10; /* Compute sum of power of last digit */ sum = sum + round(pow(lastDigit, digits)); /* Remove the last digit */ j = j / 10; } /* Check for Armstrong number */ if(originalNum == sum) { printf("%d is ARMSTRONG NUMBER", originalNum); } else { printf("%d is NOT ARMSTRONG NUMBER", originalNum); } return 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,

Raise to power. Returns base raised to the power exponent: baseexponent. The function pow() is used to calculate the power raised to the base value. It takes two arguments. It returns the power raised to the base value. It is declared in "math.h" header file.

Round to nearest. Returns the integral value that is nearest to x, with halfway cases rounded away from zero. Rounds a floating-point number to an integer value. The round() functions round a floating-point number to the nearest integer value, regardless of the current rounding direction setting in the floating-point environment. If the argument is exactly halfway between two integers, round() rounds it away from 0. The return value is the rounded integer value.

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.

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.

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

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.

Compute common logarithm. Returns the common (base-10) logarithm of x. Calculates the base-10 logarithm of a number. The log10() functions calculate the common logarithm of their argument. The common logarithm is the logarithm to base 10. The common logarithm of a number x is defined only for positive values of x. If x is negative, a domain error occurs; if x is zero, a range error may occur. Function returns common logarithm of x. If x is negative, it causes a domain error. If x is zero, it may cause a pole error (depending on the library implementation).

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.

#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:

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.

In C programming language we use bitwise AND & operator to check status of any bit. Bitwise AND operation evaluate each bit of resultant value as 1, if corresponding bit of...

Declare function to print all Perfect Numbers in given range. First give meaningful name to the function. Say "printPerfect()" will print all perfect numbers in given range. Along with...

Use of strtok() in C Programming Language. Skip lines shorter as. split string into tokens, return token array. Make sure, copy string to a safe place. Always keep the last entry NULL

If condition returns true then the statements inside the body of "if" are executed and the statements inside body of "else" are skipped. If condition returns false then the statements

C program input sides of a triangle and check whether a Triangle is Equilateral, Scalene or Isosceles triangle using if else. Triangle is said Equilateral Triangle, if all its sides are equal. If