Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > For Loops and While Loops Code Examples

C program to check whether a number is prime number or not

/* C program to check whether a number is prime number or not Write a program in C to input a number and check whether the number is prime number or not using for loop. How to check prime numbers using loop in C programming. What is Prime number? Prime numbers are the positive integers greater than 1 that is only divisible by 1 and self. For example: 2, 3, 5, 7, 11 etc... There are several efficient algorithms for prime test. For this post I am implementing the simplest and easiest algorithm for beginners. If the number is divisible by any number in between 2 to n-1. Then it is composite number otherwise prime. Input a number from user. Store it in some variable say j. Declare and initialize another variable say isPrime = 1. isPrime variable is used as a notification or flag variable. Assigning 0 means number is composite and 1 means prime. Run a loop from 2 to j/2, increment 1 in each iteration. The loop structure should be like for(i=2; i<=j/2; i++). Check, divisibility of the number i.e. if(j%i == 0) then, the number is not prime. Set isPrime = 0 indicating number is not prime and terminate from loop. Outside the loop check the current value of isPrime. According to our assumption if it is equal to 1 then the number is prime otherwise composite. */ #include <stdio.h> int main() { int i, j, isPrime; /* * isPrime is used as flag variable. * If isPrime = 0, then number is composite * else if isPrime = 1, then number is prime. * Initially I have assumed the number as prime. */ isPrime = 1; /* Input a number from user */ printf("Enter any number to check prime: "); scanf("%d", &j); for(i=2; i<=j/2; i++) { /* Check divisibility of j */ if(j%i==0) { /* Set isPrime to 0 indicating it as composite number */ isPrime = 0; /* Terminate from loop */ break; } } /* * If isPrime contains 1 then it is prime */ if(isPrime == 1) { printf("%d is prime number", j); } else { printf("%d is composite number", j); } return 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.

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 break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.

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.

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.

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.

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,


Program code count number of words, digits, vowels using pointers. Program code Finding number of words, blank spaces, digits, special symbols, vowels using pointers. Counting the



Program to check whether a number can be expressed as sum of two prime numbers. To accomplish this task, "checkPrime()" function is created. The "checkPrime()" returns 1 if the


C program code input any number from user and find total number of leading zeros of the given number. How to find total leading zeros of a given number (in binary representation)



C program to read elements in a matrix and find determinant of the given matrix. Find determinant of a 2x2 matrix and 3x3 matrix. Logic to find determinant of a matrix in C.