C Programming Code Examples
C > Recursion Code Examples
C Program to Find whether a Number is Prime or Not using Recursion
/* C Program to Find whether a Number is Prime or Not using Recursion
The following C program, using recursion, finds whether the entered number is a prime number or not. A prime number is an integer that has no integral factor but itself and 1. */
#include <stdio.h>
int primeno(int, int);
int main()
{
int j, check;
printf("Enter a number: ");
scanf("%d", &j);
check = primeno(j, j / 2);
if (check == 1)
{
printf("%d is a prime number\n", j);
}
else
{
printf("%d is not a prime number\n", j);
}
return 0;
}
int primeno(int j, int x)
{
if (x == 1)
{
return 1;
}
else
{
if (j % x == 0)
{
return 0;
}
else
{
return primeno(j, x - 1);
}
}
}
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:
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.
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 if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
C program to 'Read any String' from user and Remove Last Occurrence of a given character from the string. Remove last Occurrence of a character from the string. Shift all characters
Sum of upper triangular matrix is denoted with below image. In below image we need to find sum of matrix elements inside the red triangle. C program to read elements in a...
This 2D-Drawing Program is used for drawing circles, arcs, rectangle, lines and many more. Controls for the two-dimensional drawing. Enter radius of circle, enter radius, starting...
Color at stars position. Start in upper corner. Gives a message to user. For checking that a particular no. Decides whether output is 1 or 0. Normal mux implementation. Decides...
This program takes string input from the user Stores in variable line. Initially, the 'Variables' 'vowels', 'consonants', 'digits' and 'spaces' are initialized to 0. When the "vowel character" is
C programming code to implement calender. Program example to display day of month. Program will accept year, month and date from the user and will display the day of the
Print an array in reverse order, we shall know the length of the array in advance. We can start an iteration from length value of array to zero and in each iteration we can print...
C Language Program code to copy one string to another using recursion. The Program uses recursive function and copies a string entered by user from one 'Character Array' to another