Showing posts with label 'C' Codes. Show all posts
Showing posts with label 'C' Codes. Show all posts

Sunday, 9 March 2014

'C' Code to print Pascal's Triangle

1
1  1
1  2  1
1 3  3  1
1 4  6  4  1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1

main()
{
 int n,i,j;
 printf("Enter the number of rows of pattern to be drawn: ");
 scanf("%d",&n);
 
 for(i=0;i<=n-1;i++)
 {
  for(j=1;j<=n-i-1;j++)
  {
   printf(" ");
  }
  for(j=0;j<=i;j++)
  {
   if(i==j || j==0)
   printf("1 ");
   else
      printf("%d ",f(i,j));
      
  }
  printf("\n");
 }
 
}

f(x,y)
  int x , y;
{
 int z = x - y , fx , fy , fz;
 fx=1;
 while(x!=1)
 {
  fx = fx * x;
  x--;
 }
 fy = 1;
 while(y! = 1)
 {
  fy = fy * y;
  y--;
 }
  fz = 1;
 while(z!=1)
 {
  fz = fz * z;
  z--;
 }
 
 return fx / (fy * fz);
 
} 

Tuesday, 25 February 2014

'C' Code to print "Star Triangle"

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

main()
{
int i,j,r;
printf("Enter the number of rows of stars of Star Triangle to be drawn ");
scanf("%d",&r);
for(i=1;i<=r;i++)
{
 for(j=1;j<=r-i;j++)
 {
  printf(" ");
 }
 for(j=1;j<=2*i-1;j++)
 {
  printf("*");
 }
 printf("\n");
}
}

'C' Code to swap 2 numbers without the use of a third variable




main()
{
int a,b;
printf("Enter the two numbers,namely 'a' and 'b'");
scanf("%d%d",&a,&b);
a = a + b;
b = a - b;
a = a - b;
printf("The swapped number 'a' is %d\n",a);
printf("The swapped number 'b' is %d\n",b);
}

Saturday, 15 February 2014

First 'C' Program:"Hello World"

A program to print "Hello World" on the output screen.

main()
{
  printf("Hello World");
}

Explanation:

                  Here:
                         "{"-->start of the program
                         main()-->driver of the program(always used in a program and never terminated with a semicolon";").
                         printf()-->to printf "Hello World" on the output screen
                         "}"-->end of the program.