Skip to content

Commit 2e4444a

Browse files
committed
chore: use using-declaration
1 parent 6c8deb6 commit 2e4444a

10 files changed

+46
-56
lines changed

C++ programs/task1.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
// Write a C++ program to print ASCII value of a character.
33

44
#include <iostream>
5-
using namespace std;
65

76
int main() {
87
char c;
9-
cout << "Enter a character: ";
10-
cin >> c;
11-
cout << "ASCII Value is: " << int(c);
8+
std::cout << "Enter a character: ";
9+
std::cin >> c;
10+
std::cout << "ASCII Value is: " << int(c);
1211
}

C++ programs/task10.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <string.h>
55
#include <iostream>
6-
using namespace std;
76

87
char to_lowercase(char c) {
98
if (c >= 'A' && c <= 'Z') {
@@ -14,12 +13,12 @@ char to_lowercase(char c) {
1413
}
1514

1615
int main() {
17-
string str;
18-
cout << "Enter a string: ";
19-
getline(cin, str);
16+
std::string str;
17+
std::cout << "Enter a string: ";
18+
std::getline(std::cin, str);
2019
for (char& c : str) {
2120
c = to_lowercase(c);
2221
}
23-
cout << str;
22+
std::cout << str;
2423
return 0;
2524
}

C++ programs/task2.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
// Write a C++ program to check if the given number is even or odd.
33

44
#include <iostream>
5-
using namespace std;
65

76
int main() {
87
int no;
9-
cout << "Enter a no.: ";
10-
cin >> no;
8+
std::cout << "Enter a no.: ";
9+
std::cin >> no;
1110
if (no % 2 == 0) {
12-
cout << "Even";
11+
std::cout << "Even";
1312
} else {
14-
cout << "Odd";
13+
std::cout << "Odd";
1514
}
1615
return 0;
1716
}

C++ programs/task3.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22
// Write a C++ program to check if the given year is a leap year.
33

44
#include <iostream>
5-
using namespace std;
65

76
int main() {
87
int year;
9-
cout << "Enter a year: ";
10-
cin >> year;
8+
std::cout << "Enter a year: ";
9+
std::cin >> year;
1110
if (year % 100 == 0) {
1211
if (year % 400 == 0) {
13-
cout << "Century Leap year.\n";
12+
std::cout << "Century Leap year.\n";
1413
} else {
15-
cout << "Not a century leap year.\n";
14+
std::cout << "Not a century leap year.\n";
1615
}
1716
} else if (year % 4 == 0) {
18-
cout << "Leap year.\n";
17+
std::cout << "Leap year.\n";
1918
} else {
20-
cout << "Not a leap Year.\n";
19+
std::cout << "Not a leap Year.\n";
2120
}
2221
return 0;
2322
}

C++ programs/task4.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
// Write a C++ program to calculate sum of all the elements of an array.
33

44
#include <iostream>
5-
using namespace std;
65

76
int main() {
87
int ar[10];
9-
cout << "Enter 10 elements: " << endl;
8+
std::cout << "Enter 10 elements: " << std::endl;
109
int sum = 0;
1110
for (int i = 0; i < 10; i++) {
12-
cin >> ar[i];
11+
std::cin >> ar[i];
1312
sum += ar[i];
1413
}
15-
cout << "Sum of array: " << sum;
14+
std::cout << "Sum of array: " << sum;
1615
}

C++ programs/task5.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
// Write a C++ program to find the largest and smallest element in an array.
33

44
#include <iostream>
5-
using namespace std;
65

76
int main() {
87
int ar[10];
9-
cout << "Enter 10 elements: " << endl;
8+
std::cout << "Enter 10 elements: " << std::endl;
109
for (int i = 0; i < 10; i++) {
11-
cin >> ar[i];
10+
std::cin >> ar[i];
1211
}
1312
int large = 0, small = 99999;
1413
for (int i = 0; i < 10; i++) {
@@ -20,6 +19,6 @@ int main() {
2019
small = ar[i];
2120
}
2221
}
23-
cout << "Largest number:" << large << endl
24-
<< "Smallest number:" << small << endl;
22+
std::cout << "Largest number:" << large << std::endl
23+
<< "Smallest number:" << small << std::endl;
2524
}

C++ programs/task6.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Write a C++ program to sort elements of array in ascending order.
33

44
#include <iostream>
5-
using namespace std;
65

76
int* bubbleSort(int a[]) {
87
int temp;
@@ -20,13 +19,13 @@ int* bubbleSort(int a[]) {
2019

2120
int main() {
2221
int ar[10];
23-
cout << "Enter 10 elements: " << endl;
22+
std::cout << "Enter 10 elements: " << std::endl;
2423
for (int i = 0; i < 10; i++) {
25-
cin >> ar[i];
24+
std::cin >> ar[i];
2625
}
2726
int* a = bubbleSort(ar);
28-
cout << "Sorted array: " << endl;
27+
std::cout << "Sorted array: " << std::endl;
2928
for (int i = 0; i < 10; i++) {
30-
cout << a[i] << " ";
29+
std::cout << a[i] << " ";
3130
}
3231
}

C++ programs/task7.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Write a C++ program to swap two numbers using function.
33

44
#include <iostream>
5-
using namespace std;
65

76
void swap(int* a, int* b) {
87
int temp;
@@ -13,12 +12,12 @@ void swap(int* a, int* b) {
1312

1413
int main() {
1514
int q, w;
16-
cout << "Enter 2 numbers: " << endl;
17-
cin >> q >> w;
18-
cout << "1st number = " << q << endl;
19-
cout << "2nd number = " << w << endl;
15+
std::cout << "Enter 2 numbers: " << std::endl;
16+
std::cin >> q >> w;
17+
std::cout << "1st number = " << q << std::endl;
18+
std::cout << "2nd number = " << w << std::endl;
2019
swap(&q, &w);
21-
cout << "After swapping:" << endl;
22-
cout << "1st number = " << q << endl;
23-
cout << "2nd number = " << w << endl;
20+
std::cout << "After swapping:" << std::endl;
21+
std::cout << "1st number = " << q << std::endl;
22+
std::cout << "2nd number = " << w << std::endl;
2423
}

C++ programs/task8.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
#include <stdio.h>
1414
#include <string.h>
1515
#include <iostream>
16-
using namespace std;
1716

1817
int main() {
1918
char str1[20], str2[20];
2019
int len, len1, len2, i, j, found = 0, not_found = 0;
21-
cout << "Enter first string :";
20+
std::cout << "Enter first string :";
2221
fgets(str1, 20, stdin);
23-
cout << "Enter second string :";
22+
std::cout << "Enter second string :";
2423
fgets(str2, 20, stdin);
2524
len1 = strlen(str1);
2625
len2 = strlen(str2);
@@ -40,13 +39,13 @@ int main() {
4039
}
4140
}
4241
if (not_found == 1) {
43-
cout << str1 << " and " << str2 << " are not anagrams.";
42+
std::cout << str1 << " and " << str2 << " are not anagrams.";
4443
} else {
45-
cout << str1 << " and " << str2 << " are anagrams.";
44+
std::cout << str1 << " and " << str2 << " are anagrams.";
4645
}
4746
} else {
48-
cout << str1 << " and " << str2
49-
<< " are not of same length to be anagrams.";
47+
std::cout << str1 << " and " << str2
48+
<< " are not of same length to be anagrams.";
5049
}
5150
getch();
5251
}

C++ programs/task9.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@
1212
*/
1313

1414
#include <iostream>
15-
using namespace std;
1615

1716
int main() {
1817
int days, y, w, d;
19-
cout << "Enter number of days: ";
20-
cin >> days;
18+
std::cout << "Enter number of days: ";
19+
std::cin >> days;
2120
y = days / 365;
2221
days = days % 365;
2322
w = days / 7;
2423
days = days % 7;
2524
d = days;
26-
cout << "Years: " << y << endl;
27-
cout << "Weeks: " << w << endl;
28-
cout << "Days : " << d << endl;
25+
std::cout << "Years: " << y << std::endl;
26+
std::cout << "Weeks: " << w << std::endl;
27+
std::cout << "Days : " << d << std::endl;
2928
}

0 commit comments

Comments
 (0)