0% found this document useful (0 votes)
6 views

PROGRAMS

quick revision

Uploaded by

pameluft
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)
6 views

PROGRAMS

quick revision

Uploaded by

pameluft
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/ 5

C++ Code

1. Basic Maths Code

1) Leap Year :

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

bool isLeapYear(int num){


if(num %4 ==0 && num%100 !=0 || num%400 ==0){
return true;
}
return false;
}

int main(){
int n; cin>> n;
isLeapYear(n) ? cout<<"true\n" : cout<<"false\n";
}

2) FIBONACCI NUMBER:

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

int main(){
int num; cin>>num;
int n1=0,n2=1,n3,i,j;

for(i=1;i<=num;i++){
n1=0;
n2=1;
printf("%d\t",n2);

for(j=1;j<i;j++){
n3 = n1+n2;
printf("%d\t",n3);
n1=n2;
n2=n3;
}
printf("\n");
}

3) NUMBER OF VOWELS

#include <iostream>
using namespace std;

int main() {
string name;
cin >> name;
int count = 0;

for (int i = 0; i < name.length(); i++) {


char ch = tolower(name[i]); // Convert the character to lowercase
if (ch == 'a' || ch == 'i' || ch == 'e' || ch == 'o' || ch == 'u') {
count++;
4) Fibo nth Term :

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

int fibo(int n){


if(n <= 1) return n;
return fibo(n-1) + fibo(n-2);
}

int main(){
int num; cin>>num;
cout << fibo(num) << endl;
}

5) Fibo Series :

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

int main(){
int num; cin>>num;
int ft =0, st = 1;
int i=2;

cout << ft << " " << st << " ";

while(i<num){
int next = ft+st;
cout << next << " ";
ft = st;
st = next;

i++;
}

6) BINARY TO DECIMAL

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

int main(){
int bi_num; cin>>bi_num;
int ans =0, pow_con =0;

while(bi_num !=0){
int las_dig = bi_num%10;
bi_num/=10;

ans += las_dig * pow(2,pow_con);

pow_con++;
}

cout<< ans << endl;


}
7) DECIMAL TO BINARY

8) Amrstrong

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

bool isArmstrong(int n) {
int temp = n;
int res = 0;
int digits = log10(n) + 1; // Calculate the number of digits in the number

while (temp != 0) {
int last = temp % 10;
res += pow(last, digits); // Raise the digit to the power of the number of digits
temp /= 10;
}

if (res == n) return true;


return false;
}

int main() {
int num;
cin >> num;
isArmstrong(num) ? cout << "true\n" : cout << "false\n";
}

9) Check Whether a Number is a Palindrome

#include <iostream>
using namespace std;

bool isNumberPalindrome(int n) {
int original = n, reversed = 0;

while (n > 0) {
int digit = n % 10;
reversed = reversed * 10 + digit;
n /= 10;
}

return original == reversed;


}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;

if (isNumberPalindrome(num)) {
cout << num << " is a palindrome." << endl;
} else {
cout << num << " is not a palindrome." << endl;
}

return 0;
}
10) Check Whether a String is a Palindrome

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

bool isStringPalindrome(const string& s) {


int left = 0, right = s.length() - 1;

while (left < right) {


if (s[left] != s[right]) {
return false;
}
left++;
right--;
}

return true;
}

int main() {
string str;
cout << "Enter a string: ";
cin >> str;

if (isStringPalindrome(str)) {
cout << str << " is a palindrome." << endl;
} else {
cout << str << " is not a palindrome." << endl;
}

11) Reverse a String

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

string reverseString(string s) {
int left = 0, right = s.length() - 1;

while (left < right) {


swap(s[left], s[right]);
left++;
right--;
}

return s;
}

int main() {
string str;
cout << "Enter a string to reverse: ";
cin >> str;

string reversed = reverseString(str);


cout << "Reversed string: " << reversed << endl;
12)

You might also like