Practical File
Practical File
file
Computer Science
(c++)
Submitted By :- Nijanshu
Singh
Class - XII A
Roll no. – 12128
INDEX
1.Program to input a character and to print
whether it is an alphabet, digit or any other
character.
12. Palindrome.
#include<iostream.h>
void main()
{char ch;
cout<<”enter a character:”;
cin>>ch;
if (((ch>=’A’)&& (ch<=’Z’))||((ch>=’a’)&&(ch<=’z’)))
cout<<”You entered an alphabet”;
else
if(ch>=’0’&&ch<=’9’)
cout<<”You entered a digit”;
else
cout<<”You entered a character other than alphabets and digits”;}
2. Program which gives the squares of all the odd
numbers stored in an array .
#include<iostream.h>
#include<conio.h>
void main()
{ int a[20],i,x; clrscr(); cout<<"\nEnter no of elements ";
cin>>x;
for(i=0;i<x;i++)
{ cout<<"\nEnter no ";
cin>>a[i]; } cout<<"\n\nArr1: ";
for(i=0;i<x;i++) cout<<" "<<a[i];
cout<<"\n\nArr1: ";
for(i=0;i<x;i++)
{ if(a[i]%2==1) a[i]=a[i]*a[i];
cout<<" "<<a[i]; }
getch(); }
3. Program to store numbers in an array and allow
user to enter any number which he/she wants to
find in array .(Binary Search)
#include<iostream.h>
#include<conio.h>
void main()
{ int a[20],n,i,no,low=0,high,mid,f=0;
clrscr();
cout<<"\nEnter no of elements ";
cin>>n;
for(i=0;i<n;i++)
{ cout<<"\nEnter no "; cin>>a[i]; }
cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
cout<<"\nEnter no to be search ";
cin>>no;
high=n-1;
while(low<=high && f==0)
{ mid=(low+high)/2;
if(a[mid]==no)
{ cout<<"\n\n"<<no<<" store at "<<mid+1; f=1; break; }
else
if(no>a[mid])
low=mid+1;
else
high=mid-1;
if(f==0)
cout<<"\n\n"<<no<<" does not exist";
getch(); }
4. Program to Enter any number from 2 digit to 5
digit and the output will be the sum of all the
distinguish digits of the numbers .
#include<iostream.h>
#include<conio.h>
void main()
{ int a=0,b=0,c;
clrscr();
cout<<"\nEnter value for A ";
cin>>a;
while(a>0)
{ c=a%10; b=b+c;
a=a/10; }
cout<<"\nSum of digits "<<b;
getch(); }
5. Program Find absolute value of a number.
#include <iostream.h>
using namespace std;
int absolute(int);
float absolute(float);
int main()
{
int a = -5;
float b = 5.5;
cout << "Absolute value of " << a << " = " << absolute(a) << endl;
cout << "Absolute value of " << b << " = " << absolute(b);
return 0;
}
int absolute(int var) {
if (var < 0)
var = -var;
return var;
}
float absolute(float var){
if (var < 0.0)
var = -var;
return var;
}
6. Program to print area of rectangle and circle.
#include<iostream.h>
#include<conio.h>
class CalculateArea
{
public:
void Area(int r)
{
cout<<"\n\tArea of Circle is : "<<3.14*r*r;
}
};
void main()
{
CalculateArea C;
C.Area(5);
C.Area(5,3);
C.Area(7,2.1f);
C.Area(4.7f,2);
}
7. Pattern Program to print the pyramid of stars.
#include <stdio.h>
int main()
{
int row, c, n, s;
s = n;
s--;
printf("\n");
}
return 0;
}
8. Program to find transpose of a matrix.
#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
return 0;
}
9. Program to Generate Armstrong Number.
#include <stdio.h>
int main()
printf("Input an integer\n");
scanf("%d", &n);
temp = n;
while (temp != 0) {
digits++;
temp = temp/10;
temp = n;
while (temp != 0) {
remainder = temp%10;
temp = temp/10;
}
if (n == sum)
else
return 0;
int c, p = 1;
p = p*n;
return p; }
}
10. Linear Search Program.
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
11. Program to find the Factorial of a given
number.
#include <stdio.h>
int main()
{
int c, n, fact = 1;
return 0;
}
12. Program to Generate Fibonacci Series.
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
return 0;
}
13. Program to find Palindrome.
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
return 0;
}
14. Program to generate multiplication table up to
a certain range.
#include <iostream>
using namespace std;
int main()
{
int n, range;
cout << "Enter an integer: ";
cin >> n;
cout << "Enter range: ";
cin >> range;
return 0;
}
15. Program to find LCM.
#include <iostream>
using namespace std;
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;
}
16. Program to check prime number.
#include <iostream>
using namespace std;
int main()
{
int n, i;
bool isPrime = true;
cout << "Enter a positive integer: ";
cin >> n;
for(i = 2; i <= n / 2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout << "This is a prime number";
else
cout << "This is not a prime number";
return 0;}
17. Program to calculate sum using function
overloading.
#include<iostream.h>
#include<conio.h>
class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}
18. Program using the same function print to print
different data types.
#include <iostream>
using namespace std;
class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};
int main(void) {
printData pd;
pd.print(5);
pd.print(500.263);
pd.print("Hello C++");
return 0;
}
19. Program for Matrix multiplication.
#include <iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
cout<<"enter the number of row=";
cin>>r;
cout<<"enter the number of column=";
cin>>c;
cout<<"enter the first matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the second matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>b[i][j];
}
}
cout<<"multiply of the matrix=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<mul[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
20.Program to check grades of a student.
#include <iostream>
using namespace std;
int main () {
int num;
cout<<"Enter a number to check grade:";
cin>>num;
if (num <0 || num >100)
{
cout<<"wrong number";
}
else if(num >= 0 && num < 50){
cout<<"Fail";
}
else if (num >= 50 && num < 60)
{
cout<<"D Grade";
}
else if (num >= 60 && num < 70)
{
cout<<"C Grade";
}
else if (num >= 70 && num < 80)
{
cout<<"B Grade";
}
else if (num >= 80 && num < 90)
{
cout<<"A Grade";
}
else if (num >= 90 && num <= 100)
{
cout<<"A+ Grade";
}
}