Linear search program in c++ using recursion
#include
#include
int recursivelinearsearch(int array[],int key,int size)
{
size=size-1;
if(size <0 span="">0>
size=size-1;
if(size <0 span="">0>
{
return -1;
}
else if(array[size]==key)
return -1;
}
else if(array[size]==key)
{
return 1;
}
else
return 1;
}
else
{
return recursivelinearsearch(array,key,size);
}
}
int main()
return recursivelinearsearch(array,key,size);
}
}
int main()
{
cout<<"enter the size of array: ";
int size;
cin>>size;
int array[size], key,i;
taking input in array
for(int j=0;j
cout<<"enter the size of array: ";
int size;
cin>>size;
int array[size], key,i;
taking input in array
for(int j=0;j
{
cout<<"enter "<
cin>>array[j];
}
cout<<"enter "<
cin>>array[j];
}
your entered array is
for(int a=0;a
for(int a=0;a
{
cout<<"array[ "<
cout<
}
cout<<"enter key to search in array";
cin>>key;
int result;
result=recursivelinearsearch(array,key,size--);
if(result==1)
cout<
}
cout<<"enter key to search in array";
cin>>key;
int result;
result=recursivelinearsearch(array,key,size--);
if(result==1)
{
cout<<"key found in array ";
}
else
cout<<"key found in array ";
}
else
{
cout<<"key not found in array ";
}
return 0;
}
cout<<"key not found in array ";
}
return 0;
}
Switch statement to calculate grade points GPA
#include
#includeint main() { char grade; double gpa=0.0; cout<<"enter your grade= "; cin>>grade; switch(grade) { case'a': case'a': gpa=4.0; cout<<"your gpa is "< break; case'b': case'b': gpa=3.0; cout<<"your gpa is "< break; case'c': case'c': gpa=2.0; cout<<"your gpa is "< break;
case'd':
case'd':
gpa=1.0;
cout<<"your gpa is "<
break;
case'f':
case'f':
gpa=0.0;
cout<<"your gpa is "<
break;
default:
cout<<"invalid grade entered";
break;
case'd':
gpa=1.0;
cout<<"your gpa is "<
break;
case'f':
case'f':
gpa=0.0;
cout<<"your gpa is "<
break;
default:
cout<<"invalid grade entered";
break;
}
return 0;
}
return 0;
}
C++ program to check the entered character
is capital letter, small letter, digit or a special character
#include
#includeint main() { char character; cout<<"enter a character = "; cin>>character; int storeascii=character; cout<<"the ascii value of "< if (storeascii>=65 && storeascii<=90) { cout<<"\nyou have entered a capital letter"; } else if (storeascii>=97 && storeascii<=122) { cout<<"\nyou have entered a small letter"; } else if (storeascii>=47 && storeascii<=57) { cout<<"\nyou have entered a digit "; } else if (storeascii>=0 && storeascii>=47 storeascii>=54 && storeascii<=64 storeascii>=91 && storeascii<=96 storeascii>=123 && storeascii<=127) { cout<<"\nyou have entered a special character"; } return 0; }
No comments:
Post a Comment