You are on page 1of 5

DSA Problems

02.03.2021

1) #include <iostream>
using namespace std;
int main() {

int num, i, upto;

// Take input from user


cout << "Find prime numbers upto : ";
cin >> upto;

cout << endl << "All prime numbers upto " << upto << " are : " << endl;

for(num = 2; num <= upto; num++) {

for(i = 2; i <= (num / 2); i++) {

if(num % i == 0) {
i = num;
break;
}
}
// If the number is prime then print it.
if(i != num) {
cout << num << " ";
}
}
return 0;
}

output
Find prime numbers upto : 100
All prime numbers upto 100 are :
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

4) #include <iostream>
using namespace std;

struct employee {
string ename;
int age, phn_no;
int salary;
};

// Function to display details of all employees


void display(struct employee emp[], int n)
{
cout << "Name\tAge\tPhone Number\tSalary\n";
for (int i = 0; i < n; i++) {
cout << emp[i].ename << "\t" << emp[i].age << "\t"
<< emp[i].phn_no << "\t" << emp[i].salary << "\n";
}
}

// Driver code
int main()
{
int n = 3;
// Array of structure objects
struct employee emp[n];
// Details of employee 1
emp[0].ename = "Chirag";
emp[0].age = 24;
emp[0].phn_no = 1234567788;
emp[0].salary = 20000;

// Details of employee 2
emp[1].ename = "Arnav";
emp[1].age = 31;
emp[1].phn_no = 1234567891;
emp[1].salary = 56000;
// Details of employee 3
emp[2].ename = "Shivam";
emp[2].age = 45;
emp[2].phn_no = 1100661111;
emp[2].salary = 30500;
display(emp, n);

return 0;
}

Output:
Name Age Phone Number Salary
Chirag 24 1234567788 20000
Arnav 31 1234567891 56000
Shivam 45 8881101111 30500

2) #include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
// iterate until n becomes 0
// remove last digit from n in each iteration
// increase count by 1 in each iteration
while (n != 0) {
n /= 10; // n = n/10
++count;
}

printf("Number of digits: %d", count);


}

Output:
Enter an integer: 1234567892
Number of digits: 10

You might also like