Custom Search

Sunday 11 March 2012

C/C++ Program to print the following star pattern in diamond shape using Loop

C/C++ Program to print the following output using Loop


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


Program
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k;
clrscr();
printf("Enter the number:");
scanf("%d", &n);
for(i=1; i<n; i++)
 { 
 for(j=1; j<n-i; j++)
  {
   printf ("_");
   }
    for(k=1;k<=i;k++)
    {
      printf ("*_");
     }
    printf("\n");
  }
 for(i=1, i<=n; i++) // for printing
   {
    for(m=1;m<=i; m++)
     {
       printf (" ");
      }
       for (k=n-i; k>=1;k--)
      {
       printf("* ");
       }
    printf ("\n");
   }
getch();
}
    
Click here to check the output

C/C++ Program to determine whether a given number is odd or even & print the message the number is odd or even without using else option

C/C++ Program to determine whether a given number is odd or even & print the message the number is odd or even without using else option


Program
# include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the number: ");
scanf ("%d", &a);
if(a%2==0)
{
printf("\n Number is Even");
}
if(a%2!=0)
printf("\n Number is Odd");
getch()
}


Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.


Click here to check the output

Laptops