C program for prime number
C program for prime number
Prime
number program in c: c program for prime number, this code prints prime
numbers using c programming language. To check whether a number is
prime or not see another code below. Prime number logic: a number is
prime if it is divisible only by one and itself. Remember two is the
only even and also the smallest prime number. First few prime numbers
are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many applications
in computer science and mathematics. A number greater than one can be
factorized into prime numbers, For example 540 = 22*33*51
Download Prime number program.
Output of program:

There are many logic to check prime numbers, one given below is more efficient then above method.
Only checking from 2 to square root of number is sufficient.
There are many more efficient logic available.
Prime number program in c language
#include<stdio.h> int main() { int n, i = 3, count, c; printf("Enter the number of prime numbers required\n"); scanf("%d",&n); if ( n >= 1 ) { printf("First %d prime numbers are :\n",n); printf("2\n"); } for ( count = 2 ; count <= n ; ) { for ( c = 2 ; c <= i - 1 ; c++ ) { if ( i%c == 0 ) break; } if ( c == i ) { printf("%d\n",i); count++; } i++; } return 0; }
Output of program:
C program for prime number or not
#include<stdio.h> main() { int n, c = 2; printf("Enter a number to check if it is prime\n"); scanf("%d",&n); for ( c = 2 ; c <= n - 1 ; c++ ) { if ( n%c == 0 ) { printf("%d is not prime.\n", n); break; } } if ( c == n ) printf("%d is prime.\n", n); return 0; }
C program for prime number using function
#include<stdio.h> int check_prime(int); main() { int n, result; printf("Enter an integer to check whether it is prime or not.\n"); scanf("%d",&n); result = check_prime(n); if ( result == 1 ) printf("%d is prime.\n", n); else printf("%d is not prime.\n", n); return 0; } int check_prime(int a) { int c; for ( c = 2 ; c <= a - 1 ; c++ ) { if ( a%c == 0 ) return 0; } if ( c == a ) return 1; }
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )Only checking from 2 to square root of number is sufficient.
There are many more efficient logic available.
Print Diamond
C program to print diamond pattern
Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as follows:
Download Diamond program.
Output of program:

* *** ***** *** *
C programming code
#include <stdio.h> int main() { int n, c, k, space = 1; printf("Enter number of rows\n"); scanf("%d", &n); space = n - 1; for (k = 1; k <= n; k++) { for (c = 1; c <= space; c++) printf(" "); space--; for (c = 1; c <= 2*k-1; c++) printf("*"); printf("\n"); } space = 1; for (k = 1; k <= n - 1; k++) { for (c = 1; c <= space; c++) printf(" "); space++; for (c = 1 ; c <= 2*(n-k)-1; c++) printf("*"); printf("\n"); } return 0; }
Output of program:
Print Pattern in C
C program to print patterns of numbers and stars
These
program prints various different patterns of numbers and stars. These
codes illustrate how to create various patterns using c programming.
Most of these c programs involve usage of nested loops and space. A
pattern of numbers, star or characters is a way of arranging these in
some logical manner or they may form a sequence. Some of these patterns
are triangles which have special importance in mathematics. Some
patterns are symmetrical while other are not. Please see the complete
page and look at comments for many different patterns.
Download Stars pyramid program.
Output of program:

For more patterns or shapes on numbers and characters see comments below and also see codes on following pages:
Floyd triangle
Pascal triangle
Consider the pattern
*
**
***
****
*****
to print above pattern see the code below:
Using these examples you are in a better position to create your
desired pattern for yourself. Creating a pattern involves how to use
nested loops properly, some pattern may involve alphabets or other
special characters. Key aspect is knowing how the characters in pattern
changes.
Pattern:
* *** ***** ******* *********We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.
C programming code
#include <stdio.h> int main() { int row, c, n, temp; printf("Enter the number of rows in pyramid of stars you wish to see "); scanf("%d",&n); temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) printf("*"); printf("\n"); } return 0; }
Output of program:
For more patterns or shapes on numbers and characters see comments below and also see codes on following pages:
Floyd triangle
Pascal triangle
Consider the pattern
*
**
***
****
*****
to print above pattern see the code below:
#include <stdio.h> int main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("*"); printf("\n"); } return 0; }
C pattern programs
Pattern:* *A* *A*A* *A*A*A*C pattern program of stars and alphabets:
#include<stdio.h> main() { int n, c, k, space, count = 1; printf("Enter number of rows\n"); scanf("%d",&n); space = n; for ( c = 1 ; c <= n ; c++) { for( k = 1 ; k < space ; k++) printf(" "); for ( k = 1 ; k <= c ; k++) { printf("*"); if ( c > 1 && count < c) { printf("A"); count++; } } printf("\n"); space--; count = 1; } return 0; }
1
232
34543
4567654
567898765
C program:#include<stdio.h> main() { int n, c, d, num = 1, space; scanf("%d",&n); space = n - 1; for ( d = 1 ; d <= n ; d++ ) { num = d; for ( c = 1 ; c <= space ; c++ ) printf(" "); space--; for ( c = 1 ; c <= d ; c++ ) { printf("%d", num); num++; } num--; num--; for ( c = 1 ; c < d ; c++) { printf("%d", num); num--; } printf("\n"); } return 0; }
Palindrome Numbers
Palindrome Numbers
Palindrome
number in c: A palindrome number is a number such that if we reverse
it, it will not change. For example some palindrome numbers examples are
121, 212, 12321, -454. To check whether a number is palindrome or not
first we reverse it and then compare the number obtained with the
original, if both are same then number is palindrome otherwise not. C
program for palindrome number is given below.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.
Download Palindrome number program.
Output of program:

Palindrome number algorithm
1. Get the number from user.2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.
Palindrome number program c
#include <stdio.h> int main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); scanf("%d",&n); temp = n; while( temp != 0 ) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10; } if ( n == reverse ) printf("%d is a palindrome number.\n", n); else printf("%d is not a palindrome number.\n", n); return 0; }
Output of program:
Reversing a Number.
C program to reverse a number
C
Program to reverse a number :- This program reverse the number entered
by the user and then prints the reversed number on the screen. For
example if user enter 123 as input then 321 is printed as output. In our
program we use modulus(%) operator to obtain the digits of a number. To
invert number look at it and write it from opposite direction or the
output of code is a number obtained by writing original number from
right to left. To reverse or invert large numbers use long data type or
long long data type if your compiler supports it, if you still have
large numbers then use strings or other data structure.
Download Reverse number program.
Output of program:

C programming code
#include <stdio.h> int main() { int n, reverse = 0; printf("Enter a number to reverse\n"); scanf("%d",&n); while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } printf("Reverse of entered number is = %d\n", reverse); return 0; }
Output of program:
Swapping two numbers
C program to swap two numbers
C program to swap two numbers with and without using third variable, swapping in c using pointers, functions (Call by reference) and using bitwise XOR
operator, swapping means interchanging. For example if in your c
program you have taken two variable a and b where a = 4 and b = 5, then
before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers.
Download Swap numbers program.
Output of program:

To understand above logic simply choose a as 7 and b as 9 and then do
what is written in program. You can choose any other combination of
numbers as well. Sometimes it's a good way to understand a program.
Swapping is used in sorting algorithms that is when we wish to
arrange numbers in a particular order either in ascending order or in
descending.
In our c program to swap numbers we will use a temp variable to swap two numbers.
Swapping of two numbers in c
#include <stdio.h> int main() { int x, y, temp; printf("Enter the value of x and y\n"); scanf("%d%d", &x, &y); printf("Before Swapping\nx = %d\ny = %d\n",x,y); temp = x; x = y; y = temp; printf("After Swapping\nx = %d\ny = %d\n",x,y); return 0; }
Output of program:
Swapping of two numbers without third variable
You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-#include <stdio.h> int main() { int a, b; printf("Enter two integers to swap\n"); scanf("%d%d", &a, &b); a = a + b; b = a - b; a = a - b; printf("a = %d\nb = %d\n",a,b); return 0; }
Swap two numbers using pointers
#include <stdio.h> int main() { int x, y, *a, *b, temp; printf("Enter the value of x and y\n"); scanf("%d%d", &x, &y); printf("Before Swapping\nx = %d\ny = %d\n", x, y); a = &x; b = &y; temp = *b; *b = *a; *a = temp; printf("After Swapping\nx = %d\ny = %d\n", x, y); return 0; }
Swapping numbers using call by reference
In this method we will make a function to swap numbers.#include <stdio.h> void swap(int*, int*); int main() { int x, y; printf("Enter the value of x and y\n"); scanf("%d%d",&x,&y); printf("Before Swapping\nx = %d\ny = %d\n", x, y); swap(&x, &y); printf("After Swapping\nx = %d\ny = %d\n", x, y); return 0; } void swap(int *a, int *b) { int temp; temp = *b; *b = *a; *a = temp; }
C programming code to swap using bitwise XOR
#include <stdio.h> int main() { int x, y; scanf("%d%d", &x, &y); printf("x = %d\ny = %d\n", x, y); x = x ^ y; y = x ^ y; x = x ^ y; printf("x = %d\ny = %d\n", x, y); return 0; }
Subscribe to:
Posts (Atom)