Skip to content

Instantly share code, notes, and snippets.

@hussnainsheikh
Last active February 17, 2018 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hussnainsheikh/d1fccbde5a4bf3b9e8867f7fd8c8c23f to your computer and use it in GitHub Desktop.
Save hussnainsheikh/d1fccbde5a4bf3b9e8867f7fd8c8c23f to your computer and use it in GitHub Desktop.
Simple Problems solution in C++. Please find the comment below for Problem statements.
Program No 1:
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n, reversedNumber = 0, remainder;
cout << "Enter an integer: ";
cin >> n;
while (n != 0)
{
remainder = n % 10;
reversedNumber = reversedNumber * 10 + remainder;
n /= 10;
}
cout << "Reversed Number = " << reversedNumber;
_getch();
return 0;
}
Program No 2:
// ProgramNo2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int find_char(string st1 , char c){
const char * st2 = st1.c_str();
int length = strlen(st2), n=0;
for (int i = 0; i < length; i++)
{
if (st2[i] == c)
{
n = 1;
break;
}
else
{
n = 0;
}
}
return n;
}
int main()
{
char c;
string st;
cout << "Enter String: ";
getline(cin , st);
cout << "Enter a character to find: ";
cin >> c;
cout<< "Return: "<<find_char(st, c);
_getch();
return 0;
}
Program No 3:
// ProgramNo3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
void calculate(int year, int month, int day){
int agey, agem, aged, age;
agey = 2017 - year;
agem = 12 - month;
agem = agem + 5;
aged = 30 - day;
aged = aged - 8;
agey = agey * 12;
agey = agey - agem;
cout << agey;
age = agey * 30.4167;
cout << "\n\n\t\tYour Age in Days is " << age;
}
void main()
{
int birthmonth, birthyear, birthday;
cout << "Enter Your Birth Year(Eg:1994): ";
cin >> birthyear;
cout << "\n\nEnter Your Birth Month(Eg:11): ";
cin >> birthmonth;
cout << "\n\nEnter Your Birth Day(Eg:14): ";
cin >> birthday;
calculate(birthyear, birthmonth, birthday);
_getch();
}
Program No 4:
// ProgramNo4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
void main(){
string username, pass;
cout << "Enter Username: ";
getline(cin, username);
cout << "Enter Password: ";
getline(cin, pass);
if (username == "abcd" && pass == "pakistan"){
cout << "Login is success.";
}
else
{
cout << "Access denied.";
}
_getch();
}
Program No 5:
// ProgramNo5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int secondLargest(int a[], int size) {
int currentMax = a[0];
int secondMax = 0;
for (int i = 0; i< size; i++) {
if (a[i] >secondMax)
secondMax = a[i];
if (a[i] > currentMax){
secondMax = currentMax;
currentMax = a[i];
}
}
return secondMax;
}
int main() {
int a[5];
cout << "Enter an Array of 5 integers: ";
for (int i = 0; i < 5; i++)
{
cin >> a[i];
}
cout << "The Second Largest Number of the array is :" << secondLargest(a, 5) << endl;
_getch();
return 0;
}
Program No 6:
// ProgramNo6.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
int array[50], n, i, j, temp;
cout << "Enter number of elements: \n";
cin >> n;
cout << "Enter "<< n << " integers: \n";
for (i = 0; i < n; i++)
cin >> array[i];
for (i = 0; i < (n - 1); i++){
for (j = 0; j < n - i - 1; j++){
if (array[j] < array[j + 1]){
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
cout<<"Sorted Array in descending order:\n";
for (i = 0; i < n; i++)
cout << array[i]<<endl;
_getch();
return 0;
}
Program No 7:
// ProgramNo7.cpp : Defines the entry point for the console application.
//
#include "stdafx.h";
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int mat1[2][2], mat2[2][2], i, j, mat3[2][2];
cout << "Enter 2X2 Matrix 1 elements :";
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
cin >> mat1[i][j];
}
}
cout << "Enter 2X2 Matrix elements :";
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
cin >> mat2[i][j];
}
}
cout << "\nThe First Matrix1: \n";
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
cout << " " << mat1[i][j] << " ";
}
cout << "\n";
}
cout << "\nThe Second Matrix2: \n";
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
cout << " " << mat2[i][j] << " ";
}
cout << "\n";
}
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
cout << "\nThe Addition of two matrix:\n";
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
cout << " " << mat3[i][j] << " ";
}
cout << "\n";
}
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j)
{
mat3[j][i] = mat1[i][j];
}
cout << endl << "\nTranspose of First Matrix1: " << endl;
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j)
{
cout << " " << mat3[i][j];
if (j == 2 - 1)
cout << endl << endl;
}
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j)
{
mat3[i][j] = 0;
}
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j)
for (int k = 0; k < 1; ++k)
{
mat3[i][j] += mat1[i][k] * mat2[k][j];
}
cout << endl << "Multiplication of Matrix: " << endl;
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j)
{
cout << " " << mat3[i][j];
if (j == 2 - 1)
cout << endl;
}
_getch();
}
Program No 8:
// ProgramNo8.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
int a[10][10], i, j, sum = 0, m, n;
cout<<"\nEnter the row and column of matrix: ";
cin>> m >> n;
cout << "\nEnter the elements of matrix: ";
for (i = 0; i<m; i++)
for (j = 0; j<n; j++)
cin>>a[i][j];
cout<<"\nThe matrix is\n";
for (i = 0; i<m; i++){
cout<<"\n";
for (j = 0; j<m; j++){
cout<<"\t" << a[i][j];
}
}
for (i = 0; i<m; i++){
for (j = 0; j<n; j++){
if (i == j)
sum = sum + a[i][j];
}
}
cout<<"\n\nSum of the diagonal elements of a matrix is: "<<sum;
_getch();
return 0;
}
Program No 9:
// ProgramNo9.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main()
{
string s1, s2, result;
cout << "Enter string String one: ";
getline(cin, s1);
cout << "Enter string String two: ";
getline(cin, s2);
result = s1 + s2;
cout << "Resultant String = " << result;
_getch();
return 0;
}
Program No 10:
// ProgramNo10.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i, j, len, flag = 1;
char a[20];
cout << "Enter a string:";
cin >> a;
for (len = 0; a[len] != '\0'; ++len);
for (i = 0, j = len - 1; i<len / 2; ++i, --j)
{
if (a[j] != a[i])
flag = 0;
}
if (flag == 1)
cout << "\nThe string is Palindrome";
else
cout << "\nThe string is not Palindrome";
_getch();
return 0;
}
@hussnainsheikh
Copy link
Author

  1. Write a function, which return reverse of integer number. For example input is ‘12345’ Output will be ‘54321’.

  2. Write a C++ program with a function int find_char(char*,char), That finds out whether there is a given character in a string. The return value of the function find_char should be 1 if the given character is in the string otherwise 0.

  3. Write a program which tells a user in number of days how old is he / she when date-of-birth is entered. Take input of date of birth in main function and pass it through any means to function NoOfDays(). The function should return the number of days, cater for months with 28, 30 and 31 days, also cater for leap year.
    A leap year is one in which February is of 29 days, 2012 was a leap year.

  4. Write a function that takes username and password as string and display message "login in success" if username is "abcd" and password is="pakistan" and display message "Login failed" otherwise.

  5. Write a program that find the second largest number in an array.(solution should be generic)

  6. Write a program to sort an array using bubble sort in descending order.

  7. Write a program that add two matrix, multiply two matrix and find the transpose of a matrix.

  8. Write a program that finds the sum of diagonal entries of a matrix.

  9. Write a program that concatenates two strings.

  10. Write a program that checks the string entered by user is palindrome or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment