C++ Program To
Convert Binary Number To Decimal & Vice-Versa.
#include <iostream>
#include <cmath>
using namespace std;
int binary_decimal(int n);
int decimal_binary(int n);
int main() {
int n;
char c;
cout << "Instructions: " << endl
<<endl;
cout << "1. Enter Alphabet 'd' To Convert
Binary To Decimal." << endl;
cout << "2. Enter Alphabet 'b' To Convert
Decimal to Binary." << endl << endl;
cin >> c;
if (c =='d' || c == 'D') {
cout << "\nEnter A Binary Number: ";
cin >> n;
cout << endl << n << " In Binary =
" << binary_decimal(n) << " In Decimal" <<
endl;
}
if (c =='b' || c == 'B') {
cout << "\nEnter A Decimal number: ";
cin >> n;
cout << endl << n << " In Decimal
= " << decimal_binary(n) << " In Binary" <<
endl;
}
return 0;
}
int decimal_binary(int n) {
int rem, i=1, binary=0;
while (n!=0) {
rem=n%2;
n/=2;
binary+=rem*i;
i*=10;
}
return binary;
}
int binary_decimal(int n) {
int decimal=0, i=0, rem;
while (n!=0) {
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}
C++ Program To
Find the Frequency Of Characters In A String.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char c[1000],ch;
int i, count=0;
cout << "Enter A Sentence: ";
cin.getline(c, 1000);
cout << "\nEnter A Character To Find
Frequency: ";
cin >> ch;
for(i=0;c[i]!='\0';++i) {
if(ch==c[i])
++count;
}
cout << "\nFrequency Of " << ch
<< " = " << count;
return 0;
}
C++ Program To
Copy One String Into Another String.
Method 1:
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char s1[1000], s2[1000];
int i;
cout << "Enter A Sentence: ";
cin.getline(s1, 1000);
for(i=0; s1[i]!='\0'; ++i) {
s2[i]=s1[i];
}
s2[i]='\0';
cout << "\nYou Wrote: " << s2
<< "\n";
return 0;
}
Method 2:
int main() {
char s1[1000], s2[1000];
cout<<"Enter A Sentence: ";
cin.getline(s1, 1000);
strcpy(s2, s1);
cout << "\nYou Wrote: " << s2
<< "\n";
return 0;
}