Subscribe

Category: C Programming

In C Programming Language we can make many type of shape using numbers and other characters. Here I will show how we can create triangle with number. Please see the below program. Comment here for any questions.

Check whether a number is Palindrome number or not in C Programming Language: A palindrome number is a number of which reverse is same.

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.

Algorithm Palindrome number

1. Get the number from command prompt.
2. Reverse the number.
3. Compare it with the given number entered from Command prompt.
4. If both the numbers are same then print the number is palindrome
5. otherswise print number is not palindrome.

C program to find HCF (Highest Common Factor) and LCM( Least Common Multiple): Today I will write a program on how to find HCF and LCM of two integers. HCF is also known as Greatest Common Divisor(GCD) or Greatest Common Factor(GCF).

Here there are three methods to find HCF and LCM in C Programming as follow:

Factorial program in C Programming Language: C Program code to find and print factorial of a number.

Factorial can be achieved by three methods as follow:

Factorial Program using loop,

Program using a function to find factorial and

Factorial Program using recursion.

Factorial is represented using ‘!’, so five factorial will be written as (5!), n factorial as (n!). Also n! = n*(n-1)*(n-2)*(n-3)…3.2.1 and zero factorial is defined as one i.e. 0! = 1.

Factorial program in C Programming using ‘for’ loop

Here we find factorial using ‘for’ loop.

Factorial Program Using For Loop in C

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

/**

Write a c program to find the factorial of number using for loop

**/

#include <stdio.h>

intmain()

{

intc,n,fact=1;

printf("Enter a number to calculate it's factorial\n");

scanf("%d",&n);

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

fact=fact*c;

printf("Factorial of %d = %d\n",n,fact);

return0;

}

Output:

Output

1

2

3

Enteranumber tocalculate it'sfactorial

6

Factorial of6=720

Factorial program in C Programming using function

Here we find factorial using function.

Factorial Program using Function

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

/**

Write a c program to find factorial of number using Funtion

**/

#include <stdio.h>

longfactorial(int);

intmain()

{

intnumber;

longfact=1;

printf("Enter a number to calculate it's factorial\n");

scanf("%d",&number);

printf("%d! = %ld\n",number,factorial(number));

return0;

}

longfactorial(intn)

{

intc;

longresult=1;

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

result=result*c;

returnresult;

}

Factorial program in C Programming using recursion

Here we find factorial using Recursion.

Factorial program using recursion

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/**

Write a c program to find a factorial of number using Recursion

**/

#include<stdio.h>

longfactorial(int);

intmain()

{

intn;

longf;

printf("Enter an integer to find factorial\n");

scanf("%d",&n);

if(n<0)

printf("Negative integers are not allowed.\n");

else

{

f=factorial(n);

printf("%d! = %ld\n",n,f);

}

return0;

}

longfactorial(intn)

{

if(n==0)

return1;

else

return(n *factorial(n-1));

}

Recursion is a technique in which a function calls itself, for example in above code factorial function is calling itself. To solve a problem using recursion you must first express its solution in recursive form.

Write a program in C Programming to perform basic arithmetic operation like addition, subtraction, multiplication and division. C Programming Language comes with arithmetic operators. Operator we will use in the C Program are:

‘+’ Addition

‘-‘ Subtraction

‘*’ Multiplication

‘/’ Division

This operation can be performed on Integer, Float or Double Number. These are Data Types.

Arithmetic Operation in C programming Language

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

/**

Write a C program to perform arithmetic Operation on two Numbers

**/

#include <stdio.h>

intmain()

{

intfirst,second,add,subtract,multiply;

floatdivide;

printf("Enter two integers\n");

scanf("%d%d",&first,&second);

add=first+second;

subtract=first-second;

multiply=first*second;

divide=first/(float)second;//typecasting

printf("Sum = %d\n",add);

printf("Difference = %d\n",subtract);

printf("Multiplication = %d\n",multiply);

printf("Division = %.2f\n",divide);

return0;

}

Output:

MS DOS

1

2

3

4

5

6

EnterTwoIntegers

54

Sum=9

Difference=1

Multiplication=20

Division=1.25

In c language when we divide two integers we get integer result for example 5/2 evaluates to 2. As a general rule integer/integer = integer and float/integer = float or integer/float = float. So we convert denominator to float in our program, you may also write float in numerator. This explicit conversion is known as typecasting.

Today I will show How can we check a number is odd or even in C Programming Language. We also use different method to identify even and odd numbers.

As we all know if we divide any number by 2 and the remainder is 0 (zero), then that number is Even, Otherwise its odd. We will use same logic here in C Programming. In C Programming we can get remainder by % (Modulus) operator.

For example of modulus operator, 3%2 gives 1 as remainder. Even numbers are in form of (2*x) and Odd numbers are in form of ((2*x)+1). Here ‘x’ is integer number.

Program 1: Check whether number is even or odd using modulus operator

Check Even or Odd number C Programming Language

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

main()

{

intn;

printf("Enter an integer\n");

scanf("%d",&n);

if(n%2==0)

printf("Even\n");

else

printf("Odd\n");

return0;

}

We can also check the whether the number is even or odd using bitwise operator & (AND).

For example, Binary of 9 is 1001. When we perform 9 & 1, The result will be 1 and you may observe that the least significant bit of every odd number is 1, so ( odd_number & 1 ) will be always one and also ( even_number & 1 ) is zero.

Program 2: Check whether number is even or odd using Bitwise operator

Check whether number is even or odd using bitwise operator in C Programming Language

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

main()

{

intn;

printf("Enter an integer\n");

scanf("%d",&n);

if(n&1==1)

printf("Odd\n");

else

printf("Even\n");

return0;

}

Program 3: Check whether number is even or odd using Conditional Operator (Without using ‘if’ Conditional Operator)

Check whether number is even or odd using Conditional Operator - C Programming Language

C

1

2

3

4

5

6

7

8

9

10

11

12

13

#include<stdio.h>

main()

{

intn;

printf("Input an integer\n");

scanf("%d",&n);

n%2==0?printf("Even\n"):printf("Odd\n");

return0;

}

In C Programming Language when we divide two integers we get an integer result.

For example the result of 7/3 will be 2. We can take advantage of this and can use it to find whether the number is odd or even. Consider an integer ‘n’ we can first divide by 2 and then multiply it by 2 if the result is the original number then the number is even otherwise the number is odd. For example 11/2 = 5, 5*2 = 10 (which is not equal to eleven), now consider 12/2 = 6 and 6 *2 = 12 ( same as original number). These are some logic which may help you in finding if a number is odd or not.

Program 4: Check whether number is even or odd without using Modulus or Bitwise Operator

Check whether number is even or odd without using Modulus or Bitwise Operator C Programming Language