logo

Computer Science Quiz

Programming in C++


Programming in C++
Elements
Attributes

Html5

API
Graphics

Programming in C++
#include <iostream>
int main(){
    int r, c, a[100][100], b[100][100], sum[100][100], i, j;
    cout << "Enter number of rows : ";
    cin >> r;

    cout << "Enter number of columns: ";
    cin >> c;

    cout << endl << "Enter elements of first matrix: " << endl;
    for(i = 0; i < r; ++i)
       for(j = 0; j < c; ++j)       {
           cout << "Enter element a" << i + 1 << j + 1 << " : ";
           cin >> a[i][j];
       }
     cout << endl << "Enter elements of second matrix: " << endl;
    for(i = 0; i < r; ++i)
       for(j = 0; j < c; ++j)
       {
           cout << "Enter element b" << i + 1 << j + 1 << " : ";
           cin >> b[i][j];
       }
	for(i = 0; i < r; ++i)
        for(j = 0; j < c; ++j)
            sum[i][j] = a[i][j] + b[i][j];
     cout << endl << "Sum of two matrix is: " << endl;
    for(i = 0; i < r; ++i)
        for(j = 0; j < c; ++j){
            cout << sum[i][j] << "  ";
            if(j == c - 1)
                cout << endl;
        }

    return 0;
}
#include <iostream>
int main(){
  int origNum, num, rem, sum = 0;
  cout << "Enter a positive  integer: ";
  cin >> origNum;
  num = origNum;
  while(num != 0)  {
      digit = num % 10;
      sum += digit * digit * digit;
      num /= 10;
  }

  if(sum == origNum)
	cout << origNum << " is an Armstrong number.";
  else
    cout << origNum << " is not an Armstrong number.";
  return 0;
}
#include <iostream>
int main(){    
    float n1, n2, n3;
    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;
    if(n1 >= n2 && n1 >= n3)    {
        cout << "Largest number: " << n1;
    }
    if(n2 >= n1 && n2 >= n3)    {
        cout << "Largest number: " << n2;
    }
    if(n3 >= n1 && n3 >= n2) {
        cout << "Largest number: " << n3;
    }
    return 0;
}
#include<iostream>
int main(){
    float fahrenheit, celsius;
    cout << "Enter the temperature in Celsius : ";
    cin >> celsius;
    fahrenheit = (celsius * 9.0) / 5.0 + 32;
    cout << "Temperature in Celsius    : " << celsius << endl;
    cout << "Temperature in Fahrenheit : " << fahrenheit << endl;
    return 0;
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main(){
	clrscr();
	char str1[100], str2[100];
	cout<<"Enter first string : ";
	gets(str1);
	cout<<"Enter second string : ";
	gets(str2);
	if(strcmp(str1, str2)==0)	{
		cout<<"Both the strings are equal";
	}
	else	{
		cout<<"Both the strings are not equal";
	}
	getch();
}
#include <stdio.h>
#include <math.h>
int main ()
{
  double param, result;
  param = 27.0;
  result = cbrt (param);
  printf ("cbrt (%f) = %f\n", param, result);
  return 0;
}
#include<iostream.h>
#include<conio.h>
void main(){
int i, no, fact=1;
clrscr();
cout<<"Enter the any no. : ";
cin>>no;
for(i=1;i<=no;i++){
fact=fact*i;
}
cout<<"Factorial: "<<fact;
getch();
}
#include<conio.h>
#include<iostream.h>
void main()
{
float f,c;
clrscr();
cout<<"Enter Fahrenheit degree to find temperature in celsius: ";
cin>>f;
c = (f-32)/1.8;
cout<<"\n\n\tCELSIUS DEGREE = "<<c;
getch();
}
#include<iostream.h>
#include<conio.h>
void main(){
int i,no, first=0, second=1, next;
clrscr();
first=0;
second=1;
cout<<"Enter nubmer of terms for Series: ";
cin>>no;
cout<<"Fibonacci series are: \n";
for(i=0; i<no; i++){
cout<<"\n"<<first;
next = first + second;
first = second;
second = next;
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int no,a=0;
clrscr();
cout<<"Enter any number : ";
cin>>no;
while(no>0)
{
no=no/10;
a++;
}
cout<<"\n no. of digits in given number is: "<<a;
getch();
}
#include <iostream>
int main(){    
    float n1, n2, n3;
    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;
    if(n1 >= n2 && n1 >= n3)    {
        cout << "Largest number: " << n1;
    }
    if(n2 >= n1 && n2 >= n3) {
        cout << "Largest number: " << n2;
    }
    if(n3 >= n1 && n3 >= n2) {
        cout << "Largest number: " << n3;
    }
    return 0;
}
#include<iostream.h>
#include<conio.h>
void gcd(int,int);
void main(){
    int a,b;
    clrscr();
    cout<<"Enter two numbers: ";
    cin>>a>>b;
    gcd(a,b);
    getch();
  }
void gcd(int a,int b){
    int m,n;
    m=a;
    n=b;
    while(m!=n){
        if(m>n)
            m=m-n;
        else
            n=n-m;
    }
    cout<<"\nH.C.F of"<<a<<" & "<<b<<" is "<<m;
}
#include <iostream>
int main(){
    int n1, n2, max;
    cout << "Enter two numbers: ";
    cin >> n1 >> n2;
    max = (n1 > n2) ? n1 : n2;
    do{
        if (max % n1 == 0 && max % n2 == 0){
            cout << "LCM = " << max;
            break;
        }
        else
            ++max;
    } while (true);
    
    return 0;
}
#include<iostream.h>
 #include<conio.h>
 main()  {
       clrscr();
       int year;
       cout<<"\n Enter the year = ";
       cin>>year;
       if(year%4==0 || year%400==0 || year%100==0)
      cout<<"this is a leap year"<<endl;
       else
      cout<<"this is not a leap year"<<endl;
       getch();
   }
#include <iostream>
int main(){
    string str = "C++ Programming";
    cout << "String Length = " << str.size();
    return 0;
}
#include <iostream>
int main()
{
    int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
    cout << "Enter rows and columns for first: ";
    cin >> r1 >> c1;
    cout << "Enter rows and columns for second: ";
    cin >> r2 >> c2;
	while (c1!=r2)    {
        cout << "Error! column of first matrix not equal to row of second.";

        cout << "Enter rows and columns for first matrix: ";
        cin >> r1 >> c1;

        cout << "Enter rows and columns for second matrix: ";
        cin >> r2 >> c2;
    }

    cout << endl << "Enter elements of matrix 1:" << endl;
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c1; ++j) {
            cout << "Enter element a" << i + 1 << j + 1 << " : ";
            cin >> a[i][j];
        }

    cout << endl << "Enter elements of matrix 2:" << endl;
    for(i = 0; i < r2; ++i)
        for(j = 0; j < c2; ++j)  {
            cout << "Enter element b" << i + 1 << j + 1 << " : ";
            cin >> b[i][j];
        }

	for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)  {
            mult[i][j]=0;
        }

    for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)
            for(k = 0; k < c1; ++k)  {
                mult[i][j] += a[i][k] * b[k][j];
            }

    cout << endl << "Output Matrix: " << endl;
    for(i = 0; i < r1; ++i)
    for(j = 0; j < c2; ++j) {
        cout << " " << mult[i][j];
        if(j == c2-1)
            cout << endl;
    }

    return 0;
}
#include <iostream>
int main(){
    int n;
    cout << "Enter an integer: ";
    cin >> n;
    if ( n % 2 == 0)
        cout << n << " is even.";
    else
        cout << n << " is odd.";

    return 0;
}
#include<iostream>
int main(){
    int palindrome, reverse=0;
    cout<<"Enter number:  ";
    cin>>palindrome;
    int num=0,key=palindrome;
for(int i=1;palindrome!=0;i++){
    num=palindrome%10;
    palindrome=palindrome/10;
    reverse=num+(reverse*10);
   }
   if(reverse==key){
   cout<<key<<" is a Palindrome Number";
   }
 else{
   cout<<key<<"is NOT a Palindrome Number";
   }
return 0;
}
#include<iostream>
int main(){
    int rows;
    cout << "Enter the number of rows : ";
    cin >> rows;
    cout << endl;
    for (int i = 0; i < rows; i++){
        int val = 1;
        for (int j = 1; j < (rows - i); j++) {
            cout << "   ";
        }
        for (int k = 0; k <= i; k++) {
            cout << "      " << val;
            val = val * (i - k) / (k + 1);
        }
        cout << endl << endl;
    }
    cout << endl;
    return 0;
}
#include <stdio.h>
#include <math.h> 
int main (){
  printf ( pow (8.0, 3.0) );
   return 0;
}
#include<iostream.h>
#include<conio.h>

void main()
{
  int i,no;
  clrscr();
  cout<<"Enter any num: ";
  cin>>no;
  if(no==1)  {
  cout<<"Smallest prime num is 2";
  }
  for(i=2;i<no;i++)  {
  if(no%i==0)  {
  cout<<"Not prime num";
  break;
  }
  }
 if(no==i) {
 cout<<"Prime num";
 }
  getch();
}
#include <iostream>
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;
    return 0;
}
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(){
 char str[100],temp;
 int i,j=0;
 clrscr();
 cout<<"Enter any the string :";
 gets(str); 
 i=0;
 j=strlen(str)-1;
  while(i<j)   {
   temp=str[i];
   str[i]=str[j];
   str[j]=temp;
   i++;
   j--;
   }
 cout<<"Reverse string is: "<<str;
 getch();
}
#include<iostream>
int main(){
	int p,r,t,i;
	cout<<"Enter Principle : ";
	cin>>p;
	cout<<"Enter Rate : ";
	cin>>r;
	cout<<"Enter Time : ";
	cin>>t;
	i=(p*r*t)/100;
	cout<<"Simple interest is : "<<i;
	return 0;
}
#include<iostream.h>
#include<conio.h>
 void main() {
 int i,a[10],temp,j;
 clrscr();
 cout<<"Enter any 10 num in array: \n";
 for(i=0;i<=10;i++) {
 cin>>a[i];
 }
 cout<<"\nData before sorting: ";
 for(j=0;j<10;j++) {
 cout<<a[j];
 }
 for(i=0;i<=10;i++) {
 for(j=0;j<=10-i;j++) {
 if(a[j]>a[j+1]) {
 temp=a[j];
 a[j]=a[j+1];
 a[j+1]=temp;
 }
 }
 }
 cout<<"\nData after sorting: ";
 for(j=0;j<10;j++) {
 cout<<a[j];
 }
 getch();
 }
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main() { 
int num,ans; 
clrscr(); 
cout<<"Enter any Num: "; 
cin>>num; 
ans=pow(num,0.5);
cout<<"\n Squre of "<<num<<" is: "<<ans; 
getch(); 
} 
#include<iostream>
int main(){
    int val, num, sum = 0;
    cout << "Enter the number : ";
    cin >> val;
    num = val;
    while (num != 0)    {
        sum = sum + num % 10;
        num = num / 10;
    }
    cout << "Sum of the digits of "<< val << " is " << sum;
}
#include<iostream>
int main(){
    int i,n,sum=0;
    cout<<"How many numbers? ";
    cin>>n;
    for(i=1;i<=n;++i)    {
        sum+=i;
    }
    cout<<"Sum="<<sum;
    return 0;
}
#include<iostream>
void main(){
   int first[20], second[20], c, n;
   cout << "Enter the number of elements in the array ";
   cin >> n;
   cout << "Enter elements of first array " << endl;
   for ( c = 0 ; c < n ; c++ )
     cin >> first[c];
   cout << "Enter elements of second array " << endl;
   for ( c = 0 ; c < n ; c++ )
      cin >> second[c];
   cout << "Sum of elements of two arrays " << endl;
   for ( c = 0 ; c < n ; c++ )
     cout << first[c] + second[c] << endl;
   return 0;
}
#include <iostream>
int main()
{
    int a = 5, b = 10, temp;
    cout << "Before swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;
    temp = a;
    a = b;
    b = temp;
    cout << "\nAfter swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;
    return 0;
}
#include<iostream>
int main(){
	char uppercase;
	int offset = 'a' - 'A';
	char lowercase;
	cout<<"Enter an uppercase letter:  ";
	cin>>uppercase;
	if(uppercase>='A' && uppercase<='Z')
		lowercase = (char)(uppercase + offset);
	else
		lowercase= uppercase;
	cout<<"The lowercase letter is:  "<<lowercase << endl;
	return 0;
}

Most Viewed Computer Science Links