CODE
// Program to check whether the given string is palindrome or not
using library functions
#include<iostream.h>
#include<string.h>
int main()
{
char str[80],temp[80];
cout<<"Enter string to check \n";
cin>>str;
strcpy(temp,str);
strrev(temp);
if(strcmp(str,temp)==0)
cout<<"\n Given string is palindrome";
else
cout<<"\n Given string is not palindrome";
return 0;
}
using library functions
#include<iostream.h>
#include<string.h>
int main()
{
char str[80],temp[80];
cout<<"Enter string to check \n";
cin>>str;
strcpy(temp,str);
strrev(temp);
if(strcmp(str,temp)==0)
cout<<"\n Given string is palindrome";
else
cout<<"\n Given string is not palindrome";
return 0;
}
Test data 1
Enter string to check
madam
Output
Given string is palindrome
Test data 2
Enter string to check
master
Output
Given string is not palindrome
// program to check whether the string is palindrome or not
If not make it a palindrome by adding to its end
#include<iostream.h>
int main()
{
char str[80],temp[80];
int i=0,n=0,flag;
cout<<"Enter string to check \n ";
cin>>str;
while(str[i]!='\')
{
++n;
++i;
}
for(i=0;i<n/2;i++)
{
if(str[i]!=str[n-i-1])
{
flag=0;
break;
}
else flag =1;
}
if(flag==1)
cout<<"\n Given string is palindrome";
else
{
for(i=0;i<n-1;i++)
str[n+i]=str[n-i-2];
str[n+i]='\';
cout<<"\n Given string is converted to palindrome";
cout<<str;
}
return 0;
}
Test data 1
Enter string to check
madam
output
Given string is palindrome
Test data 2
Enter string to check
mouse
Output
Given string is converted to palindrome
mousesuom
// Program to find whether a given number is palindrome or not
If not make it palindrome by adding to its end
#include <iostream.h>
int main(void)
{
long n,i,j,sum=0;
cout<<"Enter any number \n";
cin>>n;
j=n;
while(j)
{
sum =sum*10+j%10;
j /=10;
}
if(sum==n)
cout<<"\n palindrome";
else
{
i=n;
n/=10;
while(n)
{
i= i*10+n%10;
n /=10;
}
cout<<"\n new palindrome"<<i;
}
return 0;
}
Test data 1
Enter any number
121
Output
palindrome
Test data 2
Enter any number
123
Output
new palindrome 12321
// Program to find the maximum sum of consecutive positive integers
#include <iostream.h>
#include<conio.h>
int main()
{
int a[50];
int i,n;
int sum=0,maxsum=0;
clrscr();
cout<<"Enter how many numbers \n";
cin>>n;
for(i=0;i<n;i++)
cin >> a[i];
for(i=0;i<n;i++)
{
if(a[i]> 0)
sum = sum + a[i];
if(sum>maxsum)
maxsum=sum;
if(a[i]<0)
sum=0;
}
cout<<maxsum;
return 0;
}
Test data
Enter how many numbers
12
-5 1 2 3 -7 4 6 -1 1 1 1 1
Output
10
// Program to sort given names
#include<iostream.h>
#include<string.h>
#include<conio.h>
int main()
{
char name[5][20],temp[20];
int i,j;
clrscr();
cout<<"Enter 5 names \n";
for(i=0;i<5;i++)
cin>>name[i];
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
cout<<"The sorted names are \n";
for(i=0;i<5;i++)
cout<<name[i]<<"\n";
return 0 ;
}
Test data
Enter 5 names
java
oracle
cplusplus
perl
cobol
Output
The sorted names are
cobol
cplusplus
java
oracle
perl
// Program to convert binary to decimal
#include<iostream.h>
#include<math.h>
int main()
{
int m,i=0,sum=0,a[16],j,x=0;
long no;
cout<<"Enter Binary number:";
cin>>no;
while(no>0)
{
m=no%10;
a[i]=m;
++i;
no=no/10;
}
for(j=0;j<i;j++)
{
sum=sum+a[j] * pow(2,x);
++x;
}
cout<<"\n Decimal number : "<<sum;
return 0 ;
}
Test data
Enter Binary number : 100011
Output
Decimal number : 35
// Example program for call by reference
#include<iostream.h>
void change(int &,int &);
int main()
{
int a,b;
cout<<"Enter values for a and b \n";
cin>>a>>b;
change(a,
cout<<"\n The values of a and b after executing the function :";
cout<<a<<" "<<b;
return 0 ;
}
void change(int & c, int & d)
{
c=c*10;
d=d+8;
cout<<"The values of a and b inside the function :"<<c<<" "<<d;
}
Test data
Enter values for and b
2 3
The values of a and b inside the function : 20 11
The values of a and b after executing the function : 20 11
// Example program for call by value
#include<iostream.h>
void change(int,int);
int main()
{
int a,b;
cout<<"Enter values for a and b \n";
cin>>a>>b;
change(a,
cout<<"\n The values of a and b after executing the function :";
cout<<a<<" "<<b;
return 0 ;
}
void change(int c, int d)
{
c=c*10;
d=d+8;
cout<<"\n The values of a and b inside the function : "<<c<<" "<<d;
}
Test data
Enter values for a and b
2 3
Output
The values of a and b inside the function : 20 11
The values of a and b after executing the function : 2 3

