Monday, August 29, 2011

c programs


/*******************************************************

 program for string conversion
*********************************************************/

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

//procedure declaration

void conversion(char *);
//*********************************************************
void main()
//*********************************************************
{
char name[10];

clrscr();

printf("enter your name:");
scanf("%s",name);

//function calling

conversion(name);

//printing after conversion

printf("after conversion the name is : %s",name);
getch();
}

// procedure declaration

void conversion( char *str )
{
// while loop

while ( *str != '\0' )
{
//if condition

if( islower ( *str ) )
{
*str = toupper ( *str );

}
else
{
*str = tolower ( *str );
}
*str++;

}
}



c programs


/*******************************************************
This program is for preparing calculator instead of using functions we have to use
procedures with the help of pointers
*********************************************************/
 #include<stdio.h>
 #include<conio.h>

 //function declaration

 void func_add ( int * , int * , int *);
 void func_sub ( int * , int * , int *);
 void func_mul ( int * , int * , int *);
 void func_mod ( int * , int * , int *);
 void func_div ( int * , int * , int *);

//variable declaration

 int num1,num2;

 //main function declaration

 void main()
 {
char ch;
int add,sub,mul,div,mod;

clrscr();

printf ("\n \n");
gotoxy ( 4 , 4 );
printf ("*********************************\n");
printf ("\t\t CALCULATOR\n");
gotoxy ( 6 , 6 );
printf ("*********************************\n");

gotoxy ( 14 , 14 );
printf (" 1 ");
printf (" 2 ");
printf (" 3 ");
printf (" + ");
printf (" - ");

gotoxy ( 14 , 16 );
printf (" 4 ");
printf (" 5 ");
printf (" 6 ");
printf (" / ");
printf (" * ");

gotoxy ( 14 , 18 );
printf (" 7 ");
printf (" 8 ");
printf (" 9 ");
printf (" 0 ");
printf (" % ");

gotoxy ( 14 , 22 );
printf("press e to exit\n");

//while condition
while(1)
{
gotoxy ( 14 , 10 );
printf (" enter operator :");
scanf ("%c",&ch);

//switch statement
switch(ch)
{
case '+':

gotoxy ( 14 , 10 );
printf("enter num1:");scanf("%d",&num1);
printf("enter num2:");scanf("%d",&num2);
func_add( &num1 , &num2 , &add );
gotoxy (16,10);
printf ("sum of two numbers:%d",add);
getch();
break;

case '-':

gotoxy ( 14 , 10 );
printf("enter num1:");scanf("%d",&num1);
printf("enter num2:");scanf("%d",&num2);
func_sub( &num1 , &num2 , &sub );
printf("sub of two numbers is : %d", sub);
getch();
break;

case '*':

gotoxy ( 14 , 10 );
printf("enter num1:");scanf("%d",&num1);
printf("enter num2:");scanf("%d",&num2);
func_mul( &num1 , &num2 , &mul );
printf("multiplication of two numbers is: %d",mul);
getch();
break;

case '/':

gotoxy ( 14 , 10 );
func_div( &num1 , &num2 , &div );
printf("division of two numbers is : %d",div);
getch();
break;

case '%':

gotoxy ( 14 , 10 );
func_mod( &num1 , &num2 , &mod );
printf("modulus of two numbers is : %d",mod);
getch();
break;

  case 'e':
exit(0);
break;
  default:
printf("invalid option");
break;
}
}

}


//procedure declaration

void func_add ( int *num1 , int *num2 , int *add )
{
*add = *num1 + *num2;
}

void func_sub ( int *num1 , int *num2 , int *sub )
{
*sub = *num1 - *num2;
}

void func_mul ( int *num1 , int *num2 , int *mul)
{
*mul = *num1 * *num2;
}

void func_div ( int *num1 , int *num2 , int *div)
{
*div = *num1 / *num2;
}

void func_mod ( int *num1 , int *num2 , int  *mod)
{
*mod = *num1 % *num2;
}