Saturday, December 24, 2011

C Program to demonstrate the use of static variables

#include<stdio.h>
#incl.ude<conio.h>
void stat();
void main()
{
    int c;
    clrscr();
     for(c=1;c<=3;c++)
     stat();
     getch();
}

void stat()
{
  static int sv=0;
  sv=sv+1;
  printf("%d\n",sv);
}

Thursday, December 15, 2011

C Program to find Hcf and Lcm Of numbers

int hcf(int n, int m) 

 
    int temp; 
    while( m > 0) 

        temp = n; 
        n = m; 
        m = temp % m; 
    } 
    return n; 

 
int lcm(int n, int m) { 
    int product = n*m; 
    int hcf = hcf(n,m); 
    int lcm = product/hcf; 
    return lcm; 

int main() 

    int n, m; 
    scanf("%d%d",&n,&m); 
    printf("%d\n",lcm(n,m)); 
    return 0; 
}
Mr. Denis Ritchie was found dead on October 12, 2011, at the age of 70 at his home in Berkeley Heights, New Jersey, where he lived alone. First news of his death came from his former colleague, Rob Pike. The cause and exact time of death have not been disclosed. He had been in frail health for several years following treatment for prostate cancer and heart disease. His death came a week after the death of Steve Jobs, although Ritchie's death did not receive as much media coverage.Computer historian Paul E. Ceruzzi said after his death: " Ritchie was under the radar. His name was not a household name at all, but... if you had a microscope and could look in a computer, you'd see his work everywhere inside.

!!.... Hats Off to One of the GEMS of the Computer World....!!
.....May his Soul REST IN PEACE...................

Sunday, November 27, 2011

C Program for Short Instant Quiz


#include<stdio.h>
#include<conio.h>
#include<string.h>
void prn();
int questions(char []);
int m1=0;
int m=0;
void main()
{
int i;
long ht;
char name[20];
char f1[10];
char ch[50]=" STATE TRUE OR FALSE . EACH CARRIES ONE MARK ";
clrscr();
prn();
 gotoxy(5,4);
 printf(" Student Name : ");
 scanf("%s",&name);
 gotoxy(5,6);
 printf(" HALL TICKET NUMBER : ");
 scanf("%ld",&ht);
clrscr();
prn();
gotoxy(2,5);
printf("%s",ch);
gotoxy(2,6);
for(i=0;i<strlen(ch);i++)
printf("-");
printf("\n\n");
questions(f1);
clrscr();
prn();
printf("\n\n");
printf(" STUDENT NAME  : %s",name);
printf("\n HALL-TICKET NUMBER  : %ld",ht);
printf("\n TOTAL MARKS  : %d",m);
getch();
}


void prn()
{
gotoxy(25,2);
printf(" C EXAMINATION ");
gotoxy(25,3);
printf(" ----------- ");
}




int questions(char f[])
{
printf(" 1. Structure is not a data type : ");
scanf("%s",&f[0]);
if(f[0]==102 || f[0]==70)
m++;
printf("\n");


printf(" 2. C Program is a collection of functions : ");
scanf("%s",&f[1]);
if(f[1]==116 || f[1]==80)
m++;
printf("\n");


printf(" 3. In an array we can accept more than one value of same type : ");
scanf("%s",&f[2]);
if(f[2]==116 || f[2]==84)
m++;
printf("\n");


printf(" 4. String is a character array : ");
scanf("%s",&f[3]);
if(f[3]==116 || f[3]==84)
m++;
printf("\n");


printf(" 5. Any function cannot be called from any other function  : ");
scanf("%s",&f[4]);
if(f[4]==102 || f[4]==70)
m++;
printf("\n");


printf(" 6. fflush() is a macro  : ");
scanf("%s",&f[5]);
if(f[5]==102 || f[5]==70)
m++;
printf("\n");


printf(" 7. When the file is opened in 'a+' mode modification is possible  : ");
scanf("%s",&f[6]);
if(f[6]==102 || f[6]==84)
m++;
printf("\n");


printf(" 8. Break is a reverse word  : ");
scanf("%s",&f[7]);
if(f[7]==116 || f[7]==84)
m++;
printf("\n");


printf(" 9. STRCHR finds the first occurence of a given character in a string  : ");
scanf("%s",&f[8]);
if(f[8]==116 || f[8]==84)
m++;
printf("\n");


printf(" 10. exit() fn is present in iomanip.h  : ");
scanf("%s",&f[9]);
if(f[0]==102 || f[0]==70)
m++;


return m;
}

Wednesday, November 2, 2011

C Program to find The Acromatic Of a String

#include<stdio.h>
#include<conio.h>
void main()
{
    char ch[100];
    int i;
    clrscr();
    printf(" Enter any String : ");
    /* to accept multi world string gets() is used */
    gets(ch);
    i=0;
    printf("\n%c",ch[0]);
    while(ch[i]!='\0')
    {
      if(ch[i]==' ')
      {
          i++;
      printf("%c",ch[i]);
      }
      i++;
     }
     getch();
}

Tuesday, October 11, 2011

C Program to find twin prime numbers

#include<stdio.h>
#include<conio.h>
void main()
{
  int n,i,k,r,a[50],x;
  clrscr();
  printf(" Enter Range : ");
  scanf("%d",&r);
  i=1;
  x=0;
  while(i<=r)
  {
     k=0;n=1;
     while(n<=i)
     {
        if(i%n==0)
            k++;
            n++;
     }
     if(k==2)
     {
        a[x]=i;
        x++;
     }
     i++;
  }

  for(n=0;n<x;n++)
  {
      if(a[n+1]-a[n]==2)
      printf("\n %d and %d are twin prime numbers ",a[n],a[n+1]);
  }
  getch();
}

Thursday, September 15, 2011

C Program to find the prime factors of a NUmber

#include<stdio.h>
#include<conio.h>
void main()
{
    int n,k,i,j,l;
    clrscr();
    printf(" Enter any number : ");
    scanf("%d",&n);
    i=1;
    while(i<=n)
    {
      if(n%i==0)
      {
          j=i;
          k=0;
          l=1;
          while(l<=j)
          {
              if(j%l==0)
              k++;
              l++;
          }
          if(k==2)
          {
             printf(" \n %d is a prime factor of %d",l-1,n);
          }
      }
      i++;
    }
    getch();
}

Tuesday, September 13, 2011

C Program for Rain Drop of String

#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<string.h>

void main()
{
char nam[10];
int i,n,j=4,k,l=10;

clrscr();
//gotoxy(10,4);
gets(nam);
n=strlen(nam);
for(i=0;i<n;i++)
{
 gotoxy(j,l);
 for(k=0;k<10;k++)
 {printf("\b");
  printf("%c",nam[i]);
  delay(500);
  printf("\b");
  if(k!=9)
  {
    printf(" ");
  }
    gotoxy(j,l);
      l=l+2;
       }
     l=10;   j=j+4;
     } getch();
     }

Saturday, September 3, 2011

C Program to illustrate the use of ELLIPSIS operator (...)

What is Ellipsis  ?
Ans:

  Suppose we declare some prototypes:
     a) int factorial(int);
     b) int sum(int,int);

  Then the function calls should be somewhat like this :
    A)  factorial(n);
    B)  sum(a,b);

  But if we write in the following way :
       factorial(n1,n2);
       sum(a,c,b);

  Then we will definitely encounter an error.
  That's because we have already declared the number of

arguments which the  respective functions can take. So ,

we cannot put more arguments than the specified ones.


But if this happens in the functions in C, then what about

the functions "printf()" and "scanf()". We can take any

number of arguments according to our choice. So, what

do their prototypes look like  ???

THey look somewhat like this :
 
                     int printf(const int *str, ...)

Now here's where the "ELLIPSIS" comes into play. This

ellipsis assures the entry of any number of argument

inputs or output as desired by the user.


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

After this, another question arises , how can we access

the arguments in the function.  ??

This can be done by the power of pointers. Let’s take a

pointer which points to the last argument before …
and depending on the next arguments of what we expect,

we increment the pointer and increment it accordingly.

eg:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int print(const char *str,...)

/*str has the number of integers passed*/

{

          int i;

          int num_count=atoi(str);

          int *num=(int *)&str;

          for(i=1;i<=num_count;i++)

                   

 printf("%d ",*(num+i));}

int print_num(int num_count,...)

/*num_count contains the number of integers passed*/

{

          int i;

          int *num=&num_count;

          for(i=1;i<=num_count;i++)

                   

 printf("%d ",*(num+i));

}

int main()

{

          print_num(3,2,3,4);
          printf("\n");
          print("3",2,3,4);

}

Tuesday, August 30, 2011

C Program to find the biggest and smallest number in an array using pointers

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[20],i,d;
  int *ptr,*big=0;
  clrscr();
  printf(" \n Enter dimension : ");
  scanf("%d",&d);
  printf(" \n Enter %d values : ",d);
     for(i=0;i<d;i++)
     scanf("%d",&a[i]);
     ptr=a;

     for(i=0;i<d;i++)
     {
        if(*big < *ptr)
        *big=*ptr;
            if(*low<*ptr)
            *low=*ptr;
         ptr++;
     }
     printf("\n Elements of Array a : \n");
     for(i=0;i<d;i++)
     printf("%d\t",a[i]);
     printf("\n The biggest value Of ARRAy : %d",*big);
     printf("\n The lowest value of  ARRAy : %d",*low);
     getch();
}

Saturday, August 20, 2011

C Program to generate a bunch of random numbers


#include <stdlib.h>
#include <stdio.h>


const int MAX = 40;
int main()
{
    int index;


    for (index = 0; index < 20; ++index) {
       int number;


       number = rand();
       printf("%d\n", (number % (MAX-1)) +1);
    }
    return (0);
}




C Program to illustrate the use of rand() function


#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main ()
{
  /* initialize random generator */
  srand ( time(NULL) );


  /* generate random numbers */


  printf ("A number between 0 and 100: %d\n", rand() % 100);


  printf ("A number between 20 and 30: %d\n", rand() % 10 + 20);


  return 0;
}

Sunday, August 7, 2011

C Program to demonstrate use of goto statement


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double x,y;
int count;
count=1;
printf(" Enter FIVE real values in a LINE \n");
read:
 scanf("%lf",&x);
 if(x<0)
printf("Value - %d is negative \n",count);
 else
 {
 y=sqrt(x);
 printf("%lf\t %lf\n",x,y);
 }
 count=count+1;
 if(count<=5)
 goto read;
 printf("\n END of Computation ");
getch();
}

C Program to evalute the power series


#include<conio.h>
#include<stdio.h>
#define ACCURACY 0.0001
void main()
{
int n,count;
float x,term,sum;
printf(" Enter value of x : ");
scanf("%f",&x);
n=term=sum=count=1;
while(n<=100)
{
 term=term*(x/n);
 sum=sum+term;
 count=count+1;
 if(term<ACCURACY)
 n=999;
 else
 n=n+1;
}
printf(" TERMS = %d SUM = %f\n",count,sum);
getch();
}

C Program to demonstrate Liability Graph


#include<stdio.h>
#include<math.h>
#define LAMBDA 0.001
#include<conio.h>
void main()
{
double t;
float r;
int i,R;
for(i=1;i<=27;++i)
{
printf("--");


}
printf("\n");
for(t=0;t<=3000;t+=150)
{
r=exp(-LAMBDA*t);
R=(int)(50*r+0.5);
printf(" |");
for(i=1;i<=R;++i)
{
 printf("*");
}
printf("#\n");
}
for(i=1;i<3;i++)
{
printf(" |\n");
}
getch();
}

Saturday, July 23, 2011

C Program to print various STAR PATTERNS II









       

       (6)

#include <stdio.h> 
int main() 
{   
    char prnt = '*';
     int i, j, k, s, nos = -1;
     for (i = 5; i >= 1; i--)
     {  
                 for (j = 1; j <= i; j++)     
               { 
                                       printf("%2c", prnt);
               } 
                for (s = nos; s >= 1; s--) 
               {   
                     printf("  ");  
                } 
               for (k = 1; k <= i; k++)
              {   if (i == 5 && k == 5) 
                  {    continue;  
                  }   
                   printf("%2c", prnt);  
               }  
             nos = nos + 2;
             printf("\n"); 
           }
               nos = 5; 
      for (i = 2; i <= 5; i++) 
     { 
          for (j = 1; j <= i; j++) 
         {
               printf("%2c", prnt);
         }
          for (s = nos; s >= 1; s--) 
        { 
                   printf("  "); 
        }
         for (k = 1; k <= i; k++)
       {   
           if (i == 5 && k == 5) 
          {    break;  
          }
          printf("%2c", prnt); 
       }  
             nos = nos - 2;  
          printf("\n"); 
  } 
      return 0;
}















                 (7)


#include <stdio.h> 
int main() 

     char prnt = '*'; 
     int i, j, k, s, nos = -1;
     for (i = 5; i >= 1; i--) 
    { 
         for (j = 1; j <= i; j++) 
         { 
            printf("%2c", prnt);
         }
         for (s = nos; s >= 1; s--) 
         {   
            printf("  ");  
         }  
         for (k = 1; k <= i; k++) 
         {   
            if (i == 5 && k == 5) 
            {    
               continue; 
            } 
            printf("%2c", prnt);  
         } 
            nos = nos + 2; 
            printf("\n"); 
    } 
       nos = 5;
      for (i = 2; i <= 5; i++) 
      {  
         for (j = 1; j <= i; j++) 
         {
             printf("%2c", prnt);
         }
      for (s = nos; s >= 1; s--)
      {   
         printf("  "); 
      }  
      for (k = 1; k <= i; k++) 
      {  
           if (i == 5 && k == 5)
           {   
              break;  
           }   
          printf("%2c", prnt);  
      } 
          nos = nos - 2;  
          printf("\n"); 
  } 
      return 0;
}













         


                         (8)


#include <stdio.h>
int main() 

       char prnt = '*'; 
       int i, j, k, s, sp, nos = 0, nosp = -1; 
       for (i = 9; i >= 3; (i = i - 2)) 
       {  
          for (s = nos; s >= 1; s--)
          {  
             printf("  "); 
          }  
          for (j = 1; j <= i; j++) 
          {
             printf("%2c", prnt);
          }
          for (sp = nosp; sp >= 1; sp--) 
          {   
             printf("  ");  
          }  
          for (k = 1; k <= i; k++) 
          { 
            if (i == 9 && k == 1)
            {
               continue;
            }
            printf("%2c", prnt);
          }
              nos++;
              nosp = nosp + 2;
              printf("\n");
              nos = 4;
       for (i = 9; i >= 1; (i = i - 2)) 
       { 
           for (s = nos; s >= 1; s--) 
           {  
               printf("  ");  
           } 
           for (j = 1; j <= i; j++)
           {   
               printf("%2c", prnt); 
           } 
               nos++; 
               printf("\n");
       }  
return 0;
}


















   

       
              (9)


#include <stdio.h>
/* * 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;
}











                               


                                       (10)



#include <stdio.h>
/* * 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;
}





//  This post is inspired by the great response to its first part.........................    
//   I hope the viewers will like these patterns again as they did for the previous post......
//  Please let me know if any errors are encountered........


Friday, July 22, 2011

C Progarm to calculate Electric Bill



#include<stdio.h>
#include<conio.h>
void main()
{
   int cno,pmr,cmr,cu;
   float total;
   char sec;
   char cname[10];
   clrscr();
   printf(" ENTER the SECTOR     : ");
   scanf("%c",&sec);
   printf(" Enter customer name   : ");
   scanf("%s",&cname);
   printf(" Enter customer number  :     ");
   scanf("%d",cno);
   printf(" Enter the previous units  : ");
   scanf("%d",&pmr);
   printf(" Enter the current units  : ");
   scanf("%d",&cmr);
   cu=cmr-pmr;
   if(sec == 'a')
   {
      if(cu>300)
      total=cu*2.00;
if(cu>200  && cu<=300)
total=cu*1.50;
 if(cu>100 && cu<=200)
 total=cu*1.00;
   if(cu<=100)
   total=cu*0.50;
   }
   if(sec=='i')
   {
      if(cu>300)
      total=cu*4.00;
if(cu>200  && cu<=300)
total=cu*3.00;
if(cu>100 && cu<=200)
 total=cu*2.00;
   if(cu<=100)
   total=cu*1.00;
   }
    if(sec == 'a')
   {
      if(cu>300)
      total=cu*2.00;
if(cu>200  && cu<=300)
total=cu*1.50;
 if(cu>100 && cu<=200)
 total=cu*1.00;
   if(cu<=100)
   total=cu*0.50;
   }
    if(sec == 'd')
   {
      if(cu>300)
      total=cu*2.00;
if(cu>200  && cu<=300)
total=cu*1.50;
 if(cu>100 && cu<=200)
 total=cu*1.00;
   if(cu<=100)
   total=cu*0.50;
   }
   printf("\n Customer name   :   %s ",cname);
   printf("\n Customer number :   %d",cno);
   printf("\n Previous Units  :   %d",pmr);
   printf("\n Current Units   :   %d",cmr);
   printf("\n Category        :   %c",sec);
   printf("\n Consumed Units  :   %d",cu);
   printf("\n Electric Bill   :   %f",total);
   getch();
   }