1. Write a program that prints the following array’s values in descending order:
ID: 3728128 • Letter: 1
Question
1. Write a program that prints the following array’s values in descending order:
int a[]={1,5,3,7,9,0,2,6,4,8};
2. A palindrome is a number or text phrase that reads the same backwards as forwards. For example, each of the following five-letter words is a palindrome: abcba, ghihg, bbbbb. Write a program containing a method that determines whether a five-letter word is a palindrome.
char ch[] = {'a', 'b', 'c', 'b', 'a'}; // it is a palindrome
char ch2[] = {'a', 'b', 'c', 'd', 'e'}; // it is not a palindrome
Explanation / Answer
Question (1)
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
int a[]={1,5,3,7,9,0,2,6,4,8};
int size =sizeof(a)/sizeof(a[0]);
sort(a, a+size);
for(int i=size-1;i>=0;i--)
cout<<a[i]<<" ";
return 0;
}
// OUTPUT
Success #stdin #stdout 0s 4504KB
QUESTION (2) :-
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
int j=0;
char ch[] = {'a', 'b', 'c', 'b', 'a'};
int size=sizeof(ch)/sizeof(ch[0]);
char ch1[size];
for(int i=size-1;i>=0;i--)
{
ch1[j]=ch[i];
j++;
}
for(int i=0;i<size;i++)
{
if(ch[i]!=ch1[i])
{
cout<<"it is not a palindrome ! ";
exit(0);
}
}
cout<<"it is a palindrome ! ";
return 0;
}
// OUTPUT
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.