0% found this document useful (0 votes)
49 views19 pages

C Sheet1

The document provides C++ programs for various string and array operations like finding ASCII value of a character, checking if a character is a vowel or consonant, counting vowels, finding string length without strlen, removing vowels from a string, removing spaces from a string, checking for a leap year, checking for palindrome, checking if a number is Armstrong, finding the nth Fibonacci term, calculating GCD and LCM, finding min and max of an array, finding the second smallest element, calculating array sum, and checking if a string is a palindrome.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views19 pages

C Sheet1

The document provides C++ programs for various string and array operations like finding ASCII value of a character, checking if a character is a vowel or consonant, counting vowels, finding string length without strlen, removing vowels from a string, removing spaces from a string, checking for a leap year, checking for palindrome, checking if a number is Armstrong, finding the nth Fibonacci term, calculating GCD and LCM, finding min and max of an array, finding the second smallest element, calculating array sum, and checking if a string is a palindrome.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Write a Program to Find the ASCII Value of a Character


 C++

// C++ Program to find ASCII value of a character


#include <iostream>
using namespace std;

int main()
{
char ch;

ch = 'A';

cout << "The ASCII value of " << ch << " is " << int(ch)
<< endl;

return 0;
}

2. Write a Program to Check Whether a Character is a Vowel or


Consonant
 C++

// C++ Program to print whether a character is vowel or not


#include <cctype>
#include <iostream>
using namespace std;

int main()
{
char ch = 'e';

if (isalpha(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u' || ch == 'A' || ch == 'E'
|| ch == 'I' || ch == 'O' || ch == 'U') {
cout << ch << " is a vowel." << endl;
}
else {
cout << ch << " is a consonant." << endl;
}
}
else {
cout << ch << " is not an alphabet." << endl;
}

return 0;
}

3. Write a Program to Count the Number of Vowels


 C++

// C++ Program to count the number of vowels


#include <cstring>
#include <iostream>
using namespace std;

int main()
{
string str = "GeeksforGeeks to the moon";
int vowels = 0;

for (int i = 0; str[i] != '\0'; i++) {


if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i'
|| str[i] == 'o' || str[i] == 'u'
|| str[i] == 'A' || str[i] == 'E'
|| str[i] == 'I' || str[i] == 'O'
|| str[i] == 'U') {
vowels++;
}
}

cout << "Number of vowels in the string: " << vowels


<< endl;

return 0;
}

4. Write a Program to Find the Length of the String Without using


strlen() Function
 C++

// C++ Program to find the length of a string without using


// strlen()
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
string str = "GeeksforGeeks";
int length = 0;

for (int i = 0; str[i] != '\0'; i++) {


length++;
}

cout << "The length of the string is: " << length
<< endl;

return 0;
}

5. Write a Program to Remove the Vowels from a String


 C++

// C++ Program to remove the vowels from a string


#include <cstring>
#include <iostream>
using namespace std;

int main()
{
int j = 0;

string str = "GeeksforGeeks";

for (int i = 0; str[i] != '\0'; i++) {


if (str[i] != 'a' && str[i] != 'e' && str[i] != 'i'
&& str[i] != 'o' && str[i] != 'u'
&& str[i] != 'A' && str[i] != 'E'
&& str[i] != 'I' && str[i] != 'O'
&& str[i] != 'U') {
str[j++] = str[i];
}
}
while (j < str.size()) {
str[j] = '\0';

j++;
}
cout << "String without vowels: " << str << endl;

return 0;
}

6. Write a Program to Remove Spaces From a String


 C++

// C++ Program to remove spaces from a string


#include <iostream>
#include <string>

using namespace std;

string remove_spaces(string str)


{
string result = "";
for (char c : str) {
if (c != ' ') {
result += c;
}
}
return result;
}

int main()
{
string str = "Gfg to the moon";

cout << "Without spaces: " << remove_spaces(str)


<< endl;

return 0;
}

7. Write a Program to Find a Leap Year or Not

 C++
// C++ program to check if a given
// year is leap year or not
#include <iostream>
using namespace std;

bool checkYear(int year)


{
// leap year
if (year % 400 == 0)
return true;

// Not leap year


if (year % 100 == 0)
return false;

// leap year
if (year % 4 == 0)
return true;

// Not leap year


return false;
}

int main()
{
int year = 2000;

if (checkYear(year))
cout << "Leap Year";
else
cout << "Not a Leap Year";
return 0;
}

8. Write a Program to Check Palindrome

 C++

// C++ program to check if a


// number is Palindrome or not
#include <iostream>
using namespace std;
// Function to check Palindrome
bool checkPalindrome(int n)
{
int ans = 0;
int temp = n;
while (temp != 0) {
ans = (ans * 10) + (temp % 10);
temp = temp / 10;
}

return (ans == n);


}

int main()
{
int n = 12321;

if (checkPalindrome(n) == 1) {
cout << "Yes\n";
}
else {
cout << "No\n";
}

return 0;
}

9. Write a Program to Check Whether a Number is an Armstrong


Number or Not

 C++

// C++ Program to check


// if number is Armstrong
// or not
#include <iostream>
using namespace std;

int main()
{
int n = 153;
int temp = n;
int ans = 0;
// function to calculate
// the sum of individual digits
while (n > 0) {

int rem = n % 10;


ans = (ans) + (rem * rem * rem);
n = n / 10;
}

// condition to check
if (temp == ans) {
cout << ("Yes, it is Armstrong Number");
}
else {
cout << ("No, it is not an Armstrong Number");
}
return 0;
}

10. Write a Program to Find the Nth Term of the Fibonacci Series

 C++

// C++ Program to Find the


// Nth Term of the Fibonacci Series
#include <iostream>
using namespace std;

int fib(int n)
{
int first = 0, second = 1, ans;
if (n == 0)
return first;

for (int i = 2; i <= n; i++) {


ans = first + second;
first = second;
second = ans;
}

return ans;
}
int main()
{
int n = 13;

cout << fib(n);


return 0;
}

11. Write a Program to Calculate the Greatest Common Divisor of Two


Numbers

 C++

// C++ program to find


// GCD of two numbers
#include <iostream>

using namespace std;

// Function to return gcd of a and b


int gcd(int a, int b)
{
int result = min(a, b);

while (result > 0) {


if (a % result == 0 && b % result == 0) {
break;
}
result--;
}

return result;
}

int main()
{
int a = 54, b = 33;

cout << "GCD: " << gcd(a, b);

return 0;
}
12. Write a Program to Calculate the Lowest Common Multiple (LCM)
of Two Numbers

 C++

// C++ program to
// Find LCM of two numbers
#include <iostream>
using namespace std;

long long gcd(long long int a, long long int b)


{
if (b == 0)
return a;
return gcd(b, a % b);
}

// Function to return LCM of two numbers


long long lcm(int a, int b)
{
long long result = (a / gcd(a, b)) * b;
return result;
}

int main()
{
int a = 24, b = 13;
cout << "LCM : " << lcm(a, b);
return 0;
}

13. Write a Program to Find the Smallest and Largest Element in an


Array

 C++

// C++ code to for


// Finding the minimum
// And maximum of the array
#include <iostream>
using namespace std;

// Function to find the minimum


// and maximum of the array
void findMinMax(int arr[], int n)
{
int mini = arr[0];
int maxi = arr[0];

for (int i = 0; i < n; i++) {


if (arr[i] < mini) {
mini = arr[i];
}
else if (arr[i] > maxi) {
maxi = arr[i];
}
}

cout << "Min: " << mini << endl;


cout << "Max: " << maxi << endl;
}

int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);

findMinMax(arr, N);

return 0;
}

14. Write a Program to Find the Second Smallest Element in an Array

 C++

// C++ program to find


// Second smallest elements
#include <climits>
#include <iostream>
using namespace std;

void print2Smallest(int arr[], int n)


{
int first, second;

if (n < 2) {
cout << " Invalid Input ";
return;
}

first = second = INT_MAX;


for (int i = 0; i < n; i++) {
// If current element is smaller than first
// Then update both first and second
if (arr[i] < first) {
second = first;
first = arr[i];
}

// If arr[i] is in between first and second


// Then update second
else if (arr[i] < second && arr[i] != first)
second = arr[i];
}
if (second == INT_MAX)
cout << "There is no second smallest element\n";
else
cout << " Second smallest element is " << second
<< endl;
}

int main()
{
int arr[] = { 21, 3, 15, 41, 34, 10 };
int n = sizeof(arr) / sizeof(arr[0]);

print2Smallest(arr, n);

return 0;
}

15. Write a Program to Calculate the Sum of Elements in an Array

 C++
// C++ Program to calculate
// sum of elements in an array
#include <iostream>
using namespace std;

int sum(int arr[], int n)


{
int sum = 0;

for (int i = 0; i < n; i++)


sum += arr[i];

return sum;
}

int main()
{
int arr[] = { 1, 23, 54, 12, 9 };
int n = sizeof(arr) / sizeof(arr[0]);

cout << "Sum: " << sum(arr, n);

return 0;
}

16. Write a Program to Check if the Given String is Palindrome or Not

 C++

// C++ program for checking


// if it is Palindrome or not
#include <iostream>
using namespace std;

string isPalindrome(string S)
{
for (int i = 0; i < S.length() / 2; i++) {
if (S[i] != S[S.length() - i - 1]) {
return "No";
}
}

return "Yes";
}

int main()
{
string S = "GeekeeG";

cout << isPalindrome(S);

return 0;
}

17. Write a Program to Print the Given String in Reverse Order


 C++

// C++ Program to reversea string


#include <cstring>
#include <iostream>
using namespace std;

int main()
{

int len;

string str = "GeeksforGeeks";

len = str.size();

cout << "Reverse of the string: ";


for (int i = len - 1; i >= 0; i--) {
cout << str[i];
}
cout << endl;

return 0;
}

18. Write a Program for Octal to Decimal Conversion


 C++

// C++ Program to convert ocatal to decimal


#include <cmath>
#include <iostream>

using namespace std;

int main()
{
int oct, dec = 0, place = 0;
// 67 is an octal number with binary equivalent 110000
oct = 67;

int temp = oct;


while (temp) {
int lastDigit = temp % 10;
temp /= 10;
dec += lastDigit * pow(8, place);
++place;
}

cout << "Decimal equivalent is: " << dec << endl;

return 0;
}

19. Write a Program for Hexadecimal to Decimal Conversion


 C++

// C++ Program to convert hexadecimal to decimal conversion


#include <cmath>
#include <iostream>

using namespace std;

int hexToDecimal(char hexDigit)


{
if (hexDigit >= '0' && hexDigit <= '9') {
return int(hexDigit - '0');
}
else if (hexDigit >= 'A' && hexDigit <= 'F') {
return int(hexDigit - 'A' + 10);
}
else if (hexDigit >= 'a' && hexDigit <= 'f') {
return int(hexDigit - 'a' + 10);
}
return -1;
}

int main()
{
string hex;
int decimal = 0, place = 0;

hex = "67";

int n = hex.length();
for (int i = n - 1; i >= 0; i--) {
int digit = hexToDecimal(hex[i]);
decimal += digit * pow(16, place);
place++;
}

cout << "Decimal equivalent " << decimal << endl;

return 0;
}

20. Write a Program for Decimal to Binary Conversion


 C++

// c++ program to convert decimal to binary


#include <bitset>
#include <iostream>

using namespace std;

int main()
{
int decimal = 7;

// simplest method to convert decimal to binary


bitset<32> binary(decimal);

cout << "Binary equivalent: " << binary << endl;

return 0;
}
21. Write a Program for Decimal Octal Conversion
 C++

// C++ Program to convert decimal to octal equivalent


#include <cmath>
#include <iostream>

using namespace std;

int main()
{
int decimal, octal = 0, place = 1;

decimal = 55;

int temp = decimal;


while (temp) {
int lastDigit = temp % 8;
temp /= 8;
octal += lastDigit * place;
place *= 10;
}

cout << "Octal equivalent " << octal << endl;

return 0;
}

22. Write a Program for Decimal to Hexadecimal Conversion


 C++

// C++ program to convert decimal to hexadecimal


#include <cmath>
#include <iostream>
#include <string>

using namespace std;

string decimalToHexa(int decimal)


{
string hexadecimal = "";
char hexaDecimals[16]
= { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
while (decimal > 0) {
int remainder = decimal % 16;
hexadecimal = hexaDecimals[remainder] + hexadecimal;
decimal /= 16;
}
return hexadecimal;
}

int main()
{
int decimal = 103;

cout << "Hexadecimal equivalent: "


<< decimalToHexa(decimal) << endl;

return 0;
}

23. Write a Program for Binary to Octal Conversion


 C++

// C++ implementation to convert a binary number


// to octal number
#include <bits/stdc++.h>
using namespace std;

// function to create map between binary


// number and its equivalent octal
void createMap(unordered_map<string, char>* um)
{
(*um)["000"] = '0';
(*um)["001"] = '1';
(*um)["010"] = '2';
(*um)["011"] = '3';
(*um)["100"] = '4';
(*um)["101"] = '5';
(*um)["110"] = '6';
(*um)["111"] = '7';
}

// Function to find octal equivalent of binary


string convertBinToOct(string bin)
{
int l = bin.size();
int t = bin.find_first_of('.');

// length of string before '.'


int len_left = t != -1 ? t : l;

// add min 0's in the beginning to make


// left substring length divisible by 3
for (int i = 1; i <= (3 - len_left % 3) % 3; i++)
bin = '0' + bin;

// if decimal point exists


if (t != -1) {
// length of string after '.'
int len_right = l - len_left - 1;

// add min 0's in the end to make right


// substring length divisible by 3
for (int i = 1; i <= (3 - len_right % 3) % 3; i++)
bin = bin + '0';
}

// create map between binary and its


// equivalent octal code
unordered_map<string, char> bin_oct_map;
createMap(&bin_oct_map);

int i = 0;
string octal = "";

while (1) {
// one by one extract from left, substring
// of size 3 and add its octal code
octal += bin_oct_map[bin.substr(i, 3)];
i += 3;
if (i == bin.size())
break;

// if '.' is encountered add it to result


if (bin.at(i) == '.') {
octal += '.';
i++;
}
}

// required octal number


return octal;
}

// Driver program to test above


int main()
{
string bin = "1111001010010100001.010110110011011";
cout << "Octal number = " << convertBinToOct(bin);
return 0;
}

You might also like