Pages

Wednesday, February 20, 2019

Special alphabet pattern as per user request


Special Alphabet Pattern

This program is written for special request received from blogger "Sree". Thanks to Sree for referring this blog.

Following C program will print special alphabet pattern series.

For n=3, expected output is

A B C B A
  B C B
    C

For n=4, expected output is

A B C D C B A
  B C D C B
    C D C 
      D

#include<stdio.h>
int main() 
{
  int i, j, k, n;
  printf("Enter your number:");
  scanf("%d", &n);
  for (i=0; i<n; i++) 
  {
     //logic for intial spaces
     for(j=0; j<i; j++) 
          printf("  ");

     //printing first half in a row(eg for n=4; ABCD)
     for(k=65+i; k<65+n-1; k++)
           printf("%c ", k);
     //printing second half in a row(eg for n=4; CBA)

     for( ; k >=65+i; k--)
           printf("%c ", k);

     printf("\n");
  }
  return 0; 
}

Output of program

Enter your number:4

A B C D C B A
  B C D C B
    C D C
      D


Read more Recommended Patterns Programs here.


Saturday, February 9, 2019

C++ Program to calculate Area of Circle

/*
Following C++ program will calculate Area of Circle for the given radius.
*/

#include <iostream>
using namespace std;

int main()
{
    float radius, area;

    cout << "Enter the radius of circle : ";
    cin >> radius;
    
    area = 3.14 * radius * radius;
    
    cout << "Area of circle with radius "
         << radius << " is " << area;
}

Output of program

Enter the radius of circle : 2
Area of circle with radius 2 is 12.56