Skip to main content

Posts

Showing posts with the label CPP Programming

Introduction To C++ Programming - Part 10.

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 =...

Introduction To C++ Programming - Part 9.

C++ Program To Add Two Matrices Using Multi-dimensional Arrays #include<iostream> #include<cstdlib> using namespace std; int main() { int A[10][10],B[10][10],c[10][10]; int i,j,m,n,p,q; cout << "Number Of Rows & Columns Of Matrix A: " << endl; cin >> m >> n; cout << "\nNumber Of Rows & Columns Of Matrix B: " << endl; cin >> p >> q; if(m==p&&n==q) cout << "\n\nMatrices Can Be Added."; else { cout << "\nMatrices can Not Be Added Because They're Not The Same Size..\n"; exit(0); } cout<<"\nEnter Matrix A: " << endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) cin >> A[i][j]; } cout<<"\nMatrix A:\n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) cout << A[i][j] <<" "; cout << "\n"; } cout << "\nEnter Matrix B: " << endl; for(i=0;i<m;i++) { for(j...

C++ Program To Implement Casino Number Guessing Game.

#include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; void drawLine(int n, char symbol); void rules(); int main() { string playerName; int amount; int bettingAmount; int guess; int dice; char choice; srand(time(0)); drawLine(70,'_'); cout << "\n\n\n\t\tCASINO GAME\n\n\n\n"; drawLine(70,'_'); cout << "\n\nEnter Your Name : "; getline(cin, playerName); cout << "\n\nEnter Deposit Amount To Play Game : $"; cin >> amount;

C++ Program To Implement Hangman Game.

#include <iostream> #include <cstdlib> #include<ctime> #include <string> using namespace std; const int MAX_TRIES=5; int letterFill (char, string, string&); int main () { string name; char letter; int num_of_wrong_guesses=0; string word; string words[] = { "india", "pakistan", "nepal", "malaysia", "philippines", "australia", "iran", "ethiopia", "oman", "indonesia" }; srand(time(NULL)); int n=rand()% 10; word=words[n];

Introduction To C++ Programming - Part 8.

C++ Program To Multiply Two Matrices Using Multi-dimensional Arrays #include <iostream> using namespace std; int main() { int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; cout << "Enter No. Of Rows & Columns For First Matrix:\n"; cin >> r1 >> c1; cout << "\nEnter No. Of Rows & Columns For Second Matrix:\n"; cin >> r2 >> c2; while (c1!=r2) { cout << "\nError! Column Of First Matrix Is Not Equal To Row Of Second Matrix.\n"; cout << "\nEnter No. Of Rows & Columns For First Matrix:\n"; cin >> r1 >> c1; cout << "\nEnter No. Of Rows & Columns For Second Matrix:\n"; cin >> r2 >> c2; } cout << endl << "\nEnter The Elements Of Matrix 1:" << endl; for(i=0; i<r1; ++i)

C++ Program To Implement Student Report Card System.

#include<iostream> #include<fstream> #include<iomanip> #include <cstdlib> using namespace std; class student { int rollno; char name[50]; int p_marks, c_marks, m_marks, e_marks, cs_marks; double per; char grade; void calculate(); public: void getdata(); void showdata() const; void show_tabular() const; int retrollno() const; }; void student::calculate() { per=(p_marks+c_marks+m_marks+e_marks+cs_marks)/5.0; if(per>=60) grade='A'; else if(per>=50)  grade='B'; else if(per>=33) grade='C'; else grade='F'; }

C++ Program To Implement Tic Tac Toe Game.

#include <iostream> #include<cstdlib> using namespace std; char square[10] = {'o','1','2','3','4','5','6','7','8','9'}; int checkwin(); void board(); int main() { int player = 1,i,choice; char mark; do { board(); player=(player%2)?1:2; cout << "Player " << player << ", Enter A Number: "; cin >> choice; mark=(player == 1) ? 'X' : 'O'; if (choice == 1 && square[1] == '1') square[1] = mark; else if (choice == 2 && square[2] == '2') square[2] = mark; else if (choice == 3 && square[3] == '3') square[3] = mark; else if (choice == 4 && square[4] == '4') square[4] = mark;

Introduction To C++ Programming - Part 7.

C++ Program To Check Whether A Number Is The Sum Of 2 Prime Numbers #include <iostream> using namespace std; int check_prime(int n); int main() { int n, i, flag=0; cout << "Enter A Positive Integer: "; cin >> n; for(i=2; i<=n/2; ++i) { if (check_prime(i)==0){ if ( check_prime(n-i)==0) { cout << endl<< n << " = " << i << " + " << n-i << endl; flag=1; }}} if (flag==0) cout << endl << n << " Can't Be Expressed As Sum Of Two Prime Numbers." << endl; return 0; } int check_prime(int n) /*check prime number*/ { int i, flag=0; for(i=2;i<=n/2;++i) { if(n%i==0) { flag=1; break; }} return flag; }

Introduction To C++ Programming - Part 6.

C++ Program To Calculate Standard Deviation Of Given Numbers #include <iostream> #include <cmath> using namespace std; int main() { int n , j ,y; double _sum=0 , variance; cout << "How Many Numbers To Type? "; cin >> y; cout << endl; double max[y]; for(n=0;n<=y-1;n++) { cout << "Enter Number " << n+1 << " : "; cin >> max[n]; } n=0; cout << endl; cout << "\nSo The Numbers Are:\t"; for(j=0;j<=y-1;j++) { cout << max[n] << "\t"; _sum+=max[n]; n++; }

C++ Program To Implement Bank Management System.

#include<iostream> #include<fstream> #include<cctype> #include<iomanip> #include <cstdlib> using namespace std; class account { int acno; char name[50]; int deposit; char type; public: void create_account(); //function to get data from user void show_account() const; //function to show data on screen void modify(); //function to add new data void dep(int); //function to accept amount and add to balance amount void draw(int); //function to accept amount and subtract from balance amount void report() const; //function to show data in tabular format int retacno() const; //function to return account number int retdeposit() const; //function to return balance amount char rettype() const;  //function to return type of account }; void account::create_account() { cout<<"\nEnter The Account No. : "; cin>>acno; cout<<"\n\nEnter The Name Of The Account Holder : "; cin.ig...

Introduction To C++ Programming - Part 5.

C++ Program To Remove All Characters In A String Except Alphabet #include<iostream> #include<cstring> using namespace std; int main() { char line[150]; int i,j; cout << "Enter A String: "; cin.getline(line, 150); for(i=0; line[i]!='\0'; ++i) { while (!((line[i]>='a'&&line[i]<='z')|| (line[i]>='A'&&line[i]<='Z'|| line[i]=='\0'))) { for(j=i;line[j]!='\0';++j) { line[j]=line[j+1]; } line[j]='\0'; }} cout << "\nOutput String: " << line; return 0; } C++ Program To Find The Number Of Vowels, Consonants, Digits And White Spaces In A String #include<iostream> #include<cstring> using namespace std; int main() { char line[150]; int i,v,c,ch,d,s,o; o=v=c=ch=d=s=0; cout<<"Enter A Sentence: "; cin.getline(line, 150);

Introduction To C++ Programming - Part 4.

C++ Program To Calculate Determinant Of A Matrix #include<iostream> #include<math.h> using namespace std; double d = 0; double det(int n, double mat[10][10]); double det(int n, double mat[10][10]){ double submat[10][10]; if (n == 2) // set the matrix (must be square) to find determinant. return ((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1])); else { for (int c = 0; c < n; c++) { int subi = 0; for (int i = 1; i < n; i++) { int subj = 0; for (int j = 0; j < n; j++) { if (j == c) continue; submat[subi][subj] = mat[i][j]; subj++; } subi++; } d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat)); }} return d; }

Introduction To C++ Programming - Part 3.

Problem: A Simple Program To Find The Absolute Value Of An Integer. Method 1:  #include <iostream> using namespace std; int main() { int number; int abs_number; cout << "Enter an integer (positive or negative): " << endl; cin >> number; // Find the absolute value if(number >= 0) { abs_number = number; } else abs_number = -number; // Print out output cout << "The absolute value of " << number << " is " << abs_number; cout << endl; return 0; } Output: Enter an integer (positive or negative): -11 The absolute value of -11 is 11

Introduction To C++ Programming - Part 2.

if statement: #include <iostream> using namespace std; int main()  { int a, b; cout << "Enter first number: " << endl; cin >> a; cout << "Enter second number: " << endl; cin >> b; if(a < b) cout << "First number is less than second.\n"; return 0; } Output: Enter first number: 5 Enter second number: 7 First number is less than second. if-else statement: #include <iostream> using namespace std; int main()  { int a, b; cout << "Enter first number: " << endl; cin >> a; cout << "Enter second number: " << endl; cin >> b;

Introduction To C++ Programming - Part 1.

Hello World: // File name: hello_world.cpp // A simple C++ program which prints "Hello World!" on the screen. #include <iostream>        // header file to support the C++ I/O system. using namespace std;     // telling the compiler to use namespace "std", // where the entire C++ library is declared. int main() { cout << "Hello World!" <<  endl; /* *  cout is the standard output device on the screen. *  << causes the expression on its right to be directed to the device on its left. *  return 0; indicates the successful termination of the "main" function. */ return 0; } Output: Hello World! Variables And Constants: // File name: variable.cpp // Demonstrate the use of variables and constants. #include <iostream> using namespace std; const int CONST_VAL = 5; // declaring a constant. Its value can't be changed.