In: Computer Science
Write a program to receive a string from the user and compares the first and second half of the word and prints “First and Second Half Same” or “First and Second Half Different”. If the length of the word is odd, ignore the middle letter.
For example:
Run 1:
Enter a word: abcdabc
First and Second Half Same
Run 2:
Enter a word: abcde
First and Second Half Different
Hint: use the Math.floor() and length() to get the halves of the string and use the equals() to perform the similarity check inside if-else statement.
**********************************************************************************
Write a program that reads three different integers from the users and prints “all the same” if they are all equal, “all different”, if three numbers are different and “neither” for all other cases.
For example:
Enter first number: 3
Enter first number: 2
Enter first number: 6
All Different.
**********************************************************************************
Write a program that reads three different floating point numbers from the users and prints the largest of those three:
For example:
First number: 2.36
Second number: 2.99
Third number: 2.78
Largest: 2.99
1->
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cout<<"Enter a word: ";
cin>>s;
if(s.length()%2==0) //checking that string length is even or
not
{
int k=s.length()/2;
string left,right;
left=s.substr(0,k); //storing left substring of string from
mid
right=s.substr(k,s.length()-1); //storing right substring of string
from mid
if(left==right) //comparing substrings
{
cout<<"First and Second Half Same"<<endl;
}
else
{
cout<<"First and Second Half Different"<<endl;
}
}
else
{
int k=s.length()/2;
string left,right;
left=s.substr(0,k);//storing left substring of string from
mid
right=s.substr(k+1,s.length()-1); //storing right substring of
string from mid
if(left==right)//comparing substrings
{
cout<<"First and Second Half Same"<<endl;
}
else
{
cout<<"First and Second Half Different"<<endl;
}
}
}
2->
#include<bits/stdc++.h>
using namespace std;
int main() {
int a,b,c;
//taking input
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
cout<<"Enter third number: ";
cin>>c;
//comparing all three numbers
if(a==b && b==c)
{
cout<<"All The Same"<<endl;
}
else if(a!=b && b!=c && c!=a)
{
cout<<"All Different"<<endl;
}
else
{
cout<<"neither"<<endl;
}
}
3->
#include<bits/stdc++.h>
using namespace std;
int main() {
float a,b,c;
//taking input
cout<<"First number: ";
cin>>a;
cout<<"Second number: ";
cin>>b;
cout<<"Third number: ";
cin>>c;
//comparing all three numbers
if(a>b && a>c)
{
cout<<a<<endl;
}
else if(b>a && b>c)
{
cout<<b<<endl;
}
else
{
cout<<c<<endl;
}
}