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/ 8
Table from 1 to n
int main(){ int num; cout<<"Input number upto:"; cin>>num; for(int
i=1;i<=10;i++){ for(int j=1;j<=num;j++){ cout<<j<<"x"<<i<<"="<<j*i<<" "; } cout<<endl; } } sum of the series [ 9 + 99 + 999 + 9999 ...] int main(){ int sum=0,num,val=10; cout<<"Enter the number of terms:"; cin>>num; for(int i=1;i<=num;i++){ sum+=val-1; cout<<val-1<<" "; val*=10; } cout<<"\nThe sum of the series="<<sum<<endl; } ELECTRICITY BOARD float calcBill (int &unit) { float bill=0; if(unit<=100){ bill=unit*0.6; }else if(unit<=200){ bill=60+(unit-100)*0.8; }else{ bill=140+(unit-200)*0.9; bill+=50+0.15*bill; //additional charge for more than 300 units } return bill; } void printBill(string name,float amt){ cout<<"******ELECTRICITY BOARD*******"<<endl; cout<<"User name:\t"<<name<<endl; cout<<"Bill amount:\t"<<amt<<endl; cout<<"**********THANKYOU***********"<<endl; } int main(){ int unit; float amt=0; string name; cout<<"Enter the name of user:"; getline(cin,name); cout<<"Enter the units consumed:"; cin>>unit; amt=calcBill(unit); printBill(name,amt); } sort a given array of 0s, 1s and 2s. In the final array put all 0s first, then all 1s and all 2s in last. void input(int arr[],int size){ for(int i=0;i<size;i++) cin>>arr[i]; } void bubbleSort(int arr[], int size) { for(int i=0;i<size;i++){ for(int j=0;j<size-1;j+ +){ if(arr[j]>arr[j+1]){ int temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } } int main(){ int arr1[10],arr2[10],ans[20]; int size1,size2,size3=0; cout<<"Enter the number of elements in array 1:"; cin>>size1; cout<<"Enter the number of elements in array 2:"; cin>>size2; cout<<"Enter the elements of array 1:"; input(arr1,size1); cout<<"Enter the elements of array 2:"; input(arr2,size2); for(int i=0;i<size1;i++){ ans[size3++]=arr1[i]; } for(int i=0;i<size2;i++){ ans[size3+ +]=arr2[i]; } size3--; bubbleSort(ans,size3); cout<<"new sorted array: "; for(int i=0;i<size3;i++){ cout<<ans[i]<<" "; } } tabulated report of car and city int main(){ string cities[5]={"Delhi","Mumbai","Chennai","Kolkata"}; string cars[5]={"Maruti-K10","Zen-Astelo","Wagnor","Maruti-SX4"}; int arr[10][10]={0},city,car; do{ cout<<"*****MAIN MENU*****"<<endl; cout<<"Press 1 for Delhi"<<endl; cout<<"Press 2 for Mumbai"<<endl; cout<<"Press 3 for Chennai"<<endl; cout<<"Press 4 for Kolkata"<<endl; cout<<"Press 0 to exit"<<endl; cin>>city; if(city==0) break; cout<<"Press 1 for Maruti-K10"<<endl; cout<<"Press 2 for Zen-Astelo"<<endl; cout<<"Press 3 for Wagnor"<<endl; cout<<"Press 4 for Maruti-SX4"<<endl; cin>>car; arr[city-1][car-1]++; } while(car!=0); cout<<setw(10)<<"City"; for(int i=0;i<4;i++) { cout<<setw(15)<<cars[i]; } for(int i=0;i<4;i++) { cout<<"\n"<<setw(10)<<cities[i]; for(int j=0;j<4;j++){ cout<<setw(15)<<arr[i] [j]; } } removes a specific character from a given string string remove_char(string str,char ch){ string ans=""; int indx=0; for(int i=0;i<str.length();i++) { if(str[i]!=ch) { ans+=str[i]; } } return ans; } int main(){ string str,ans; char ch; cout<<"Enter the string:"; getline(cin,str); cout<<"Enter the character to delete:"; cin>>ch; cout<<"main string:"<<str<<endl; ans=remove_char(str,ch); cout<<"Modified string:"<<ans<<endl; } reverse each word of string void revstr(string &str) { int i=0,j=str.length()-1; while(i<j) { char temp=str[i]; str[i++]=str[j]; str[j--]=temp; } } int main(){ string str; cout<<"Enter the string:"; getline(cin,str); cout<<"Entered string:"<<str<<endl; revstr(str); cout<<"Reversed string:"<<str<<endl; } maximum occurring character in a string char max_occur(string str){ int arr[256]={0}; int max=0; char ch; for(int i=0;i<str.length();i++){ int val=(char)str[i]; arr[val]++; } for(int i=0;i<256;i++){ if(arr[i]>max){ max=arr[i]; ch=(int)i; } } cout<<max<<endl; return ch; } int main(){ string str; char ch; cout<<"Enter the string:"; getline(cin,str); ch=max_occur(str); cout<<"Maximum occuring character:"<<ch<<endl; } cout no. of vowels #include <string> int main() { string str; int vowelCount = 0; cout << "Enter a string: "; getline(cin, str); for (char c : str) { c = tolower(c); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { vowelCount++; } } cout << "Number of vowels: " << vowelCount << std::endl; } String is a Palindrome #include <string> int main() { string str, revStr; cout << "Enter a string: "; getline(cin, str); revStr = str; reverse(revStr.begin(), revStr.end()); if (str == revStr) { cout << "The string is a palindrome." << endl; } else { cout << "The string is not a palindrome." << endl; } } String to Uppercase #include <string> #include <algorithm> int main() { string str; cout << "Enter a string: "; getline(cin, str); transform(str.begin(), str.end(), str.begin(), ::toupper); cout << "Uppercase string: " << str << endl; } Length of the Longest Word in a String #include <string> #include <sstream> int main() { string str, word; int maxLength = 0; cout << "Enter a sentence: "; getline(cin, str); istringstream stream(str); while (stream >> word) { if (word.length() > maxLength) { maxLength = word.length(); } } cout << "Length of the longest word: " << maxLength << endl; } bank account class Bank{ private: string dname; string accno; string type; double bal; public: Bank(string dname,string accno,string type,double bal):dname(dname),accno(accno),type(type),bal(bal){} void deposit(int value){ bal+=value; cout<<"The amount "<<value<<" has been credited to your account"<<endl; } void withdraw(int value){ if(value>bal){ cout<<"Insufficient bank balance"<<endl; }else{ bal-=value; cout<<"The amount "<<value<<" has been debited from your account"<<endl; } } void display(){ cout<<"*****Welcome to E-Bank site*****"<<endl; cout<<"Account holder:"<<dname<<endl; cout<<"Account number:"<<accno<<endl; cout<<"Account type:"<<type<<endl; cout<<"Balance:"<<bal<<endl; cout<<"***********Visit again***********"<<endl; } }; int main(){ Bank d1("Abhay kanojia","456789417","savings",300000); int choice=0; do{ cout<<"1.To Deposit"<<endl; cout<<"2.To Withdraw"<<endl; cout<<"3.To Check balance"<<endl; cout<<"0.To exit"<<endl; cout<<"Enter your choice:"; cin>>choice; switch(choice){ case 1:{ int value; cout<<"Enter the amount to deposit:"; cin>>value; d1.deposit(value); break; } case 2:{ int value; cout<<"Enter the amount to debit:"; cin>>value; d1.withdraw(value); break; } case 3: { d1.display(); break; } } }while(choice!=0); cout<<"\n ********Thankyou*********"<<endl; } represents a specific time of day using hours, minutes, and seconds #include<iomanip> class Time{ private: int hour; int min; int sec; public: Time (){ this->hour=0; this->min=0; this->sec=0; }; Time(int hour,int min,int sec){ this->hour=hour; this->min=min; this->sec=sec; } Time operator +(Time temp){ Time t3(0,0,0); t3.sec=sec+temp.sec; t3.min=min+temp.min; t3.hour=hour+temp.hour; if(t3.sec>=60){ t3.sec-=60; t3.min+=1; } if(t3.min>=60){ t3.min-=60; t3.hour+=1; } if(t3.hour>12){ t3.hour-=12; } return t3; } void display(){ cout<<setfill('0')<<setw(2)<<hour<<":"<<setfill('0')<<setw(2)<<min<<":"<<setfill ('0')< <setw(2)<<sec<<":"<<endl; } }; int main(){ Time t1(10,50,55); cout<<"time 1= "; t1.display(); Time t2(2,10,23); cout<<"time 2= "; t2.display(); Time t3(0,0,0); t3=t1+t2; cout<<"time 3= "; t3.display(); } the marks of a student in six subjects and calculate the result. using namespace std; int main() { string studentName, studentClass; string subjects[6]; int marks[6]; int total = 0; float average; char grade; cout << "Enter student name: "; getline(cin, studentName); cout << "Enter student class: "; getline(cin, studentClass); for (int i = 0; i < 6; i++) { cout << "Enter subject " << i + 1 << " name: "; getline(cin, subjects[i]); cout << "Enter marks for " << subjects[i] << ": "; cin >> marks[i]; total += marks[i]; cin.ignore(); // To clear the input buffer for next subject name } average = total / 6.0; if (average >= 90) grade = 'A+'; else if (average >= 80) grade = 'A'; else if (average >= 70) grade = 'B'; else if (average >= 60) grade = 'C'; else grade = 'F'; cout << "\nStudent Name: " << studentName << endl; cout << "Class: " << studentClass << endl; cout << "\nMarks in Subjects:" << endl; for (int i = 0; i < 6; i++) { cout << subjects[i] << ": " << marks[i] << endl; } cout << "\nTotal Marks: " << total << endl; cout << "Average Marks: " << average << endl; cout << "Grade: " << grade << endl;} the area of a triangle, area of rectangle, square and circle using function Overloading. #include <cmath> float area(float base, float height) { return 0.5 * base * height; } float area(float length, float width, char shape) { return length * width; } float area(float side) { return side * side; } float area(double radius) { return M_PI * radius * radius; } int main() { float base, height, length, width, side; double radius; cout << "Enter base and height of triangle: "; cin >> base >> height; cout << "Area of triangle: " << area(base, height) << endl; cout << "Enter length and width of rectangle: "; cin >> length >> width; cout << "Area of rectangle: " << area(length, width, 'r') << endl; cout << "Enter side of square: "; cin >> side; cout << "Area of square: " << area(side) << endl; cout << "Enter radius of circle: "; cin >> radius; cout << "Area of circle: " << area(radius) << endl; } check whether the entered number is Armstrong or not #include <cmath> bool isArmstrong(int num) { int originalNum = num, sum = 0; int digits = 0; int temp = num; while (temp != 0) { digits++; temp /= 10; } temp = num; while (temp != 0) { int digit = temp % 10; sum += pow(digit, digits); temp /= 10; } return sum == originalNum; } int main() { int number; cout << "Enter a number: "; cin >> number; if (isArmstrong(number)) cout << number << " is an Armstrong number." << endl; else cout << number << " is not an Armstrong number." << endl; } Year is a Leap Year int main() { int year; cout << "Enter a year: "; cin >> year; if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { cout << year << " is a leap year." << endl; } else { cout << year << " is not a leap year." << endl; } } Array Rotation void rotateLeft(int arr[], int n, int d) { int temp[d]; for (int i = 0; i < d; i++) { temp[i] = arr[i]; } for (int i = 0; i < n - d; i++) { arr[i] = arr[i + d]; } for (int i = 0; i < d; i++) { arr[n - d + i] = temp[i]; } } int main() { int n, d; cout << "Enter the size of the array: "; cin >> n; int arr[n]; cout << "Enter the elements of the array: "; for (int i = 0; i < n; i++) { cin >> arr[i]; } cout << "Enter the number of positions to rotate: "; cin >> d; rotateLeft(arr, n, d); cout << "Array after rotation: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } } Find Factorial of Any Number int factorial(int n) { if (n == 0 || n == 1) { return 1; } return n * factorial(n - 1); } int main() { int number; cout << "Enter a number: "; cin >> number; cout << "Factorial of " << number << " is " << factorial(number) << endl; } Multiply two 3x3 matrices and print the matrix in the tabular form. int main() { int matrix1[3][3], matrix2[3][3], result[3][3]; cout << "Enter the elements of the first 3x3 matrix: " << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> matrix1[i][j]; } } cout << "Enter the elements of the second 3x3 matrix: " << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> matrix2[i][j]; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { result[i][j] = 0; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } cout << "\nResultant Matrix after multiplication:" << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << result[i][j] << "\t"; } cout << endl; } } remove the character from 1st string which are present in 2nd string. void moveCharacters(string &str1, const string &str2) { string movedChars = ""; for (int i = 0; i < str1.length(); ) { if (str2.find(str1[i]) != string::npos) { movedChars += str1[i]; str1.erase(i, 1); } else { i++; } } cout << "Modified string 1: " << str1 << endl; cout << "Moved characters: " << movedChars << endl; } int main() { string str1 = "hello world"; string str2 = "ole"; moveCharacters(str1, str2); }