## C Program to Print Pyramid

##### Write a C Program to print pyramid ?

Hello friends, we are back with another simple and most interesting C Program.In our last blog we saw you how to write  c program to check whether the number is palindrome or not.In this program we will make a pyramid with the help of C programming Language.This kind of problems is useful for beginners to understands the fundamentals of loops and spaces.
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.Let’s see the programming part of this program.

###### C Programming Code to print pyramid:

#include<stdio.h>
int pyramid(int row )
{
int i,j,space=4,c=0;
for ( i=1;i<=row;i++)
{
for (j=1;j<=space;j–)
{
printf(” “);
}

for( j=1;j<=i;j++)
{
printf(“%d”,j);

}
printf(“\n”);
}
return ( row );
}
int main()
{
int line;
printf(“enter the number of lines”);
scanf(“%d”,&line);
pyramid ( line );
return 0;
}

Logic behind this program:

We first take the number of rows in the pattern as input from user using scanf function.
One iteration of outer for loop will print a row of pyramid.
Inner for loop prints the initial spaces for every line and nested while loop prints (2*r – 1) space separated stars for rth row of pyramid.

## C Program to Check the Number is Palindrome or Not

##### Write a C Program to check whether the number is palindrome or not?

What is Palindrome number?
A palindrome is a number that remains the same when its digits are reversed.For example – 1551,121,32423, etc, if these digits are reversed then also we get the same result.The term palindromic is derived from palindrome, which refers to a word (such as rotor or racecar) whose spelling is unchanged when its letters are reversed.
This program reverses an integer (entered by the user) using while loop. Then, if statement is used to check whether the reversed number is equal to the original number or not.

###### C Programming code for Palindrome number or not:

#include<stdio.h>
int integer( int n )
{
int rem,rev=0,a;
a=n;
while( n!=0 )
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if ( a == rev)
{
printf(“%d is a palindrome”,a);
}
else
{
printf(“%d is a not palindrome”,a);
}
}
int main()
{
int num,a;
printf(“enter the num”);
scanf(“%d”,&num);
integer( num );
return 0;
}

Logic behind this program to check number is palindrome or not:

1. Input a number from user. Store it in some variable.
2. Find reverse of the given number. Store it in some variable say reverse.
3. Compare it with the number entered by the user.
4. If both are the same then print palindrome number else print not a palindrome number.

## C Program to Check Vowel or Consonant

##### Write a C Program to find vowel or consonant  using if-else statement?

In English, A, E, I, O, and U are the five alphabets which are known as Vowels.Except these five alphabets all the remaining alphabets are called as consonants. In this tutorial, we will show you, How to write a C Program to check Vowel or Consonant with the explanation.

C Programming Code:
#include<stdio.h>
int main()
{
char alphabet;
int lowercase,highercase;
printf(“enter the charter”);
scanf(“%c”,&alphabet);
lowercase=(alphabet==’a’||alphabet==’e’|| alphabet==’i’||alphabet==’o’||alphabet==’u’);
highercase=(alphabet==’A’||alphabet==’E’|| alphabet==’I’||alphabet==’O’||alphabet==’U’);
if(lowercase||highercase)
{
printf(“%c is vowel\n”,alphabet);
}
else
{
printf(“%c is consonant\n”,alphabet);
}
return 0;
}

Explanation:

The user will first ask to enter any character, and then we are assigning the character to the previously declared variable.
Then, we used the If Else Statement to check whether the character entered by the user is equal to, A, E, I, O, U, for both upper and lower case. And if it is TRUE, it is a Vowel otherwise, it’s a Consonant.

## C Program to Find LCM of Two Number

##### Write a C Program to find lcm of two number ?

Today, let’s learn how to find LCM of two numbers in C Programming Language. But before executing the program, we will see what is LCM.
Least common multiple (LCM) The least common multiple (LCM) of two numbers is the smallest number that they both divide evenly into. In simple language,the smallest positive number that is a multiple of two or more numbers.
for example: ###### Code of the C Program to calculate the LCM of two numbers:

#include
int main()
{
int n1,n2,i,lcm;
printf(“enter the value of n1,n2”);
scanf(“%d %d”,&n1,&n2);
for(i=2;i<=n1 && i<=n2;i++)
{
if(n1%i==0 && n2%i==0)
{
n1=n1/i;
n2=n2/i;
lcm=i;
lcm=lcm*i;
printf(“lcm=%d\n”,lcm);
}
}
printf(“n1=%d,n2%d,lcm=%d\n”,n1,n2,lcm);
return 0;
}

## C Program to Calculate Gross salary

##### Write a C Program to Find Total Salary ?

In our last blog we learn how to write a c program to check whether the number is prime or not.  In this article, we will show you, how to write a C Program to Calculate Gross Salary of an Employee with example.This program will read Basic Salary of an employee, calculate other parts of the salary on percent basic and finally print the Gross Salary of the employee.This was another simple program which has been taught to the beginners.

C Program Code:
#include<stdio.h>
int gross( int basic_salary)
{
int total_salary;
total_salary=basic_salary+.1*basic_salary+.2*basic_salary;
}
int main()
{
int basic_salary,total_salary;
printf(“enter value basic_salary”);
scanf(“%d”,&basic_salary);
total_salary=gross(basic_salary);
printf(“%d=total_salary”,total_salary);
return 0;
}

## C Program to Check the number is prime or not

##### Write a C Program to check whether the number is prime or not?

In our last program, we saw how to calculate profit and loss in c programming. In this program, we will use an if-else statement to check whether the number is prime or not.
A prime number is a positive integer which is divisible only by 1 and itself. For example: 2, 3, 5, 7, 11, 13.When a number has more than two factors then it is called a composite number.

This program takes the number  from the user and then checks whether the input number is prime or not. Then the program displays the result.

C programming code:

#include<stdio.h>
int prime(int num)
{
int i,c=0;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
c++;
}
}
if(c==2)
{
printf(“%d num is prime\n”,num);
}
else
{
printf(“%d num is not prime\n”,num);
}
return (num);
}
int main()
{
int a;
printf(“enter the value of a”);
scanf(“%d”,&a);
prime(a);
printf(“a=%d”,a);
return 0;
}

Let us know if you are stuck somewhere and need help.We will try our best to provide you a solution to your problem. You are also free to request some other problem using our comments section.

## C Program to find Profit and Loss

Write a C Program to calculate Profit and Loss?

Hi, We are back with another simple program. In this program, we will learn how to write a program to find profit and loss with the use of if-else statement to calculate profit and loss in C Language.

We all have learned about profit and loss in primary mathematics classes. So, If cost price is greater than selling price then there is a loss otherwise it is a profit.

The basic formula to calculate profit and loss
Profit = S.P – C.P (Where S.P is Selling Price and C.P is Cost Price)
Loss = C.P – S.P

Lets see the programming part:

#include<stdio.h>
int loss(int selling_price)
{
int cost,difference;
if(selling_price>cost)
{
difference=selling_price-cost;
printf(“%d is profit\n”,difference);
}
else
{
printf(“%d is loss\n”,difference);
}
}
int main()
{
int selling,price,difference;
printf(“enter the price”);
scanf(“%d”,&price);
printf(“enter the selling “);
scanf(“%d”,&selling);
loss(selling);
return 0;
}

## C Program to Find Factorial of a Number

Write a C Program to find factorial of a number?

This C Program prints the factorial of a given number.
Let’s discuss first, what is a factorial number.
In simple language, a factorial number is the product of all the number from 1 to the user-specified number.
n! = n * (n-1) * (n -2) * …….* 1
For example, Factorial of 4 is represented as
4! = 4 * 3 * 2 * 1 = 24

Here is the Programming code to find factorial of a number:

#include<stdio.h>
int fact(int num)
{
int i,a=1;
for(i=1;i<=num;i++)
{
a=a*i;
}
return(a);
}
int main()
{
int x,y;
printf(“enter the value of x”);
scanf(“%d”,&x);
y=fact(x);
printf(“y=%d”,y);
return 0;
}

Let’s try to understand the logic behind calculation part of this program.

We initialized the integer i value to 1 and also (i <= Number) condition will help the loop to terminate when the condition fails.

Let assume the user entered the integer is 3

• First Iteration
i = 1, Factorial = 1 and Number = 3, It means (i <= Number) the condition is true.

Factorial = Factorial * i;
Factorial = 1 *1 = 1
i++ means i will become 2

• Second Iteration
i = 2, Factorial = 1 and Number = 3 – It means (i <= Number) the condition is true

Factorial = Factorial * i;
Factorial = 1 *2 = 2
i++ means i will become 3

• Third Iteration
i = 3, Factorial = 2 and Number = 3 – It means (i <= Number) the condition is True

Factorial = Factorial * i;
Factorial = 2 *3 = 6
i++ means i will become 4 – It means (i <= Number) the condition is Flase. So, For loop will be Terminated.

Result:

Factorial of 3 = 6

All the best

## C Program to find the leap year using if-else statement

Write a program to find the leap year in C Language?

Before writing a C program for leap year, let’s see what is Leap year and what is the logic behind, to write a program.Leap year has 366 days in year with 29 days in February.It is a year which comes once every 4 years.

Logic:
If a year is perfectly divisible by 4, it means that is a leap year and If the year can be evenly divided by 100, it is NOT a leap year, unless the year is also evenly divisible by 400. Then it is a leap year.To understand more about the logic for leap year, read Leap year article at Wikipedia.

Here is source code of the C program to check a given year is leap year:

#include<stdio.h>
int divide(int num)
{
if(num%4==0)
{
printf(“%d is leap year”,num);
}
else
{
printf(“%d is not a leap year”,num);
}
return (num);
}
int main()
{
int a;
printf(“enter the value of a”);
scanf(“%d”,&a);
divide(a);
return 0;
}

Professional Training Institute is one of the top Embedded System training Institute in Bangalore, Our unique training practical based training approach makes every student as knowledgeable and experienced. Our student Practical hands on training in Embedded system makes student employable. And also till now are having 100% placement records in our Embedded system.