Você está na página 1de 41

1.

Program to multiply two 2d array /-

#include<iostream>

using namespace std;

int main ()

int r1, c1, r2, c2, i, j, k;

int A[5][5], B[5][5], C[5][5];

cout << "Enter number of rows and columns of matrix A : ";

cin >> r1 >> c1;

cout << "Enter number of rows and columns of matrix B : ";

cin >> r2 >> c2;

if (c1 != r2)

cout << "Matrices cannot be multiplied!";

exit(0);

cout << "Enter elements of matrix A : ";

for (i = 0; i < r1; i++)

for (j = 0; j < c1; j++)

cin >> A[i][j];

cout << "Enter elements of matrix B : ";

for (i = 0; i < r2; i++)

for (j = 0; j < c2; j++)

1
cin >> B[i][j];

for (i = 0; i < r1; i++)

for (j = 0; j < c2; j++)

C[i][j] = 0;

for (k = 0; k < r2; k++)

C[i][j] += A[i][k] * B[k][j];

cout << "Product of matrices\n";

for (i = 0; i < r1; i++)

for (j = 0; j < c2; j++)

cout << C[i][j] << " ";

cout << "\n";

return 0;

2
Sample Programe :-

3
Sample output:-

4
2.program to print the calendar of a year/-
// A C++ Program to Implement a Calendar
// of an year
#include<bits/stdc++.h>
using namespace std;

/*A Function that returns the index of the day


of the date- day/month/year
For e.g-

Index Day
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday*/
int dayNumber(int day, int month, int year)
{

static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1,


4, 6, 2, 4 };
year -= month < 3;
return ( year + year/4 - year/100 +
year/400 + t[month-1] + day) % 7;
}

/*
A Function that returns the name of the month
with a given month number

Month Number Name


0 January
1 February
2 March
3 April
4 May
5 June
6 July
7 August
8 September
9 October
10 November
11 December */
string getMonthName(int monthNumber)
{
string months[] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",

5
"October", "November", "December"
};

return (months[monthNumber]);
}

/* A Function to return the number of days in


a month

Month Number Name Number of Days


0 January 31
1 February 28 (non-leap) / 29 (leap)
2 March 31
3 April 30
4 May 31
5 June 30
6 July 31
7 August 31
8 September 30
9 October 31
10 November 30
11 December 31

*/
int numberOfDays (int monthNumber, int year)
{
// January
if (monthNumber == 0)
return (31);

// February
if (monthNumber == 1)
{
// If the year is leap then February has
// 29 days
if (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0))
return (29);
else
return (28);
}

// March
if (monthNumber == 2)
return (31);

// April
if (monthNumber == 3)
return (30);

// May
if (monthNumber == 4)

6
return (31);

// June
if (monthNumber == 5)
return (30);

// July
if (monthNumber == 6)
return (31);

// August
if (monthNumber == 7)
return (31);

// September
if (monthNumber == 8)
return (30);

// October
if (monthNumber == 9)
return (31);

// November
if (monthNumber == 10)
return (30);

// December
if (monthNumber == 11)
return (31);
}

// Function to print the calendar of the given year


void printCalendar(int year)
{
printf (" Calendar - %d\n\n", year);
int days;

// Index of the day from 0 to 6


int current = dayNumber (1, 1, year);

// i --> Iterate through all the months


// j --> Iterate through all the days of the
// month - i
for (int i = 0; i < 12; i++)
{
days = numberOfDays (i, year);

// Print the current month name


printf("\n ------------%s-------------\n",
getMonthName (i).c_str());

7
// Print the columns
printf(" Sun Mon Tue Wed Thu Fri Sat\n");

// Print appropriate spaces


int k;
for (k = 0; k < current; k++)
printf(" ");

for (int j = 1; j <= days; j++)


{
printf("%5d", j);

if (++k > 6)
{
k = 0;
printf("\n");
}
}

if (k)
printf("\n");

current = k;
}

return;
}

// Driver Program to check above funtions


int main()
{
int year = 2016;
printCalendar(year);

return (0);
}

8
Sample output:-

9
3.program to check palindrome string /-
#include <stdio.h>

#include <string.h>

int main()

char a[100], b[100];

printf("Enter a string to check if it is a palindrome\n");

gets(a);

strcpy(b, a); // Copying input string

strrev(b); // Reversing the string

if (strcmp(a, b) == 0) // Comparing input string with the reverse string

printf("The string is a palindrome.\n");

else

printf("The string isn't a palindrome.\n");

return 0;

10
Sample program :-

Sample output/-

11
4.program to display all the prime numbers of array/-
#include<iostream>

using namespace std;

int main() {

int arr[10], i, s, j, p;

cout << "Enter Size of An Array :";

cin>>s;

cout << "\nEnter Array Elements :";

for (i = 0; i < s; i++) {

cin >> arr[i];

cout << "\nAll Prime List is :";

for (i = 0; i < s; i++) {

j = 2;

p = 1;

while (j < arr[i]) {

if (arr[i] % j == 0) {

p = 0;

12
break;

j++;

if (p == 1) {

cout << arr[i] << " ";

return 0;

Sample program:-

13
Sample output:-

14
5.Transpose of matrix/-
#include <iostream>

using namespace std;

int main()

int a[10][10], trans[10][10], r, c, i, j;

cout << "Enter rows and columns of matrix: ";

cin >> r >> c;

// Storing element of matrix entered by user in array a[][].

cout << endl << "Enter elements of matrix: " << endl;

for(i = 0; i < r; ++i)

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

cout << "Enter elements a" << i + 1 << j + 1 << ": ";

cin >> a[i][j];

// Displaying the matrix a[][]

cout << endl << "Entered Matrix: " << endl;

for(i = 0; i < r; ++i)

15
for(j = 0; j < c; ++j)

cout << " " << a[i][j];

if(j == c - 1)

cout << endl << endl;

// Finding transpose of matrix a[][] and storing it in array trans[][].

for(i = 0; i < r; ++i)

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

trans[j][i]=a[i][j];

// Displaying the transpose,i.e, Displaying array trans[][].

cout << endl << "Transpose of Matrix: " << endl;

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

for(j = 0; j < r; ++j)

cout << " " << trans[i][j];

if(j == r - 1)

cout << endl << endl;

16
return 0;

Sample programe:-

17
Sample output:-

18
6.program to find the number of vowels , consonant …. In a string/-
#include <iostream>

using namespace std;

int main()

char line[150];

int vowels, consonants, digits, spaces;

vowels = consonants = digits = spaces = 0;

cout << "Enter a line of string: ";

cin.getline(line, 150);

for(int i = 0; line[i]!='\0'; ++i)

if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||

line[i]=='o' || line[i]=='u' || line[i]=='A' ||

line[i]=='E' || line[i]=='I' || line[i]=='O' ||

line[i]=='U')

++vowels;

else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))

19
++consonants;

else if(line[i]>='0' && line[i]<='9')

++digits;

else if (line[i]==' ')

++spaces;

cout << "Vowels: " << vowels << endl;

cout << "Consonants: " << consonants << endl;

cout << "Digits: " << digits << endl;

cout << "White spaces: " << spaces << endl;

return 0;

20
Sample program /-

21
Sample output/-

22
7.program to convert octal to binary /-
#include <iostream>

#include <cmath>

using namespace std;

long long convertOctalToBinary(int);

int main()

int octalNumber;

cout << "Enter an octal number: ";

cin >> octalNumber;

cout << octalNumber << " in octal = " << convertOctalToBinary(octalNumber) << "in binary";

return 0;

long long convertOctalToBinary(int octalNumber)

int decimalNumber = 0, i = 0;

long long binaryNumber = 0;

23
while(octalNumber != 0)

decimalNumber += (octalNumber%10) * pow(8,i);

++i;

octalNumber/=10;

i = 1;

while (decimalNumber != 0)

binaryNumber += (decimalNumber % 2) * i;

decimalNumber /= 2;

i *= 10;

return binaryNumber;

24
Sample program

25
7.program to accept the marks of student and find average /-
#include <iostream>

using namespace std;

int main()

int studentcount, students, score;

float totalscores, average;

string studentname;

cout<<"How many students have taken the exam"<<endl;

cin>> students;

totalscores=0.0;

studentcount=0;

while(students>studentcount)

cout<<"what is the student name: "<<endl;

cin>>studentname;

cout<<"Please enter scores: ";

cin>> score;

cout<<studentname<< " got a score of "<<score<<endl;

26
if(score<100 && score>89)

cout<<studentname<<"'s letter grade is an A."<<endl;;

else if(score<90 && score>79)

cout<<studentname<<"'s letter grade is a B."<<endl;

else if(score<80 && score>69)

cout<<studentname<< "'s letter grade is a C."<<endl;

else

cout<<studentname<<" bellow aveage."<<endl;

studentcount=studentcount+1;

totalscores=totalscores+score;

average=totalscores/students;

27
cout<<"The class average was :"<<average<<endl;

system("pause");

return 0;

Sample program/-

28
Sample output/-

29
8.Program to calculate the roots of quadratic equation/-
#include <iostream>

#include <cmath>

using namespace std;

int main() {

float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;

cout << "Enter coefficients a, b and c: ";

cin >> a >> b >> c;

discriminant = b*b - 4*a*c;

if (discriminant > 0) {

x1 = (-b + sqrt(discriminant)) / (2*a);

x2 = (-b - sqrt(discriminant)) / (2*a);

cout << "Roots are real and different." << endl;

cout << "x1 = " << x1 << endl;

cout << "x2 = " << x2 << endl;

else if (discriminant == 0) {

cout << "Roots are real and same." << endl;

x1 = (-b + sqrt(discriminant)) / (2*a);

cout << "x1 = x2 =" << x1 << endl;

else {

realPart = -b/(2*a);

imaginaryPart =sqrt(-discriminant)/(2*a);

30
cout << "Roots are complex and different." << endl;

cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;

cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;

return 0;

Sample program/-

31
Sample output/-

32
9. Program to find the sum of the row and column of a matrix/-
#include<iostream>

#include<conio.h>

using namespace std;

main()

int a[3][3];

int i, j, s = 0, sum = 0;

cout << "Enter 9 elements of 3*3 Matrix \n";

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

cin >> a[i][j];

cout << "Matrix Entered By you is \n";

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

cout << a[i][j] << " ";

cout << endl;

for (i = 0; i < 3; i++)

33
{

for (j = 0; j < 3; j++)

s = s + a[i][j];

cout << "sum of" << i + 1 << " Row is" << s;

s = 0;

cout << endl;

cout << endl;

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

s = s + a[j][i];

cout << "sum of" << i + 1 << " Column is" << s;

s = 0;

cout << endl;

cout << endl;

for (i = 0; i < 3; i++)

sum = sum + a[i][i];

cout << "Sum of Diagnols Elements is \n" << sum;

return 0;

getch();

34
Sample programme/-

35
Sample output/-

36
37
38
39
40
41

Você também pode gostar