Pr.Pg Next Pg

Top C programming tutorials

 

Example: Write a program to check whether the given number is even or odd.

 Sam Madam

// Write a program to check whether the given number is even or odd.

#include<stdio.h>

int main(){

int a;

printf("Enter a number: ");

scanf("%d" ,&a);

if (a % 2 == 0){

printf("The given number is EVEN\n");

}

else{

printf("The given number is ODD\n");

}

return 0;

}

Output

Enter a number: 10

The given number is EVEN

 

Enter a number: 13

The given number is ODD

 

 

Example: Write a program to swap two numbers using a temporary variable.

Sam Madam

//Write a program to swap two numbers using a temporary variable.

#include<stdio.h>

int main(){

int a, b, temp;

printf("Enter the value of a : ");

scanf("%d",&a);

printf("Enter the value of b : ");

scanf("%d",&b);

printf("Before swapping a=%d, b=%d \n", a, b);

/*Swapping logic */

temp = a;

a = b;

b = temp;

printf("After swapping a=%d, b=%d", a, b);

return 0;

}

Output

Enter the value of a : 10

Enter the value of b : 15

Before swapping a=10, b=15

After swapping a=15, b=10


 

Example: Write a program to swap two numbers without using a temporary variable.

Sam Madam

//Write a program to swap two numbers without using a temporary variable.

#include<stdio.h>

int main(){

int a, b;

printf("Enter values of a: ");

scanf("%d",&a);

printf("Enter values of b: ");

scanf("%d",&b);

printf("Before swapping a=%d, b=%d\n", a,b);

/*Swapping logic */

a = a + b;

b = a - b;

a = a - b;

printf("After swapping a=%d, b=%d\n", a, b);

return 0;

}

Output

Enter values of a: 10

Enter values of b: 15

Before swapping a=10, b=15

After swapping a=15, b=10

 

 

Example: Write a program to swap two numbers using bitwise operators.

Sam Madam

//Write a program to swap two numbers using bitwise operators.

#include<stdio.h>

int main(){

int i;

int k;

printf("Enter value of i: ");

scanf("%d",&i);

printf("Enter value of k: ");

scanf("%d",&k);

printf("Value of i=%d k=%d before swapping", i, k);

i = i ^ k;

k = i ^ k;

i = i ^ k;

printf("\nValue of i=%d k=%d after swapping", i, k);

return 0;

}

Output

Enter value of i: 10

Enter value of k: 15

Value of i=10 k=15 before swapping

Value of i=15 k=10 after swapping

 


 

Example: Write a program to find the greatest of three numbers.

Sam Madam

//Write a program to find the greatest of three numbers.

#include<stdio.h>

int main(){

int a, b, c;

printf("Enter a: ");

scanf("%d",&a);

printf("Enter b: ");

scanf("%d",&b);

printf("Enter c: ");

scanf("%d",&c);

if (a>b&&a>c){

printf("a is Greater than b and c");

}

else if (b>a&&b>c){

printf("b is Greater than a and c");

}

else if (c>a&&c>b){

printf("c is Greater than a and b");

}

else{

printf("all are equal or any two values are equal");

}

return 0;

}

Output

Enter a: 15

Enter b: 10

Enter c: 20

c is Greater than a and b

 

Enter a: 10

Enter b: 20

Enter c: 10

b is Greater than a and c

 

Enter a: 10

Enter b: 10

Enter c: 10

all are equal or any two values are equal

 


 

Example: Write a program to find the greatest among ten numbers.

Sam Madam

//Write a program to find the greatest among ten numbers.

#include<stdio.h>

int main(){

int a[10];

int i;

int greatest;

printf("Enter ten values\n");

//Store 10 numbers in an array

for (i = 0; i<10; i++){

printf("Enter Number : ");

scanf("%d" ,&a[i]);

}

//Assume that a[0] is greatest

greatest = a[0];

for (i = 0; i<10; i++){

if (a[i]>greatest){

greatest = a[i];

}

}

printf("Greatest of ten numbers is %d", greatest);

return 0;

}

Output

Enter ten values

Enter Number : 10

Enter Number : 12

Enter Number : 15

Enter Number : 20

Enter Number : 5

Enter Number : 25

Enter Number : 30

Enter Number : 35

Enter Number : 26

Enter Number : 27

Greatest of ten numbers is 35


 

Example: C Program to Enter a number and print it is prime no or not

Sam Madam

// C Program to Enter a number and print it is prime no or not

#include <stdio.h>

#include <math.h>

void main()

{

int no, i,n,limit;

printf("Enter an integer : ");

scanf("%d",&no);

limit = sqrt(no);

for (n=2;n<=limit;n++)

{if(no%n==0)

break;

}

if(n > limit)

printf("%d is Prime Number",no);

else

printf("%d is Not Prime Number",no);

}

Output

Enter an integer : 7

7 is Prime Number

 

Enter an integer : 10

10 is Not Prime Number

 

 

Example: Write a program to check whether the given number is a palindromic number or not.

Sam Madam

//Write a program to check whether the given number is a palindromic number or not.

#include<stdio.h>

int main(){

int n, n1, rev = 0, rem;

printf("Enter any number: ");

scanf("%d" ,&n);

n1 = n;

/* logic */

while (n>0){

rem = n % 10;

rev = rev * 10 + rem;

n = n / 10;

}

if (n1 == rev){

printf("Given number is a palindromic number");

}

else{

printf("Given number is not a palindromic number");

}

return 0;

}

Output

Enter any number: 15

Given number is not a palindromic number

 

Enter any number: 11

Given number is a palindromic number


 

Example: Write a program to check whether the given string is a palindrome.

Sam Madam

//Write a program to check whether the given string is a palindrome.

#include<stdio.h>

#include<string.h>

int main(){

char string1[20];

int i, length;

int flag = 0;

printf("Enter a string: ");

scanf("%s", string1);

length = strlen(string1);

for(i=0;i<length;i++){

if(string1 [i] != string1[length-i-1]) {

flag = 1;

break;

}

}

if (flag){

printf("%s is not a palindrome \n", string1);

}

else{

printf("%s is a palindrome \n", string1);

}

return 0;

}

Output

Enter a string: Hello

Hello is not a palindrome

 

Enter a string: mom

mom is a palindrome

 


 

Example: Write a program to generate the Fibonacci series.

Sam Madam

//Write a program to generate the Fibonacci series.

#include<stdio.h>

int main(){

//array fib stores numbers of fibonacci series

int i, fib[25];

// initialized first element to 0

fib[0] = 0;

// initialized second element to 1

fib[1] = 1;

//loop to generate ten elements

for (i = 2; i<10; i++){

//i'th element of series is equal to the sum of i-1'th element and i-2'th element.

fib[i] = fib[i - 1] + fib[i - 2];

}

printf("The fibonacci series is as follows \n");

//print all numbers in the series

for (i = 0; i<10; i++){

printf("%d \n", fib[i]);

}

return 0;

}

Output

The fibonacci series is as follows

0

1

1

2

3

5

8

13

21

34

 

 

Example: Program to print something without using semicolon (;)

Sam Madam

//Program to print something without using semicolon (;)

#include<stdio.h>

int main(){

//printf returns the length of string being printed

if (printf("Hello World\n")) //prints Hello World

{

//do nothing

}

return 0;

}

Output

Hello World

 

 

Example: Program to print a semicolon without using semicolon in the code.

Sam Madam

//Program to print a semicolon without using semicolon in the code.

#include<stdio.h>

int main(void) {

//prints the character with ascii value 59, i.e., semicolon

if (printf("% c\n", 59)) //prints semicolon

{

//do Nothing

}

return 0;

}

Output

;

 

 

Example: Program to concatenate two strings

Sam Madam

//Program to concatenate two strings

#include<stdio.h>

#include<string.h>

char *strct(char *c1, char *c2);

char *strct(char *c1, char *c2){

//strlen function returns length of argument string

int i = strlen(c1) ;

int k = 0;

// loops until null is encountered and appends string c2 to c1

while (c2[k] !='\0'){

c1[i + k] = c2[k];

k++;

}

return c1;

}

int main(){

char string1[15] ="first";

char string2[15] ="second";

char *finalstr;

printf("Before concatenation: \nstring1 = %s \nstring2 = %s", string1, string2);

// addresses of string1, string2 are passed to strct()

finalstr = strcat(string1, string2);

printf("\nAfter concatenation:");

//prints the contents of string whose address is in finalstr

printf("\nfinalstr = %s", finalstr);

//prints the contents of string1

printf("\nstring1 = %s", string1);

//prints the contents of string2

printf("\nstring2 = %s", string2);

return 0;

}

Output

Before concatenation:

string1 = first

string2 = second

After concatenation:

finalstr = firstsecond

string1 = firstsecond

string2 = second

 


 

Example: C Program to enter a number and print Table for it

Sam Madam

// C Program to enter a number and print Table for it

#include <stdio.h>

void main()

{

int n, no;

printf("Enter an integer for Table : ");

scanf("%d",&no);

printf("Table of %d \n",no);

for(n = 1; n<= 10; n++)

printf("%d x %d = %d \n ",no,n,no*n);

}

Output

Enter an integer for Table : 5

Table of 5

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50

Pr.Pg border                                              Next Pg