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);

}