Sunday, July 24, 2011

Some Pattern's in c

Write a C program to print the following pattern:

      *
    * * *
  * * * * *
* * * * * * *
*           *
* *       * *
* * *   * * *
* * * * * * *
* * *   * * *
* *       * *
*           *
* * * * * * *
  * * * * *
    * * *
      *
Program:
#include 
/*
 * nos = Num. of spaces required in the triangle.
 * i   = Counter for the num. of charcters to print in each row
 * skip= A flag for checking whether to
 *       skip a character in a row.
 *
 */
int triangle(int nos, int i, int skip) {
 char prnt = '*';
 int s, j;
 for (s = nos; s >= 1; s--) {
  printf("  ");
 }
 for (j = 1; j <= i; j++) {
  if (skip != 0) {
   if (i == 4 && j == 1) {
    continue;
   }
  }
  printf("%2c", prnt);
 }
 return 0;
}

int main() {
 int i, nos = 4;
 for (i = 1; i <= 7; (i = i + 2)) {
  triangle(nos, i, 0);
  nos--;
  printf("\n");
 }
 nos = 5;
 for (i = 1; i <= 4; i++) {
triangle(1, i, 0); //one space needed in each case of the formation
triangle(nos, i, 1); //skip printing one star in the last row.
nos = nos - 2;
printf("\n");
}
nos = 1;
for (i = 3; i >= 1; i--) {
  triangle(1, i, 0);
  triangle(nos, i, 0);
  nos = nos + 2;
  printf("\n");
 }
 nos = 1;
 for (i = 7; i >= 1; (i = i - 2)) {
  triangle(nos, i, 0);
  nos++;
  printf("\n");
 }
 return 0;
}

Some Pattern's in C

1). Write a C program to print the following pattern:

         *
      *  *
   *  *  *
*  *  *  *

Program:
 
/* This is a simple mirror-image of a right angle triangle */

#include 
int main() {
 char prnt = '*';
 int i, j, nos = 4, s;
 for (i = 1; i <= 5; i++) {
for (s = nos; s >= 1; s--) {  // Spacing factor
   printf("  ");
  }
  for (j = 1; j <= i; j++) {
   printf("%2c", prnt);
  }
  printf("\n");
  --nos;   // Controls the spacing factor
 }
 return 0;
}


  • Write C program to print the following pattern:


  • *                   *
    * * *           * * *
    * * * * *   * * * * *
    * * * * * * * * * * *

    Program:

    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, c = 1, nos = 9;
     for (i = 1; c <= 4; i++) {
         // As we want to print the columns in odd sequence viz. 1,3,5,.etc
      if ((i % 2) != 0) {
       for (j = 1; j <= i; j++) {
     printf("%2c", prnt);
    }
    for (s = nos; s >= 1; s--) { //The spacing factor
        if (c == 4 && s == 1) {
         break;
        }
        printf("  ");
       }
       for (k = 1; k <= i; k++) {
        if (c == 4 && k == 5) {
         break;
        }
        printf("%2c", prnt);
       }
       printf("\n");
       nos = nos - 4;  // controls the spacing factor
       ++c;
      }
     }
     return 0;
    }


    Write C program to print the following pattern:


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

    Program:

    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, p, r, nos = 7;
    
     for (i = 1; i <= 5; i++) {
      for (j = 1; j <= i; j++) {
     if ((i % 2) != 0 && (j % 2) != 0) {
    printf("%3c", prnt);
    }
    else if ((i % 2) == 0 && (j % 2) == 0) {
    printf("%3c", prnt);
    }
    else {
    printf("   ");
    }
    }
    for (s = nos; s >= 1; s--) {  // for the spacing factor
       printf("   ");
      }
      for (k = 1; k <= i; k++) { //Joining seperate figures
    if (i == 5 && k == 1) {
     continue;
    }
    if ((k % 2) != 0) {
    printf("%3c", prnt);
    }
    else {
    printf("   ");
    }
    }
    printf("\n");
    nos = nos - 2;   // space control
    }  
    nos = 1; // remaining half..
    for (p = 4; p >= 1; p--)
    {
    for (r = 1; r <= p; r++)
    {
    if ((p % 2) != 0 && (r % 2) != 0)
    {
    printf("%3c", prnt);
    }
    else if ((p % 2) == 0 && (r % 2) == 0)
    {
    printf("%3c", prnt);
    }
    else
    {
    printf(" ");
    }
    }
    for (s = nos; s >= 1; s--)
    {
    printf(" ");
     for (k = 1; k <= p; k++)
    {  
    if ((k % 2) != 0)
     {
    printf("%3c", prnt);
    }
    else
     {   
     printf(" ");
    }
     nos = nos + 2; // space control
    printf("\n");
    }
    return 0;
    }

    print pattern's

    #include<stdio.h>
    int main() {
          int i = 1, j = 5;
          printf("----------\n");
          printf("a \t b \n");
          printf("----------\n");
          /* logic: while loop repeats
           * 5 times i.e. until
           * the condition i<=5 fails */
          while (i <= 5) {
                /* i and j value printed */
                printf("%d \t %d\n", i, j);
                /* i and j value incremented
                 by 1 for every iteration */
                i++;
                j--;
          }
          printf("----------");
          return 0;
    }
     
     
     
    OUTPUT:--
     
    ------
      a b
     ------
      1 5
      2 4
      3 3
      4 2
      5 1
     ------

    Tuesday, July 5, 2011

    vb script for sample

    <html>
    <body>

    <script type ="text/vbscript">
    dim a,b,x(2,2),i,j
    x(0,0)="Volvo"
    x(0,1)="BMW"
    x(0,2)="Ford"
    x(1,0)="HONDA"
    x(1,1)="Mercedz"
    x(1,2)="Bonaza"
    x(2,0)="volks"
    x(2,1)="walsfoken"
    x(2,2)="sex"

    a=inputbox("enter your name.=")
    b=prompt("enter password:=")
    msgbox a

    If b=10000 Then
     alert("you are authorized to see the name of cars")


     document.write("<h2>hi you are NITESH!! WelcomeYOU</h2>")
     document.write("<h3>welcome...!you are Authorised person..:</h3>")
    for i=0 to 2
       
        for j=0 to 2
            document.write(x(i,j) & "<br />")
        next
    next


    else
     alert("you are not authorized to see the name of cars")

     document.write("<h1>hi you are not authorized..!!</h1>")
     document.write("<h3>welcome...!you are fAKE person..:</h3>")
    End If


    </script>

    </body>

    </html>

    Monday, July 4, 2011

    Adding range checking for user input

    #include
    using namespace std;
    int main()
    {
    cout << "Temperature Conversion Program." << endl;
    cout << "Enter degrees Celsius from 0 to 100: " << flush;
    double degreesCelsius;
    cin >> degreesCelsius;
    if((degreesCelsius < 0) || (degreesCelsius > 100)){
    cout << "The number you entered is invalid." << endl;
    }else{
    double degreesFahrenheit = degreesCelsius*(9.0/5.0)+32;
    cout << degreesCelsius << " degrees Celsius = ";
    cout << degreesFahrenheit << " degrees Fahrenheit" << endl;
    }
    }

    To generate more random numbers we simply call rand() repeatedly:

    #include
    #include
    using namespace std;
    int main()
    {
    int random_integer;
    for(int index=0; index<10; index++){
    random_integer = rand();
    cout << random_integer << endl;
    }
    }