Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Assignment Programs

1- C program to find whether a given year is leap year or not.


Problem Description

This program takes a year as input and finds whether a year is leap year or not.

Problem Solution
1. Take a year as input.
2. Check whether a given year is divisible by 400.
3. Check whether a given year is divisible by 100.
4. Check whether a given year is divisible by 4.
5. If the condition at step 2 and 4 becomes true, then the year is a leap year.
6. If the condition at step 3 becomes true, then the year is not a leap year.

Program Code
#include <iostream>
using namespace std;

int main()
{
int year;
char a;
cout<<"Press 'y' to proceed!";
cin>>a;
while(a=='y'){

cout << "Enter a year: ";


cin >> year;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year.\n";
else
cout << year << " is not a leap year.\n";
}
else
cout << year << " is a leap year.\n";
}
else
cout << year << " is not a leap year.\n";
cout<<"\n\nPress 'y' to continue or Press 'n' to exit!";
cin>>a;
system("cls");

}
return 0;

}
Program Explanation
1.Take a year as input and store it in the variable year.
2. Using if,else statements to,
a) Check whether a given year is divisible by 400.
b) Check whether a given year is divisible by 100.
c) Check whether a given year is divisible by 4.
3. If the condition at step 2.a becomes true, then print the ouput as “It is a leap year”.
4. If the condition at step 2.b becomes true, then print the ouput as “It is not a leap year”.
5. If the condition at step 2.c becomes true, then print the ouput as “It is a leap year”.
6. If neither of the condition becomes true, then the year is not a leap year and print the same

Result
2-C program to display the ATM transaction.

Problem Solution:
1. Firstly initialize the ATM pin and amount with some random number.
2. Take the ATM pin as input.
3. If the input pin is equal to the initialized pin, then do the further operations.
4. Use switch statement to do the operations like Balance checking, Cash withdrawal, Cash
deposition etc.
5. Use while loop to terminate or restart the process.

Problem Description
This C Program performs ATM transaction. The types of ATM transaction are
1) Balance checking
2) Cash withdrawal
3) Cash deposition

4) Exit

Source Code:
1. #include<iostream>
2. using namespace std;
3. int main()
4. { system("color 80");
5. int pinCode ;
6. int Pincode=2368;
7. int Balance=10000;
8. int tt;
9. int TypeOfTransaction;
10. int Wamount;
11. int Damount;
12. char b;
13. cout<<"********Welcome to the ATM!********\n\n\nPlease enter your PIN code to
proceed\n";
14. cin>>pinCode;
15. a:
16. while(pinCode==Pincode)
17. {
18. cout<<"\nplease select the type of transaction\n1-Balance Check\n2-Cash Withdraw\n3-
Cash
19. Deposit\n4-Cancel\n";
20. cin>>TypeOfTransaction;
21.
22. switch(tt)
23. {
24. case 0 :
25. if(TypeOfTransaction==1)
26. {
27. cout<<"your remaining balance is 10000";
28. cout<<"\n\nPress 'y' to perform another transaction\n";
29. cin>>b;
30. system("cls");
31. if(b=='y')
32. {
33. goto a;
34. }
35. }
36. case 1:
37. if(TypeOfTransaction==2)
38. {
39. cout<<"\nEnter the amount to withdraw\n";
40. cin>>Wamount;
41. if(Wamount%100!=0)
42. {
43. cout<<"\nPlease enter the amount in multiples of 100";
44. }
45. if(Wamount>10000)
46. {
47. cout<<"\nSorry insufficient balance!";
48. }
49. if(Wamount%100==0&&Wamount<10000)
50. {
51. cout<<"\nCollect your cash\nYour remaining balance is : "<<10000-
Wamount;
52. }
53.
54. cout<<"\n\nPress 'y' to perform another transaction\n";
55. cin>>b;
56. system("cls");
57. if(b=='y')
58. {
59. goto a;
60. }
61. }
62. case 2 :
63. if (TypeOfTransaction==3)
64. {
65. cout<<"\nEnter the amount to deposit:\n";
66. cin>>Damount;
67. cout<<"\n\nyour new balance : "<<Damount+10000;
68. cout<<"\n\nPress 'y' to perform another transaction\n";
69. cin>>b;
70. system("cls");
71. if(b=='y')
72. {
73. goto a;
74. }
75. }
76. case 3 :
77. if(TypeOfTransaction==4)
78. {
79. system("cls");
80. cout<<"\n\n\n\n\n************Thank_You for visiting
Atm!*********\n\n\n\n\n";
81. exit(0);
82. }
83. }
84. }
85. }

Program Explanation:
1. Initialize the variables pin, amount and transaction with 2368, 1000 and ‘y’ respectively.
2. Ask for the pin from user. If the input pin is equal to 1520, then allow for the further
operations.
3. Use switch statement to do the operations like Check Balance, Withdraw Cash, Deposit
Cash and Quit.
4. For Check Balance simply print the variable amount as output and exit.
5. For Withdraw Cash, first ask the amount to withdraw and store it in the variable withdraw.
6. If withdraw % 100 != 0, then ask user to enter the amount in multiplies of 100.
7. If withdraw amount is greater than (amount-500), then print the output as “INSUFFICENT
BALANCE”.
8. Otherwise subtract the variable withdraw from variable amount, print the amount and exit.
9. For deposit operation, ask the user for amount and store it in the variable deposit.
10. Add the variable deposit to variable amount, print the amount and exit.
11. If quit, then finally ask the user if they wish to continue or not. Ask them to type y/n and
store it in the variable transaction.
12. If variable transaction is y/Y, then continue the operation.
Result:

3- C++ program to print Diamond Pattern


Problem Description:
This program prints the diamond pattern.
Program Source Code:
1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int number = 0;
7. cout << "Please enter a number: ";
8. cin >> number;
9.
10. for (int i = 0; i < number; i++)
11. {
12. // prints space
13. for (int j = 0; j < (number - i - 1); j++)
14. {
15. cout << " ";
16. }
17.
18. // prints stars
19. for (int j = 0; j < (2 * i + 1); j++)
20. {
21. cout << "*";
22. }
23.
24. cout << endl;
25. }
26. for (int i = number ; i >= 0; i--)
27. {
28.
29. for (int j = 0; j < (number - i - 1); j++)
30. {
31. cout << " ";
32. }
33.
34. for (int j = 0; j < (2 * i + 1); j++)
35. {
36. cout << "*";
37. }
38.
39. cout << endl;
40. }
41.
42. return (0);
43. }

Problem Solution
1. Take the number of rows as input.
2. According to the number of rows, print the ” ” and “*” using for loops.
3. Exit.

Result:
4- C++ Program to display the IP Address of the system
Problem Description
This program displays the IP address of the system.
Problem Solution:
1. Create a socket to define network interface IPv4.
2. Define the IPv4 address type.
3. Define the port name where network is attached.
4. Access the network interface information by passing address using ioctl.
5. Extract the IP address.
Source Code:
1. #include<iostream>
2. using namespace std;
3. int main()
4. {
5. system("C:\\Windows\\System32\\ipconfig");
6. return(0);
7. }

Program Explanation:
1. Create a socket to define network interface IPv4 using statement socket(AF_INET,
SOCK_DGRAM, 0) and store it in the variable n.
2. Define the IPv4 address type by assigning AF_INET to (ifr.ifr_addr.sa_family).
3. Define the port name where network is attached using statement strncpy(ifr.ifr_name ,
array , IFNAMSIZ – 1), where array is initialized with string “etho”.
4. Call the ioctl function to access the network interface information by passing the address.
5. Close the variable n.
Result:

5- C++ program to illustrate user authentication


Problem Description
This C program asks for the user name & password and displays the same to illustrate user
authentication.
Problem Solution-
{ use built-in functions gets(), getchar() and a character array username[10], password[10] }
1. Take the user name & password as input.
2. Print the each character of password as * while receiving it.
3. Now print the original password and exit.
Program/Source Code:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. char password[10], username[10], ch;
5. int i;
6. char a;
7. cout<<"Press 'y'to continue!";
8. cin>>a;
9.
10. while(a=='y')
11. {
12.
13. cout<<"Enter User name: \n";
14. cin>>username;
15. cout<<"Enter the password < any 4 characters>: \n";
16. for (i = 0; i < 4; i++) {
17. cin>>ch;
18. password[i] = ch;
19. ch = '*' ;
20. cout<<ch;
21. }
22. password[i] = '\0';
23.
24. cout<<"\n Your username is : "<<username;
25. cout<<"\n Your password is : ";
26. for (i = 0; i < 8; i++)
27. {
28. cout<<password[i];
29. }
30. cout<<"Press 'y' to continue!";
31. cin>>a;
32. system("cls");
33. }
34.
35. }

Program Explanation
1. Take the username as input and store it in the array username[].
2. Using for loop take the each character of password as input and store it in the array password[] and
consecutively print it as ‘*’.
3. Print the array password[] as output and exit.
Result :
6- C++ Program to determine if a given matrix is a
sparse matrix:-
Problem Solution:
Sparse matrix is a matrix with the majority of its elements equal to zero. This program accepts matrix
and checks whether the given matrix is a sparse matrix.
Program/Source Code:
1. #include <iostream>
2.
3. int main ()
4. {
5. static int array[10][10];
6. int i, j, m, n;
7. int counter = 0;
8.
9. cout<<”Enter the order of the matix \n";
10. cin>>m>>n;
11. cout<<"Enter the co-efficients of the matix \n";
12. for (i = 0; i < m; ++i)
13. {
14. for (j = 0; j < n; ++j)
15. {
16. cin>>array[i][j];
17. if (array[i][j] == 0)
18. {
19. ++counter;
20. }
21. }
22. }
23. if (counter > ((m * n) / 2))
24. {
25. cout<<"The given matrix is sparse matrix \n";
26. }
27. else
28. cout<<"The given matrix is not a sparse matrix \n";
29. cout<<"There are “<<counter<<” number of zeros";
30. }

Program Explanation:
In this C program, we are reading the order of the matrix row and column using ‘m’ and ‘n’ variables
respectively. Using for loop the coefficient elements of the matrix is assigned to the variable
‘array[i][j]’. Using if condition statement, check the coefficient element values of the matrix are
equal to zero. If the condition is true then it will execute if condition statement and increment the count
variable value.
The sparse matrix has more zero elements than non zero elements of the matrix. To check the given
matrix is sparse matrix or not the if-else condition statement is used to check the multiplication of row
and column of the matrix value of ‘m’ and ‘n’ variables respectively.
When dividing the element values by 2 its value should be less than the number of zero elements
counted by the value of ‘counter’ variable. If the condition is true, then it will display the given matrix is
sparse. Otherwise, it will display the given matrix is not a sparse matrix and displays the number of
zeros counted in the matrix.

Results:

7- C++ program to find whether a number is

prime or not
Problem Description:
The following C program, using recursion, finds whether the entered number is a prime number or
not.
Problem Solution [ Define a function name, primeno() ]
A prime number is an integer that has no integral factor but itself and 1.
Source Code:

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n, i;
6. bool isPrime = true;
7. char a;
8. cout<<"Press 'y' to proceed!";
9. cin>>a;
10. while (a=='y')
11. {
12. cout << "\n\nEnter a positive integer: ";
13. cin >> n;
14. for(i = 2; i <= n / 2; ++i)
15. {
16. if(n % i == 0)
17. {
18. isPrime = false;
19. break;
20. }
21. }
22. if (isPrime)
23. cout << "\nThis is a prime number";
24. else
25. cout << "\nThis is not a prime number";
26. cout<<"\n\n\nPress 'y' to proceed again!";
27. cin>>a;
28.
29. }
30. return 0;
31. }

Program Explanation:
In this C program, we are reading the integer number using ‘num’ variable. A prime number
is an integer that has no integral factor but itself and 1. The check variable is used to call the
primeno() function by passing the value of ‘num’ variable and the value of division of ‘num’
variable value by 2 as an argument.
The primeno() function is used to find whether the entered number is a prime number or not.
If else condition statement is used to check the value of ‘i’ variable is equal to 1 and return
the value of ‘i’ variable to the called variable ‘check’.
Otherwise, if the condition is false execute the else statement and call the primeno() function
by passing the value of ‘num’ variable and the decrement the value of ‘i’ variable by 1.
Return the resulted value to the called variable ‘check’.
If else condition statement is used to check that the value of ‘check’ variable is equal to 1. If
the condition is true print the statement as prime number. Otherwise, if the condition is false
print the statement as not a prime number.

Result

C++program to compute the product of two


8-
matrices
Problem Description
This C Program computes the product of two matrices. This program accepts the 2 matrices and then
find the product of 2 matrices.

Problem Solution
1. Create three 2D arrays, third matrix will store the product of first two matrices.
2. Read all the elements of first two arrays using scanf() function under nested for loop.
3. Make a function for evaluating product of the matrix.
4. Inside this function, three iterators are created. First make sure that number of columns in first
matrix equals number of rows in the second row.
5. Then multiply the first element in the first row(of first matrix) by the first element of the first
column(in second matrix), the second element of the first row by the second element of the first
column, and the third element in the first row by the third element in the first column. Then, add these
products to find the dot product.
6. Continue this multiplication of first row elements(of first matrix) with second columns elements(of
second matrix), then adding the products. In this way third matrix will get filled.
7. The third matrix will have size of number of rows of first matrix * number of columns in second
matrix.

Program/Source Code
1. #include <iostream>
2. #include <conio.h>
3. using namespace std;
4. int main(void) {
5. const int N=3;
6. int A[N][N];
7. int B[N][N];
8. int C[N][N];
9. int i;
10. int j;
11. int k;
12. int sum;
13.
14. //Here we ask numbers for A
15. for (i = 0; i < N; i++) {
16. for (j = 0; j < N; j++)
17. {
18. cout << "Type number: ";
19. cin >> A[i][j];
20. cout << endl;
21. }
22. }
23.
24. //Here we ask numbers for B
25. cout << endl << endl;
26. for (i = 0; i < N; i++) {
27. for (j = 0; j < N; j++)
28. {
29. cout << "Type number: ";
30. cin >> B[i][j];
31. cout << endl;
32. }
33. }
34.
35. //Here we calculate C
36. for (i = 0; i < N; i++) {
37. for (j = 0; j < N; j++)
38. {
39. sum = 0;
40. for (k = 0; k < N; k++) {
41. sum += A[i][k] * B[k][j];
42. }
43. C[i][j] = sum;
44. }
45. }
46.
47. //Here we cout sum
48. cout << endl << sum;
49. getch();
50. return 0;
51. }

Program Explanation
1. Define three 2D arrays (i.e matrices) of some fixed size.
2. The third matrix will store the multiplication result of first two matrices. The third matrix will have
same number of rows as that of first matrix and same number of columns as that of second matrix.
3. Read all the elements of first two matrix using cin() function inside nested for loop in a separate
function named readMatrix().
4. Then to evaluate the product, make a function called productMatrix().
5. Inside it, make three loops with three iterators(i , j and k) , each loop under another.
6. Inside these loops the same code of multiplication will be written.
7. Code consists of multiplying the elements of first row of first matrix to elements of the first column of
the second matrix respectively. Add all these products to get a single value. Now this is the first
element of the third matrix.
8. To get second element, multiply first row of first matrix with second column of second matrix
respectively and add. Continue with the pattern to get all the elements of the third array.
9. Print the product matrix.

Result:

You might also like