Tuesday, 14 May 2013

without main function

#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin()
{
printf(” hello “);
}

current system time

#include <stdio.h>
#include <dos.h>
int main(void)
{
struct time t;
gettime(&t);
printf(“The current time is: %2d:%02d:%02d\n”, t.ti_hour, t.ti_min, t.ti_sec);
return 0;
}

current system date

#include <dos.h>
#include <stdio.h>
int main(void)
{
struct date d;
getdate(&d);
printf(“The current year is: %d\n”, d.da_year);
printf(“The current day is: %d\n”, d.da_day);
printf(“The current month is: %d\n”, d.da_mon);
return 0;
}

random no example

#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i;
time_t t;
srand((unsigned) time(&t));
printf(“Ten random numbers from 0 to 99\n\n”);
for(i=0; i<10;i++)
printf(“%d\n”,rand()%100);
}
OR
 
 
If it necessary to generate a number between 0 and (num-1) then this program meets the ideal solution
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/* prints a random number in the range 0 to 99 */
int main(void)
{
randomize();
printf(“Random number in the 0-99 range: %d\n”, random (100));
return 0;
}

accept pass and mask it

#include<stdio.h>
#include<conio.h>
char pw[25],ch;
int i;
void main()
{
clrscr();
puts(“Enter password”);
while(1)
{
if(i<0)
i=0;
ch=getch();
if(ch==13)
break; /*13 is ASCII value of ENTER*/
if(ch==8) /*ASCII value of BACKSPACE*/
{
putch(‘\b’);
putch(NULL);
putch(‘\b’);
–i;
continue;
}
pw[i++]=ch;
ch=’*';
putch(ch);
}
pw[i]=’\0′;
printf(“\n\n%s”,pw);
getch();
}

Wednesday, 22 February 2012

search an element through for binary search

/*P5.15 Program to search an element through for binary search*/
#include <stdio.h>
#define SIZE 100
int binary_search(int arr[],int item, int low, int high);
main()
{
    int arr[SIZE],i, item, n;
    printf("Enter the number of elements : ");
    scanf("%d",&n);
    printf("Enter elements of the array(in sorted order) : \n");
    for(i=0; i<n; i++)
        scanf("%d",&arr[i]);
    printf("Enter the item to be searched : ");
    scanf("%d", &item);
   
    i = binary_search(arr,item,0,n-1);
    if(i == -1)
        printf("Not Present\n");
    else
        printf("Present at index %d\n", i);
}/*End of main()*/

int binary_search(int arr[],int item, int low, int up)
{
    int mid;
    if(up < low)
        return -1;    /*not found*/
    mid = (low+up)/2;
    if(item > arr[mid])
        return binary_search(arr,item,mid+1,up);    /*Search in right portion, tail recursive call */
    else if(item  < arr[mid])
        return binary_search(arr,item,low,mid-1);    /*Search in left portion, tail recursive call */
    else
        return mid;    /*found*/
}/*End of binary_search()*/





find the factorial of a number by tail recursive method

/*P5.14 Program to find the factorial of a number by tail recursive method*/

#include<stdio.h>
long TailRecursiveFact(int n);
long TRfact(int n, int result);
main( )
{
    int num;
    printf("Enter a number : ");
    scanf("%d", &num);
    if(num<0)
        printf("No factorial for negative number\n");
    printf("Factorial of %d is %ld\n", num, TailRecursiveFact(num) );
}

/*Tail recursive*/
long TRfact(int n, int result)
{
    if( n==0)
        return result;
    return TRfact(n-1, n*result);
}/*End of TRFact()*/

/*Helper function for tail recursive function*/
long TailRecursiveFact(int n)
{
    return TRfact(n, 1);
}/*End of TailRecursiveFact()*/