Triangle of Numbers Program in 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.

We can create various triangle pattern using C Program as follow:

 Triangle C Program 1:

/**
Write a C program to create triangle as follow
1
12
123
1234
12345
**/

#include<stdio.h>
#include<conio.h>
void main()
{
	int x,y;
	clrscr();
	for(x=1;x<=5;x++)
	{
		for(y=1;y<=x;y++)
		{
			printf("%d",y);
		}
		printf("\n");
	}
	getch();
}

Output of Program:

1
12
123
1234
12345

 Triangle C Program 2:

/**
Write a C program to create triangle as follow
1
21
321
4321
54321
**/

#include<stdio.h>
#include<conio.h>
void main()
{
	int x,y,z;
	clrscr();
	for(x=1;x<=5;x++)
	{
	for(y=x;y>=1;y--)
	{
	printf("%d",y);
	}
	printf("\n");
	}

getch();
}

 Output of Program:

1
21
321
4321
54321

 Triangle C Program 3:

/**
Write a C program to create triangle as follow
1
22
333
4444
55555
**/

#include<stdio.h>
#include<conio.h>
void main()
{
	int x,y;
	clrscr();
	for(x=1;x<=5;x++)
	{
	for(y=1;y<=x;y++)
	{
	printf("%d",x);
	}
	printf("\n");
	}
getch();
}

 Output of Program:

1
22
333
4444
55555

 Triangle C Program 4:

/**
Write a C program to create triangle as follow
12345
2345
345
45
5
**/

#include<stdio.h>
#include<conio.h>
void main()
{
	int x,y,z;
	clrscr();
	for(x=1;x<=4;x++)
	{
	for(z=1;z<=x;z++)
	{
	printf(" ");
	}
	for(y=x;y>=1;y--)
	{
	printf("*");
	}
	printf("\n");
	}
getch();
}

 Output of Program:

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

Please comment here if you have any questions

Palindrome Numbers Program in C Language

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.

Palindrome number program in C Programming

/**
Write a c program to find whether the number is palindrome or not.
**/
#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:

Enter a number to check if it is a palindrome or not
12321
12321 is a palindrome number.

Comment here is you ave any queries.

Find HCF and LCM in C Programming

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:

  1. Find HCF and LCM using Loop
  2. Find HCF and LCM using Recursion
  3. Find HCF and LCM using Function

Find HCF and LCM in C Programming using Loop

/**
Write a c program to find hcf and lcm using Loop
**/
#include <stdio.h>

int main() {
  int a, b, x, y, t, gcd, lcm;

  printf("Enter two integers\n");
  scanf("%d%d", &x, &y);

  a = x;
  b = y;

  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }

  gcd = a;
  lcm = (x*y)/gcd;

  printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
  printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

  return 0;
}

Output of C Program:

Enter two integers
9
24

Greatest common divisor of 9 and 24 = 3
Least common multiple of 9 and 24 = 72

 

Find HCF and LCM in C Programming using Recursion

/**
Write a c program to find hcf and lcm using Recursion
**/
#include <stdio.h>

long gcd(long, long);

int main() {
  long x, y, hcf, lcm;

  printf("Enter two integers\n");
  scanf("%ld%ld", &x, &y);

  hcf = gcd(x, y);
  lcm = (x*y)/hcf;

  printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
  printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);

  return 0;
}

long gcd(long a, long b) {
  if (b == 0) {
    return a;
  }
  else {
    return gcd(b, a % b);
  }
}

Find HCF and LCM in C Programming using Function

/**
Write a c program to find hcf and lcm using Function
**/
#include <stdio.h>

long gcd(long, long);

int main() {
  long x, y, hcf, lcm;

  printf("Enter two integers\n");
  scanf("%ld%ld", &x, &y);

  hcf = gcd(x, y);
  lcm = (x*y)/hcf;

  printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
  printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);

  return 0;
}

long gcd(long x, long y) {
  if (x == 0) {
    return y;
  }

  while (y != 0) {
    if (x > y) {
      x = x - y;
    }
    else {
      y = y - x;
    }
  }

  return x;
}

Please comment if this code is useful to you or having any queries.

Factorial Program in C Programming

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:

  1. Factorial Program using loop,
  2. Program using a function to find factorial and
  3. 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.

/**
Write a c program to find the factorial of number using for loop
**/
#include <stdio.h>

int main()
{
  int c, 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);

  return 0;
}

Output:

Enter a number to calculate it's factorial
6
Factorial of 6 = 720

 

Factorial program in C Programming using function

Here we find factorial using function.

/**
Write a c program to find factorial of number using Funtion
**/
#include <stdio.h>

long factorial(int);

int main()
{
  int number;
  long fact = 1;

  printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &number);

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

  return 0;
}

long factorial(int n)
{
  int c;
  long result = 1;

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

  return result;
}

 

Factorial program in C Programming using recursion

Here we find factorial using Recursion.

/**
Write a c program to find a factorial of number using Recursion
**/
#include<stdio.h>

long factorial(int);

int main()
{
  int n;
  long f;

  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);
  }

  return 0;
}

long factorial(int n)
{
  if (n == 0)
    return 1;
  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.

Please comment if this post is useful to you or have any queries.

Add, Subtract, Multiply and Division in C Programming Language

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.

/**
Write a C program to perform arithmetic Operation on two Numbers
**/
#include <stdio.h>

int main()
{
   int first, second, add, subtract, multiply;
   float divide;

   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);

   return 0;
}

Output:

Enter Two Integers
5 4
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.

Check Odd or Even Number using C Programming Language

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

#include<stdio.h>

main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( n%2 == 0 )
      printf("Even\n");
   else
      printf("Odd\n");

   return 0;
}

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 

#include<stdio.h>

main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( n & 1 == 1 )
      printf("Odd\n");
   else
      printf("Even\n");

   return 0;
}

 

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

#include<stdio.h>

main()
{
   int n;

   printf("Input an integer\n");
   scanf("%d",&n);

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

   return 0;
}

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

#include<stdio.h>

main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( (n/2)*2 == n )
      printf("Even\n");
   else
      printf("Odd\n");

   return 0;
}