0% found this document useful (0 votes)
119 views

C++ Program For Bca

The document contains 16 code snippets demonstrating various C++ programming concepts like: 1. Checking if a number is Armstrong or palindrome. 2. Generating Fibonacci series, finding factorial of a number. 3. Creating a basic calculator and converting cases of letters. 4. Generating pyramids, checking prime numbers, reversing arrays. 5. Adding, multiplying and finding maximum/minimum of matrices. 6. Demonstrating function overloading, inheritance, checking vowels.

Uploaded by

shailendra
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
119 views

C++ Program For Bca

The document contains 16 code snippets demonstrating various C++ programming concepts like: 1. Checking if a number is Armstrong or palindrome. 2. Generating Fibonacci series, finding factorial of a number. 3. Creating a basic calculator and converting cases of letters. 4. Generating pyramids, checking prime numbers, reversing arrays. 5. Adding, multiplying and finding maximum/minimum of matrices. 6. Demonstrating function overloading, inheritance, checking vowels.

Uploaded by

shailendra
Copyright
© © All Rights Reserved
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/ 24

1.

Write a program To check whether number is Armstrong

or not..
#include <stdio.h>
int main()
{
int number, sum = 0, temp, remainder;
cout<<Enter an integer\n;
cin>>number;
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
cout<<Entered number is an armstrong number.\n;
else
cout<<Entered number is not an armstrong number.\n;
return 0;
}

2.Write a program to check whther a number is palindrome


number or not.
#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
cout<<Enter a number to check if it is a palindrome or not\n;
cin>>n;
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
cout<<n<< is a palindrome number.;
else
cout<<n<<is not a palindrome number.;
return 0;
}

3.Write a program to generate Fibonacci series.


#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
cout<<Enter the number of terms\n;
cin>>n;
cout<<First <<n<< terms of Fibonacci series are :-\n;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout<<n<<next;
}
return 0;
}

4.Write a program to find factorial of a number.


#include<stdio.h>
#include<conio.h>
int main()
{
unsigned int x,y=1,i;
cout<<Enter Number\n;
cin>>x;
for(i=1;i<=x;i++)
{
y=y*i;
}
cout<<Factorial=<<y);
getch();
return 0;

5.Write a program to create calculator to perform mathematics


operation.
#include<stdio.h>
#include<conio.h>
int main()
{
int ch,a,b;
float c;
cout<<*******************************************\n;
cout<<****

THIS IS CALCUATOR FOR

cout<<****

1.ADDITION

cout<<****

2.SUBTRACTION

cout<<****

3.MULTIPLY

cout<<****

4.DIVISION

****\n;

****\n;
****\n;
****\n;
****\n;

cout<<*******************************************\n;
do
{
cout<< ENTER YOUR CHOICE \n;
cin>>ch;
cout<<\n;
switch(ch)
{
case 1:cout<< YOU CHOOSE ADDITION\n\n;
cout<< Enter first number \n\n;

cin>>a;
cout<< Enter second number \n\n;
cin>>b;
c=a+b;
cout<<a<< + <<b<< = <<c;
break;
case 2:cout<< YOU CHOOSE SUBTRACTION\n;
cout<< Enter first number \n\n;
cin>>a;
cout<< Enter second number \n\n;
cin>>b;
c=a-b;
cout<< a<< <<b<< = <<c;
break;
case 3:cout<< YOU CHOOSE MULTIPLY\n;
cout<< Enter first number \n\n;
cin>>a;
cout<< Enter second number \n\n;
cin>>b;
c=a*b;
cout<<a<<X<<b<< =<<c;
break;
case 4:cout<< YOU CHOOSE DIVISION\n \n;

cout<< Enter first number \n\n;


cin>>a;
cout<< Enter second number \n\n;
cin>>b;
if(b==0)
{
cout<<Mathematical ERROR !!\n;
break;
}
else
c=a/b;
cout<<a<</<<b <<=<<c;
break;
default:cout<<!!!!!!!!!!! ERROR !!!!!!!!!!!\n\n;
}
}while(ch>4);
getch();
return 0;
}

6. Write a program for conversion of upper case letter into lower


case letter.
#include<stdio.h>
#include<conio.h>
int main()
{
char a;
cout<<*************************************\n;
cout<<* ENETR ANY UPPERCASE LETTERr

*\n;

cin>>a;
if(a>=65 && a<=91)
{
cout<<* WOW !! LOWER CASE IS : <<c

cout<<*************************************\n;
}
else
{
cout<<*ERROR !! *\n YOU HAVE NOT ENTERED A ALPHBET \n*;
cout<<*************************************\n;
}
getch();
}

7.Write a program to create a pyramid.


#include<stdio.h>
#include<conio.h>
int main()
{
int x,i,j;
cout<<ENTER SIZE\n;
cin>>x;
cout<<\n;
for(i=1;i<=x;i++)
{
for(j=1;j<=i;j++)
{
cout<<*;
}
cout<<\n;
}
getch();
return 0;
}

8.Write a program to check whether a number is Prime or Not.


#include<stdio.h>
main()
{
int n, c = 2;
cout<<Enter a number to check if it is prime\n;
cin>>n;
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
cout<<n<< is not prime.\n";
break;
}
}
if ( c == n )
cout<<n<< is prime.\n";
return 0;
}

9. Write a program to reverse the elements of array.


#include <stdio.h>
#include<conio.h>
int main()
{
int n, c, d, a[100], b[100];
cout<<Enter the number of elements in array\n;
cin>>n;
cout<<Enter the array elements\n;
for (c = 0; c < n ; c++)
cin>>a[c];
/*
* Copying elements into array b starting from end of array a
*/
for (c = n - 1, d = 0; c >= 0; c--, d++)
b[d] = a[c];
/*
* Copying reversed array into original.
* Here we are modifying original array, this is optional.
*/
for (c = 0; c < n; c++)

a[c] = b[c];
cout<<Reverse array is\n;
for (c = 0; c < n; c++)
cout<<a[c];
return 0;
getch();
}

10.Write a program to find leap year.


#include <stdio.h>
int main(){
int year;
cout<<Enter a year: ;
cin>>year;
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */
{
if ( year%400 == 0)
cout<<year< is a leap year.";
else
cout<<year <<is not a leap year.";
}
else
cout<<year<<is a leap year.";
}
else
cout<<year<< is not a leap year.";
return 0;

11.Write a program to find a maximum & minimum numbers in an


array.
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int* numbers = new int[3];
for(int i = 0; i < 3; i++) {
cout << "Input number no. " << (i + 1);
cin >> numbers[i];
cout << endl;
}
system("PAUSE;
return EXIT_SUCCESS;
}

12.Write a program to addition of two matrix.


#include <stdio.h>
#include<conio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout<<Enter the number of rows and columns of matrix\n;
cin>>m>>n;
cout<<Enter the elements of first matrix\n;
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin>>first[c][d];
cout<<Enter the elements of second matrix\n;
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin>>second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout<<Sum of entered matrices:-\n;
for ( c = 0 ; c < m ; c++ )

{
for ( d = 0 ; d < n ; d++ )
cout<<sum[c][d];
cout<<\n;
}
return 0;
}

13.Write a program to multiply of two matrix.


#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
cout<<Enter the number of rows and columns of first matrix\n;
cin>>m>>n);
cout<<Enter the elements of first matrix\n;
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin>>first[c][d];
cout<<Enter the number of rows and columns of second matrix\n;
cin>>p>>q;
if ( n != p )
cout<<Matrices with entered orders can't be multiplied with each
other.\n;
else
{
cout<<Enter the elements of second matrix\n;
for ( c = 0 ; c < p ; c++ )

for ( d = 0 ; d < q ; d++ )


cin>>second[c][d];
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
cout<<Product of entered matrices:-\n;
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
cout<<multiply[c][d];
cout<<\n;
}
}
return 0;

14.Write a program to demonstrate function overloading.


#include <iostream>
using namespace std;
/* Number of arguments are different */
void display(char []); // print the string passed as argument
void display(char [], char []);
int main()
{
char first[] = "C programming";
char second[] = "C++ programming";
display(first);
display(first, second);
return 0;
}
void display(char s[])
{
cout << s << endl;
}
void display(char s[], char t[])
{
cout << s << endl << t << endl;

15.write a program to demonstrate inheritance.


#include<iostream.h>
#include<conio.h>
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
cout<<"Enter the employee number:";
cin>>eno;
cout<<"Enter the employee name:";
cin>>name;
cout<<"Enter the designation:";
cin>>des;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;

public:
void get1()
{
cout<<"Enter the basic pay:";
cin>>bp;
cout<<"Enter the Humen Resource Allowance:";
cin>>hra;
cout<<"Enter the Dearness Allowance :";
cin>>da;
cout<<"Enter the Profitablity Fund:";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t
"<<pf<<"\t"<<np<<"\n";
}
};
void main()

{
int i,n;
char ch;
salary s[10];
clrscr();
cout<<"Enter the number of employee:";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}

16. Write a program to find whether the character is Vowel or


Constant.
#include<stdio.h>
#include<conio.h>
void main()
{
//Program variables
char letter;
clrscr();//function to clear previous output
cout<<Enter the letter : ;//Display function
cin>>letter;

//Getting input function

// if((letter>'a' && letter<'z')||(letter>'A' && letter<'Z'))//Conditional


statement
// {
switch(letter) //Switch statement
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
case 'A' :

case 'E' :
case 'I' :
case 'O' :
case 'U' :cout<<Character is vowel;
break;
default: cout<<Character is consonant;
break;
}
// }
// else
//

cout<<Character is incorrect;

getch();
}

You might also like