C Programming Code Examples
C > For Loops and While Loops Code Examples
Program to swap first and last digit of a number
/* Program to swap first and last digit of a number
Write a C program to input a number from user and swap first and last digit of the given number. How to swap first and last digits of a number in C programming. Logic to swap first and last digit of a number in C program. */
#include <stdio.h>
#include <math.h>
int main()
{
int number, swappednumber;
int firstDigit, lastDigit, digits;
/* Input a number from user */
printf("Enter any number: ");
scanf("%d", &number);
/* Find last digit */
lastDigit = number % 10;
/* Total number of digit - 1 */
digits = (int) log10(number);
/* Find first digit */
firstDigit = (int) (number / pow(10, digits));
swappednumber = lastDigit;
swappednumber *= (int) round(pow(10, digits));
swappednumber += number % ((int)round(pow(10, digits)));
swappednumber -= lastDigit;
swappednumber += firstDigit;
printf("Original number = %d", number);
printf("Number after swapping first and last digit: %d", swappednumber);
return 0;
}
Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language: Simple assignment operator. Assigns values from right side operands to left side operand. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.
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.
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,
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.
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.
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).
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.
A 'median' value is the value at the center of a sorted list. To median we need to sort the list in ascending or descending order. The List of 3, 5, 2, 7, 3 as our input list, to find out median
Declare the object is a file, declare directory, declare directory that could not be read, symbolic link, The object is NOT a symbolic link and is one for, which stat() could not be
Write a Recursive Function in C Language to calculate sum of digits of a number. Declare Recursive Function to find sum of digits of a number. First give a meaningful name to the
As we know that the one "Dimensional Array" name works as a pointer to the base element of the Array. However in the case "2D Arrays" the logic is slightly different. You can consider
C Program uses recursive function & reverses the nodes in a linked list and displays the list. Linked list is an ordered set of data elements, each containing a link to its successor. Code
Get memory in c programming language. Generate 10 random numbers. Write the entire array in one step. Free memory. Get memory again. Read the entire array in one
In this program, we are doing the same thing that we have done in the above program, but here we're using functions in order to find the Quotient & Remainder. We have created two
The variable count is initialized with value 1 and then it has been tested for the condition. If the condition returns true then statements inside the body of while loop are executed...
C Language code input a number and print it into words using for loop. Input number from user. Find total digits in n. Store reverse of n in j. Find total trailing zeros. Extract last digit