[go: up one dir, main page]

0% found this document useful (0 votes)
86 views36 pages

Practical File

The document contains 20 programs written in C++ with examples of different programming concepts like functions, arrays, loops, conditional statements, classes etc. The programs include: 1) A character classification program that classifies a character as alphabet, digit or other. 2) A program that squares all odd numbers stored in an array. 3) A binary search program to search an element in an array. 4) A program to find sum of digits of a number. 5) Programs to calculate area of rectangle and circle using functions. 6) Pattern and matrix programs to print pyramid of stars and transpose of a matrix. 7) Programs on Armstrong number, linear search, factorial, Fibonacci series

Uploaded by

Nijanshu Singh
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)
86 views36 pages

Practical File

The document contains 20 programs written in C++ with examples of different programming concepts like functions, arrays, loops, conditional statements, classes etc. The programs include: 1) A character classification program that classifies a character as alphabet, digit or other. 2) A program that squares all odd numbers stored in an array. 3) A binary search program to search an element in an array. 4) A program to find sum of digits of a number. 5) Programs to calculate area of rectangle and circle using functions. 6) Pattern and matrix programs to print pyramid of stars and transpose of a matrix. 7) Programs on Armstrong number, linear search, factorial, Fibonacci series

Uploaded by

Nijanshu Singh
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/ 36

Practical

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.

2. Program which gives the squares of all the odd


numbers stored in an array .

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)

Q4. Enter any number from 2 digit to 5 digit and


the output will be the sum of all the distinguish
digits of the numbers .

5.Program to print area of rectangle and circle.

6. Pattern Program to print the pyramid of stars.

7. C program to find transpose of a matrix.

8. Program to Generate Armstrong Number.

9. Linear Search Program.

10. Factorial Program.


11. Fibonacci Series.

12. Palindrome.

13.Generate multiplication table upto a certain


range.

14. Program to find LCM.

15. Check prime number.

16. Program calculate sum using function


overloading.

17. Program using the same function print to print


different data types.

18. Program for matrix multiplication.

19.Program to check grades of a student.

20. A program with Do-while loop.


1.Program to input a character and to print
whether it is an alphabet, digit or any other
character.

#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 Area(int l,int b)


{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

void Area(float l,int b)


{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}
void Area(int l,float b)
{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

};

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;

printf("Enter the number of rows in pyramid of stars you wish to see\n");


scanf("%d", &n);

s = n;

for (row = 1; row <= n; row++)


{
for (c = 1; c < s; c++)
printf(" ");

s--;

for (c = 1; c <= 2*row - 1; c++)


printf("*");

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];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d", &m, &n);

printf("Enter elements of the matrix\n");

for (c = 0; c < m; c++)


for(d = 0; d < n; d++)
scanf("%d", &matrix[c][d]);

for (c = 0; c < m; c++)


for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];

printf("Transpose of the matrix:\n");


for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
printf("%d\t", transpose[c][d]);
printf("\n");
}

return 0;
}
9. Program to Generate Armstrong Number.

#include <stdio.h>

int power(int, int);

int main()

int n, sum = 0, temp, remainder, digits = 0;

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;

sum = sum + power(remainder, digits);

temp = temp/10;
}

if (n == sum)

printf("%d is an Armstrong number.\n", n);

else

printf("%d isn't an Armstrong number.\n", n);

return 0;

int power(int n, int r) {

int c, p = 1;

for (c = 1; c <= r; c++)

p = p*n;

return p; }

}
10. Linear Search Program.

#include <stdio.h>
int main()
{
int array[100], search, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);

return 0;
}
11. Program to find the Factorial of a given
number.

#include <stdio.h>
int main()
{
int c, n, fact = 1;

printf("Enter a number to calculate its factorial\n");


scanf("%d", &n);

for (c = 1; c <= n; c++)


fact = fact * c;

printf("Factorial of %d = %d\n", n, fact);

return 0;
}
12. Program to Generate Fibonacci Series.

#include <stdio.h>

int main()
{
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms\n");


scanf("%d", &n);

printf("First %d terms of Fibonacci series are:\n", n);

for (c = 0; c < n; c++)


{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
}

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;

for (int i = 1; i <= range; ++i) {


cout << n << " * " << i << " = " << n * i << endl;
}

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";
}
}

You might also like