Creating Pyramid

Creating Pyramid using loops



I have given here a simple program withg loops to build a pyramid with *. You can replace * with any other character you like.

Source Code


#include <stdio.h>

int main()
{
    int i,j,k, n;
    printf("Enter number of rows for pyramid: ");
    scanf("%d", &n);
 
    printf("\n");
 
    for(i = 1; i <= n; i++)
    {
        for(j = 0; j < (n - i); j++)
            printf(" ");
        for(j = 1; j <= i; j++)
            printf("*");
        for(k = 1; k < i; k++)
            printf("*");
        printf("\n");
    }
    printf("\n\n");
 
    return 0;
}

Output


Enter number of rows pyramid: 21

                    *
                   ***
                  *****
                 *******
                *********
               ***********
              *************
             ***************
            *****************
           *******************
          *********************
         ***********************
        *************************
       ***************************
      *****************************
     *******************************
    *********************************
   ***********************************
  *************************************
 ***************************************
*****************************************

ELSE IF Ladder

ELSE IF ladder


Here is an example for else if ladder in C Programming Language. Else if ladder means, an simple if statement occurs with in the else part of simple if else statement.
if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }.

if statement can be combined with else part which is explained in if else statement.

Nested if statement is explained with finding the biggest of 3 numbers example.

Else if ladder is explained with an example given below:

Source Code


#include <stdio.h>

void main()
{
   int choice;

   printf("[1] Add\n");
   printf("[2] Edit\n");
   printf("[3] Delete\n");
   printf("[4] View\n");
   printf("[5] Exit\n");

   printf("Enter your choice: ");

   scanf("%d", &choice);
  
   if(choice == 1)
      printf("Add option selected");
   else if(choice == 2)
      printf("Edit option selected");
   else if(choice == 3)
      printf("Delete option selected");
   else if(choice == 4)
      printf("View option selected");
   else if(choice == 5)
      printf("Exit option selected");
   else
      printf("Invalid option selected");

}

Output


[1] Add
[2] Edit
[3] Delete
[4] View
[5] Exit
Enter your choice: 4


View option selected

Nested IF

Nested IF Statement


Here is an example for nested if statement in C Programming Language. Nested if means, an simple if statement occurs with in another if statement.
if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }.

if statement can be combined with else part which is explained in if else statement.

Nested if statement is explained with finding the biggest of 3 numbers example given below.

Source Code


#include <stdio.h>

void main()
{
   int a,b,c;
   int biggest;

   printf("Enter 1st Number: ");
   scanf("%d", &a);
   printf("Enter 2nd Number: ");
   scanf("%d", &b);
   printf("Enter 3rd Number: ");
   scanf("%d", &c);

   if(a > b)
   {
      if(a > c)
         biggest = a;
      else
         biggest = c;
   }
   else
   {
      if(b > c)
         biggest = b;
      else
         biggest = c;
   }
   printf("Biggest of 3 numbers is: %d\n", biggest);
}

Output


Enter a Number: -100
-100 is a negative number

Enter a Number: 55
55 is a positive number

IF ELSE

Simple IF ELSE Statement



Here is an example for simple if else statement.

if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }.

if statement can be combined with else part. Else part will be executed when the if condition is false.


Source Code


#include <stdio.h>


void main()
{
   int x;

   printf("Enter a Number: ");
   scanf("%d", &x);

   if(x < 0)
      printf("%d is a negative number\n", x);
   else
      printf("%d is a positive number\n", x);
}

Output


Enter a Number: -100
-100 is a negative number

Enter a Number: 55
55 is a positive number

IF Statement

Simple IF Statement



Here is an example for simple if statement.

if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }.

Source Code


#include <stdio.h>


void main()
{
   int x;

   printf("Enter a Number: ");
   scanf("%d", &x);

   if(x < 0)
      printf("%d is a negative number\n", x);
}

Output


Enter a Number: -100
-100 is a negative number

Display numbers using loops

C Programming  Loops - display numbers using for loop, while loop and do while loop



The main difference between for loop, while loop, and do while loop is
1.While loop checks for the condition first. so it may not even enter into the loop, if the condition is false.

2.do while loop, execute the statements in the loop first before checks for the condition. At least one iteration takes places, even if the condition is false.

3.for loop is similar to while loop except that •initialization statement, usually the counter variable initialization •a statement that will be executed after each and every iteration in the loop, usually counter variable increment or decrement.

The following sample code that can display numbers explains all 3 different loops:


Source Code


#include <stdio.h>

int main()
{
 
    int num = 0;
    printf("Using while loop\n");
    while(1)
    {
        num = num + 1;
printf(
"%d\n", num);
        if(num >= 5)
            break;
    }
 
    printf("Using do while loop\n");
 
    num = 0;
    do
    {
        num = num + 1;
        printf(
"%d\n", num);
}
while(num < 5);
 
    printf("Using for loop\n");
 
    for(num = 1; num <= 5; num++)
    {
printf(
"%d\n", num);   
};
 
    return 0;
   
}

Output


Using while loop
1
2
3
4
5
Using do while loop
1
2
3
4
5
Using for loop
1
2
3
4
5
Press any key to continue . . .

Fibonacci Series

Fibonacci Series



The logic for Fibonacci series very simple. It starts with 0 and 1. The next subsequent term is the sum of the previous two terms.

Source Code



// Program for Fibonacci Number

#include <stdio.h>

void main()
{
      int f1 = 0, f2 = 1, f3, n;
      printf("Program for Fibonacci Series\n");
      printf("Enter the maximum number for Fibonacci Series: ");
      scanf("%d", &n);
      printf("\nPrinting Fibonacci Series from 0 - %d\n", n);
      printf("%d\n%d\n", f1, f2);
      while(1)
      {
            f3 = f1 + f2;
            if(f3 > n)
                  break;
            printf("%d\n", f3);
            f1 = f2;
            f2 = f3;
      }
}

 

Output



Program for Fibonacci Series
Enter the maximum number for Fibonacci Series:
Printing Fibonacci Series from 0 - 1000
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987

Finding Armstrong Number

Armstrong Numbers

Program for finding Armstrong number of a three digits number.

The condition for Armstrong number is,
Sum of the cubes of its digits must equal to the number itself.

For example, 407 is given as input.
4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an Armstrong number.


Source Code



#include <stdio.h>

int main()
{
      int i, a, b, c, d;
      printf("List of Armstrong Numbers between (100 - 999):\n");

      for(i = 100; i <= 999; i++)
      {
            a = i / 100;
            b = (i - a * 100) / 10;
            c = (i - a * 100 - b * 10);

            d = a*a*a + b*b*b + c*c*c;

            if(i == d)
            {
                  printf("%d\n", i);
            }
      }  
      return 0;
}



Output



List of Armstrong Numbers between (100 - 999):
153
370
371
407

Calculating Slope of a Line

Calculating Slope of a Line Given Two End Points



Program for Calculating Slope of a Line Given Two End Points

It uses the following formula given points are (x1,y1) and (x2, y2)

slope = (y2 - y1) / (x2 - x1)

Source Code


#include <stdio.h>
#include <math.h>

void main()
{
    float slope;
    float x1, y1, x2, y2;
    float dx, dy;

    printf("Program to find the slope of a line given two end points\n");

    printf("Enter X1: ");
    scanf("%f", &x1);

    printf("Enter Y1: ");
    scanf("%f", &y1);

    printf("Enter X2: ");
    scanf("%f", &x2);

    printf("Enter Y2: ");
    scanf("%f", &y2);

    dx = x2 - x1;
    dy = y2 - y1;

    slope = dy / dx;
    printf("Slope of the line with end points (%.4f, %.4f) and (%.4f, %.4f) = %.4f", x1, y1, x2, y2, slope);
}

Output



Program to find the slope of a line given two end points
Enter X1: 2.5
Enter Y1: 7.5
Enter X2: 12.5
Enter Y2: 18
Slope of the line with end points (2.5, 7.5 and (12.5, 18) = 1.05
Press any key to continue . . .

Finding the equation

Finding the equation of a Line Given Two End Points


C program for finding the equation of a Line Given Two End Points (x1,y1) and (x2, y2)
The equation for a line is Y = MX + C
Where M = Slope of a Line and C = Intercept
To Find the slope of a line
slope = (y2 - y1) / (x2 - x1)
To Find the intercept of the line, intercept = y1 - (slope) * x1
which would be same as, intercept = y2 - (slope) * x2

Source Code


#include <stdio.h>
#include <math.h>

void main()
{
    float slope, intercept;
    float x1, y1, x2, y2;
    float dx, dy;

    printf("Program to find the equation of a line given two end points\n");

    printf("Enter X1: ");
    scanf("%f", &x1);

    printf("Enter Y1: ");
    scanf("%f", &y1);

    printf("Enter X2: ");
    scanf("%f", &x2);

    printf("Enter Y2: ");
    scanf("%f", &y2);

    dx = x2 - x1;
    dy = y2 - y1;

    slope = dy / dx;
    // y = mx + c
    // intercept c = y - mx
    intercept = y1 - slope * x1; // which is same as y2 - slope * x2

    printf("Equation of the line with end points (%.2f, %.2f) and (%.2f, %.2f) : Y = %.2fX %c %.2f\n", x1, y1, x2, y2, slope, (intercept < 0) ? ' ' : '+',  intercept);
}

Output



Program to find the equation of a line given two end points
Enter X1: 2
Enter Y1: 3
Enter X2: 5
Enter Y2: 7
Equation of the line with end points (2, 3 and (5, 7) : Y = 1.33333X +0.333333
Press any key to continue . . .

Distance between the two points

C program for the Distance between the two points.

It uses the following formula give points are (x1,y1) and (x2, y2)

SQRT( (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1) )


Source Code


#include <stdio.h>
#include <math.h>

void main()
{
    float distance;
    int x1, y1, x2, y2;
    int dx, dy;

    printf("Program for distance between the two points\n");

    printf("Enter X1: ");
    scanf("%d", &x1);

    printf("Enter Y1: ");
    scanf("%d", &y1);

    printf("Enter X2: ");
    scanf("%d", &x2);

    printf("Enter Y2: ");
    scanf("%d", &y2);

    dx = x2 - x1;
    dy = y2 - y1;

    distance = sqrt(dx*dx + dy*dy);
    printf("%.4f", distance);
}

Output



Program for distance between the two points
Enter X1: 10
Enter Y1: 10
Enter X2: 30
Enter Y2: 30
Distance between (10, 10) and (30, 30) = SQRT(800) = 28.2843

Sum of ODD Numbers

Sum of ODD Numbers in the Given Range

 

Source Code


#include <stdio.h>

void main()
{
    int index, begno, endno, sum = 0;
    printf("Program for sum of odd numbers in the given range");

    printf("Enter Beg. No.: ");
    scanf("%d", &begno);
    printf("Enter End. No.: ");
    scanf("%d", &endno);
    index = begno;
    if( (begno % 2) == 0) // If it even, then make it ODD
        index = begno + 1;
    for(; index <= endno; index += 2)
    {
        sum = sum + index;
    }
    printf("The sum of odd numbers between %d and %d is: %d", begno, endno, sum);
}

Output



Program for sum of odd numbers in the given range
Enter Beg. No.: 1
Enter End. No.: 100
The sum of odd numbers between 1 and 100 is: 2500

Sum of EVEN Numbers

Sum of EVEN Numbers in the Given Range

Source Code


#include <stdio.h>

void main()
{
    int index, begno, endno, sum = 0;
    printf("Program for sum of even numbers in the given range\n");

    printf("Enter Beg. No.: ");
    scanf("%d", &begno);
    printf("Enter End. No.: ");
    scanf("%d", &endno);
    index = begno;
    if( (begno % 2) == 1) // If it ODD, then make it EVEN
        index = begno + 1;
    for(; index <= endno; index += 2)
    {
        sum = sum + index;
    }
    printf("The sum of even numbers between %d and %d is: %d", begno, endno, sum);
}

Output



Program for sum of even numbers in the given range
Enter Beg. No.: 1
Enter End. No.: 100
The sum of even numbers between 1 and 100 is: 2550

 

Sum of ALL Numbers

Sum of ALL Numbers in the Given Range

Source Code


#include <stdio.h>

void main()
{
    int index, begno, endno, sum = 0;
    printf("Program for sum of all numbers in the given range\n");

    printf("Enter Beg. No.: ");
    scanf("%d", &begno);
    printf("Enter End. No.: ");
    scanf("%d", &endno);
    index = begno;

    for(; index <= endno; index ++)
        sum = sum + index;
   
    printf("The sum of even numbers between %d and %d is: %d", begno, endno, sum);
}

Output



Program for sum of all numbers in the given range
Enter Beg. No.: 1
Enter End. No.: 100
The sum of even numbers between 1 and 100 is: 5050

 

Program for the sum of 0 to N

Program for the sum of 0 - N using the formula n (n+1) / 2.


Source Code


#include <stdio.h>

void main()
{
    int N, sum = 0;
    printf("Program for sum of all numbers from 0 - N\n");

    printf("Enter N: ");
    scanf("%d", &N);
   
    sum = N * (N+1) / 2;
   
    printf("The sum of all numbers between 0 and %d is: %d", N, sum);
}

Output



Program for sum of all numbers from 0 - N
Enter N: 100
The sum of all numbers between 0 and 100 is: 5050

Math Functions (Using COSINE Function)

Write a program to show the use of Math Functions.

Hints: We often use standard mathematical functions such as cos,sin,exp,etc. We shall see now the use of a mathematical function in a program. The standard mathematical functions are defined and kept as a part of C math library.

#include<math.h>
#define PI       3.1416
#define MAX       180
main()
{
 int angle;
 float x,y;
 angle = 0;
 printf("        Angle        Cos(angle)\n\n");

 while (angle <= MAX)
 {
   x = (PI / MAX) * angle;
   y = cos(x);
   printf("%15d  %13.4f\n", angle,y);
   angle = angle + 10;
  }
}


Output:

        Angle        Cos(angle)
               0         1.0000
              10         0.9848
              20         0.9397
              --            ----
              --            ----
              180       -1.0000

Use of Subroutines

Write a program to show the use of subroutines

Hints: We have used only printf function that has been provided for us by the C system. The program below uses a user-defined function. A function defined by the user is equivalent to subroutine in FORTRAN or Subprogram in Basic.

main()
{
 int a,b,c;
 a=5;
 b=10;
 c=mul(a,b);
 printf("Multiplication of  %d and %d is %d", a,b,c);
 }

mul(x,y)
int p,x,y;
{
  p =x*y;
  return (p);
 }

<< Go to Index Page >>

Interest Calculation

এই প্রোগ্রামটির মাধ্যমে একজন শিক্ষার্থী ডাটা টাইপের ব্যাবহার এবং While লুপ নিয়ে কাজ করা শিখতে পারবে ।  আউটপুটকে কিভাবে ২ টি কলামে/সারিতে আলাদা করে প্রিন্ট করা যায় তাও এখানে আছে । প্রোগ্রামটি পাটিগনিতের সহজ অংকের প্রতিফলন। এখানে একজন ব্যাক্তির ৫০০০ টাকার বিনিয়োগের বিপরীতে প্রতি বছরে ১১% হারে সুদ সংযুক্ত করলে বছর শেষে তার কত টাকা সুদে-আসলে দাঁড়াবে তা নির্নয় করা হয়েছে ।  এভাবে ১ম থেকে ১০তম বছর পর্যন্ত টাকাটি বিনিয়োগ হতে থাকলে ১০ তম বছর শেষে তার মোট মূলধন কত দাড়াবে তাও এই প্রোগ্রামটির মাধ্যমে নির্নয় করা যাবে ।


Problem:  Write a program which calculates the value of money at the end of each year of investment.

Hints: Assuming an interest rate of 11 percent and prints the year and the corresponding amount, in two columns. The sample output is shown below for a period of 10 years with an initial investment of 5000.00. The program uses the following formula:

value at end of year = Value at start of year (1+ interest rate)

Source Code:
#include<stdio.h>
#define PERIOD 10
#define PRINCIPAL 5000.00
main()
{
 int year;
 float amount,value,inrate;
 amount=PRINCIPAL;
 inrate = 0.11;
 year=0;
 while(year<=PERIOD)
   {
     printf("%2d        %8.2f\n",year,amount);
     value = amount+inrate*amount;
     year = year+1;
     amount = value;
   }
}


Output:

0        5000.00
1        5550.00
2        6160.50

-
-
-
10        14197.11

<< Go to Index Page >>

Adding Two numbers using float and integer data

নিম্নের সোর্সকোডের মাধ্যমে পূর্ণসংখ্যাকে(Integer) এবং বাস্তব সংখ্যাকে(real number) কিভাবে যোগ করা যায় এবং তা প্রিন্ট(show) করা যায় তা শেখা যাবে । ভেরিয়েবল এবং ডাটা টাইপ সম্পর্কে বিস্তারিত বুঝার জন্য ভিজিট করুন:  http://www.cprogramming-bd.com/tutorial/c_tutorial_page_6.aspx

Problem: Write a program which performs addition on two numbers and display the result. Use float and Integer data type

Source Code:

#include<stdio.h>
main()
{
  int number;
  float amount;
  //integer data representation
  number=100+10;
  //float data representation
  amount =30.75 +  75.35;
  printf("%d\n", number);
  printf("%5.2f",amount);
}


Output:
110
106.10

<< Go to Index Page >>