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