0% found this document useful (0 votes)
30 views5 pages

Task 2

The document contains multiple C++ programming tasks, each demonstrating different programming concepts such as loops, arrays, string manipulation, matrix operations, and user authentication. Tasks include printing patterns, performing arithmetic operations on matrices, and implementing a login system with error handling. Each task is presented with its respective code snippet.

Uploaded by

kingofgali36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

Task 2

The document contains multiple C++ programming tasks, each demonstrating different programming concepts such as loops, arrays, string manipulation, matrix operations, and user authentication. Tasks include printing patterns, performing arithmetic operations on matrices, and implementing a login system with error handling. Each task is presented with its respective code snippet.

Uploaded by

kingofgali36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

task 2.

#include <iostream>
using namespace std;

int main() {
for (int i=0; i<5; i++) {
for (int j=0; j<i; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}

task 2.2

#include <iostream>
using namespace std;

int main() {
int arr[10] = {3,5,10,12,15,33,20,22,30,60};

for (int i=0; i<10; i++) {


if (arr[i] % 2 == 0) {
cout << "even index " << i << " = " << arr[i] << endl;
}
}

return 0;
}

task 2.3

#include <iostream>
using namespace std;

int main() {
char message[]= "Everything is fine";

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


message[i] = message[i] + 2;
}

cout<<"New message: "<<message<<endl;

char characters[10];
int count = 0;
char ch;

cout<<"Enter 10 characters:"<<endl;

while(count<10) {
cout<<"Character "<<count + 1<<": ";
cin>>ch;
characters[count] = ch;
count++;
}
cout<<"Not allowed to enter more."<<endl;

cout<<"You entered: ";


for (int i=0; i<10; i++) {
cout << characters[i] << " ";
}

return 0;
}

task 2.4

#include <iostream>
using namespace std;

int main() {
int m1[2][2];
int m2[2][2];
int s[2][2];
int d[2][2];
cout<<"matrix1 :" <<endl;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
cin>>m1[i][j];
}
}

cout<<"matrix2 pls: " <<endl;


for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cin>>m2[i][j];
}
}

for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
s[i][j]=m1[i][j]+m2[i][j];
d[i][j]=m1[i][j]-m2[i][j];
}
}

cout<<"Sum is: " << endl;


for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
cout<<s[i][j]<<" ";
}
cout<<endl;
}

cout<<"subtructed: "<<endl;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
cout<<d[i][j]<<" ";
}
cout<<endl;
}

return 0;
}

task 2.5

#include <iostream>
using namespace std;

int main(){
int r=5;

for(int i=0;i<r;i++){
for(int j=0;j<r-i;j++){
cout<<" ";
}
for(int k=0;k<(2*i-1);k++){
cout<<"*";
}
cout<<endl;
}
return 0;
}

task 2.6

#include <iostream>
using namespace std;

int main(){
int a[2][2]={{2,4},{3,5}};
int b[2][2]={{1,2},{3,4}};
int res[2][2];

for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
res[i][j]=0;
}
}

for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
res[i][j]+=a[i][k]*b[k][j];
}
}
}

cout<<"A x B = "<<endl;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
cout<<res[i][j]<<" ";
}
cout<<endl;
}

return 0;
}

task 2.7

#include <iostream>
using namespace std;

int main(){
string users[]={"admin","test","guest","user1"};
string pass[]={"1234","pass123","guest1","hello"};
int n=4;

string u, p;
cout<<"Enter username: ";
cin>>u;
cout<<"Enter password: ";
cin>>p;

bool ok=false;

for(int i=0;i<n;i++){
if(u==users[i] && p==pass[i]){
ok = true;
break;
}
}

if(ok){
cout<<"Login Done!"<<endl;
}else{
cout<<"Wrong username or pass";
}

return 0;
}

task 2.8

#include <iostream>
using namespace std;

int main(){
string u[] = {"admin", "test", "guest", "user1"};
string p[] = {"1234", "pass123", "guest1", "hello"};
int guest = 0;
string inU, inP;

while(true){
cout<<"Username: ";
cin>>inU;
cout<<"Password: ";
cin>>inP;

bool ok = false;
for(int i=0;i<4;i++){
if(inU ==u[i] && inP ==p[i]){
ok=true;
break;
}
}

if(ok){
guest++;
cout<<"Logged in!\n";
cout<<"Guest online: "<<guest<<endl;
}
else{
cout<<"wrong user/pass."<<endl;
}
}

return 0;
}

task 2.9

#include <iostream>
using namespace std;

int main(){
string u[]={"admin","test","guest","user1"};
string p[]={"1234","pass123","guest1","hello"};
int n=4, max=3, tries=0;
bool logged=false;

while(tries<max && !logged){


string inU,inP;
cout<<"Enter username: "<<endl;
cin>>inU;
cout<<"Enter password: "<<endl;
cin>>inP;

for(int i=0; i<n;i++){


if(inU==u[i] && inP==p[i]){
cout<<"Login Done"<<endl;
logged=true;
break;
}

if(!logged){
cout<< "Wrong username/pass"<<endl;
tries++;
}
}

if(!logged){
cout<<"Too many failed [Link]."<<endl;
}

return 0;
}

You might also like