Tuesday, November 30, 2010

REDUCED TERMS OF GIVEN RATIONAL NO

//sample i/p: 10/20
//sample o/p: 1/2
void main()
{
int rem,num1,num2,i;
printf("enter the numerator and denominator");
scanf("%d%d",&num1,&num2);
for(i=2;i<10;i++)
{
num1/=i;
num2/=i;
}
printf("%d /%d",num1,num2);
getch();
}

GCD OF GIVEN TWO NOS

//sample i/p:15 20
//sample o/p:5
void main()
{
int rem,num1,num2;
printf("enter the two nos);
scanf("%d%d",&num1,&num2);
rem=num2%num1;
while(rem)
{
}
printf("%d",num1);
getch();
}

Monday, November 29, 2010

//string handling operations

#include
#include
#include
void read(char[]);
void copy(char[],char[]);
void compare(char[],char[]);
void substring(char[],int);
void main()
{
int choice,pos;
do
{
printf("***************************");
printf("1.copy two strings");
printf("2.compare two strings");
printf("3.finding substring");
printf("4.exit");
printf("***************************");
printf("enter ur option:");
scanf("%d",choice);
switch(choice)
{
case 1 :
read(str1);
copy(str1,str2);
break;
case 2:
read(str1);
read(str2);
compare(str1,str2);
break;
case 3:
read(str1);
printf("enter the position");
scanf("%d",&pos);
substring(strl,pos);
break;
case 4:exit();
default:printf("invalid option");
break;
}
}
while(choice<=4);
getch();
}
void read(char string[])
{
printf("enter a string");
scanf("%s",string);
}
void copy(char s1[],char s2[])
{
int i,j;
for(i=0;s1[i]!='\0';i++)
s2[i]=s1[i];
}
void compare(char s1[],char s2[])
{
int i,j;
for(i=0,j=0;s1[i]&&s2[j];i++,j++)
{
if(s1[i]!=s2[j])
break;
}
if(s1[j]=='\0' && s2[j]=='\0')
printf("both are equal");
}
void substring( char s1[],int p)
{
int i;
for(i=p;s1[i]!='\0';i++)
printf("%c",s1[i]);
}

//words in triangle form

#include
#include
void main()
{
char string[30];
int i;
printf("\n enter the string\t");
scanf("%s",string);
for(i=0;string[i]!='\0';i++)
{
for(j=0;j<=i;j++)
{
printf("\t%c",string[j]);
}
printf("\n");
}
getch();
}

//counting no of words

#include
#include
void main()
{
int i,words=0;
char text[100];
printf("enter the text and give '$' at end");
for(i=0;(text[i]=getchar())!='$';i++)
;
text[i]='\0';
for(i=0;text[i]!='\0';i++)
{
if((text[i]!=' '&& text[i+1]==' ')
(text[i]!='\t'&&text[i+1]=='\t')
(text[i]!='\n'&&text[i+1]=='\n'))
words++;
}
printf("%d words in a given string",words);
getch();
}

replace elements divisible by 5 by divisible by 10

#include
#include
void main()
{
int i;
int a[10];
clrscr();
printf("enter 10 nos");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
{
if(a[i]%5==0&&a[i]%10!=0)
a[i]=a[i]+5;
}
for(i=0;i<10;i++)
{
printf("%d",a[i]);
}
getch();
}

//matrix addition

#include
#include
void main()
{
int matrix1[5][5],matrix2[5][5],matrix[5][5];
int order;
printf("enter the order of matrix:");
scanf("%d\t,&order");
printf("enter the %d elements",order*order);
//elements for matrix1
for(i=0;i{
for(j=0;j{
scanf("%d",matrix1[i][j]);
}
}
printf("enter the %d elements",order*order);
//elements for matrix2
for(i=0;i{
for(j=0;j{
scanf("%d",matrix2[i][j]);
}
}
//addition of two matrices
for(i=0;i{
for(j=0;j{
matrix3[i][j]=matrix1[i][j]+matrix2[i][j];
}
}
//displaying the resultant
for(i=0;i{
for(j=0;j{
printf("%d",matrix3[i][j]);
}
}
getch();
}