Posts

Showing posts from January, 2017

Program to calculate the number of days from the beginning of the year to a date specified by the user through array in C++

Image
Using Visual Studio 2015 =============================================================== #include<iostream> using namespace std; #include<conio.h> void main() {  int day,month,totaldays;  int days_in_a_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};  cout<<"Enter the month= ";  cin>>month;  cout<<"Enter the day (1 to 31) = ";  cin>>day;  totaldays=day;  for (int i = 0;i < month - 1;i++)  {      totaldays+=days_in_a_month[i];  }  cout<<"Total number of days are "<<totaldays;  _getche(); } ==========================================================================

Program to find average of sales in a week using array in C++

Image
Using Visual Studio 2015 =============================================================== #include<iostream> using namespace std; #include<conio.h> void main() {  const int size=7;  double arr[size],total=0,average;  for (int i = 0;i < size;i++)  {   cout<<"Enter number "<<i<<"=";   cin>>arr[i];  }  for (int j = 0;j < size;j++)  total+=arr[j];  average=total/size;   cout<<"Average="<<average;  _getche(); } ===========================================================================

Program to find equal values using if statement in C++

Image
Using Visual Studio 2015 =============================================================== #include<iostream> using namespace std; #include<conio.h> void main() {  int num1, num2, num3;  cout << "Enter first number= ";  cin >> num1;  cout << "Enter second number= ";  cin >> num2;  cout << "Enter third number= ";  cin >> num3;  if (num1==num2)  {   cout <<"\nFirst number is equal to second number";  }  if (num1 == num3)  {   cout <<"\nFirst number is equal to third number";  }  if (num2 == num3)  {   cout <<"\nSecond number is equal to third number";  }  _getche();  } ==========================================================================

Find the largest number out of three given values in C++

Image
Using Visual Studio 2015 =============================================================== #include<iostream> using namespace std; #include<conio.h> void main() {  int num1,num2,num3;  cout<<"Enter first number= ";  cin>>num1;  cout << "Enter second number= ";  cin >> num2;  cout << "Enter third number= ";  cin >> num3;  if (num1 > num2&&num1 > num3)  {   cout<<num1<<" is the largest number";  }  else if(num2 > num1&&num2 > num3)  {   cout << num2 << " is the largest number";  }  else if (num3 > num1&&num3 > num2)  {   cout << num3 << " is the largest number";  }  _getche(); } ==========================================================================

Program to convert seconds into hour, minute and second format in C++

Image
Using Visual Studio 2015 =============================================================== #include<iostream> using namespace std; #include<conio.h> void main() {  int sec,hour,min;  cout<<"Enter the seconds= ";  cin>>sec;  min=sec/60;  hour=min/60;  min=min%60;  sec=sec%60;  cout<<hour<<" Hours "<<min<<" minutes "<<sec<<" seconds";  _getch();   } =========================================================================

Binary Search in array C++

Image
Using Visual Studio 2015 =============================================================== #include <iostream> using namespace std; #include <conio.h> void main () {        int arr [10], as = 10, start = 0, end = 9, mid = (start + end) / 2, input;        bool check = false ;        for ( int i = 0; i < as; i++)        {        cout << "Enter the number " << (i+1) << "=" ;        cin >> arr[i];        }        cout << "Enter the value to search= " ;        cin >> input;        while (start <= end)        {               mid = (start + end) / 2;               if (input == arr[mid])               {                      check = true ;                      break ;               }               else if (input>arr[mid])               {                      start = mid + 1;               }               else if (input<arr[mid])     

Selection Sort of array in C++

Image
Using Visual Studio 2015 =============================================================== #include<iostream> using namespace std; #include<conio.h> int main() {  int i, j, n, loc, temp, min, arr[30];  cout << "Enter the number of elements:";  cin >> n;  for (i = 0;i<n;i++)  {   cout << "Enter the number=";   cin >> arr[i];  }  for (i = 0;i<n - 1;i++)  {   min = arr[i];   loc = i;   for (j = i + 1; j<n; j++)   {    if (min>arr[j])    {     min = arr[j];     loc = j;    }   }   temp = arr[i];   arr[i] = arr[loc];   arr[loc] = temp;  }  cout << "\nSorted list is as follows"<<endl;  for (i = 0;i<n; i++)  {   cout << arr[i] << " ";  }  _getche(); }      =========================================================================

Bubble sorting of array in C++

Image
Using Visual Studio 2015 =============================================================== #include<iostream> #include<conio.h> using namespace std; void main() {  int array[5],temp;  for (int i = 0; i<5; i++)  {   cout << "Enter the number=";   cin >> array[i];  }  cout << endl;  for (int i = 0; i<4; i++)  {   for (int j = 0; j<4; j++)   {    if (array[j]>array[j + 1])    {     temp = array[j];     array[j] = array[j + 1];     array[j + 1] = temp;    }   }  }  cout << "Bubble Sorted Array is " << endl;  for (int i = 0; i<5; i++)  {   cout << array[i] << endl;  }  _getche(); }                                    =========================================================================

Suppose you are given a square matrix of size n x n, write a program to determine if this is an identity matrix. 2D array

Image
Using Visual Studio 2015 =============================================================== #include<iostream> using namespace std; #include<conio.h> void main() {  int array1[2][2];  for (int i = 0; i <= 1; i++)  {   for (int j = 0; j <= 1; j++)   {    cout<<"Enter the integers=";    cin>>array1[i][j];   }  }  for (int i = 0; i <= 1; i++)  {  for(int j = 0; j <= 1; j++)  {  cout<<array1[i][j];  }  cout<<endl;   }  if (array1[0][0] == 1 && array1[1][1] == 1 && array1[0][1] == 0 && array1[1][0] == 0)  {   cout << "\nIt is an identity matrix";  }  else  {   cout << "\nNot an identity matrix";  }  _getche(); } =========================================================================

Write the program to print the Maximum and Minimum value of array.

Image
Using Visual Studio 2015 =========================================================================== #include <iostream> using namespace std; #include <conio.h> void main() {       int arr[10],i;       for (i = 0; i < 10; i++)       {             cout << "Enter the number" << (i+1) << "=" ;             cin >> arr[i];       }       int maxvalue=arr[0],minvalue=arr[0];       for (i = 0; i < 10; i++)       {             if (arr[i] >= maxvalue)             {                   maxvalue=arr[i];             }             if (arr[i] <= minvalue)             {                   minvalue=arr[i];             }       }       cout << "\nMaximum value in the array= " << maxvalue;       cout << "\nMinimum value in the array= " << minvalue;       _getche(); } ==================================

Program to create a multiplication table of a given number using while loop.

Image
Using Visual Studio 2015 =============================================================== #include<iostream> using namespace std; #include<conio.h> void main() {  int num , value, mul;  cout<<"enter the number=";  cin>>num;  cout << "Multiplication Table for " << num << " :- " << endl;  mul = 1;  while (mul <= 10)  {   value = num * mul;   cout << num << " * " << mul << " = " << value << endl;   mul++;  }  _getche(); }

Program to calculate the sum of first 100 odd numbers using while loop

Image
Using Visual Studio 2015 =============================================================== #include<iostream> using namespace std; #include<conio.h> void main() {  int x, num;  x = 0;  num = 1;  while (num <= 100)  {   if (num % 2 == 1)   {    x = num + x;    cout << num << " + ";   }   num++;  }  cout << "\nSum of first 100 odd numbers=" << x;  _getche(); } =========================================================================

Program to calculate the sum of first 100 even numbers using while loop

Image
Using Namespace Std: ========================================================================= #include<iostream> using namespace std; #include<conio.h> void main() { int x, num; x = 0; num = 1; while (num <= 100) { if(num%2==0) {  x = num + x;   cout << num<< " + ";   }  num++;  } cout << "\nSum of first 1000 even numbers=" << x; _getche(); } =========================================================================