Skip to content

Srilekha_Week2_codes_outputs #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Week1/Srilekha_Vinjamara/10_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
c++ program to find GCD of two numbers
Approach: find maximum of the given 2 numbers and find a number in the range 1 to this maximum which will be the GCD
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);

// num1 = user input1, num2 = user input2
int num1, num2;
cin >> num1 >> num2;
int maxi = (num1 > num2) ? num1 : num2;
for(int i = maxi; i >= 1; i--){
if(num1 % i == 0 && num2 % i == 0){
cout << "GCD of " << num1 << " & " << num2 << " is: " << i;
break;
}
}
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/10_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions Week1/Srilekha_Vinjamara/11_cpp .cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
c++ program to find all prime numbers till and including given input number
Approach: sieve of erasthosthenes, all multiples of a number will not be prime and are marked false
*/

#include <vector>
#include <iostream>
using namespace std;

// function isPrime is used to find if all prime numbers from 2 to the given range = num
void isPrime(int num){
// vec is the variable used to store all prime numbers till a number num
vector<bool> vec(num, true);

for(int i = 2; (i*i) < num; i++){
if(vec[i] == true){
for(int j = (i*i); j <= num; j += i)
vec[j] = false;
}
}
for(int i = 2; i <= vec.size(); i++){
if(vec[i])
cout << i << " ";
}
}

int main() {
//code
int t;
cin >> t;
while(t--){
int num;
cin >> num;
isPrime(num);
cout << "\n";
}
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/11_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions Week1/Srilekha_Vinjamara/12_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
c++ program to find all prime numbers till and NOT including given input number
Approach: sieve of erasthosthenes, all multiples of a number will not be prime and are marked false
*/

#include <vector>
#include <iostream>
using namespace std;

// function isPrime is used to find if all prime numbers from 2 to the given range = num
void isPrime(int num){
// vec is the variable used to store all prime numbers till and NOT including number num
vector<bool> vec(num, true);

for(int i = 2; (i*i) < num; i++){
if(vec[i] == true){
for(int j = (i*i); j <= num; j += i)
vec[j] = false;
}
}
for(int i = 2; i <= vec.size(); i++){
if(vec[i])
cout << i << " ";
}
}

int main() {
//code
int t;
cin >> t;
while(t--){
int num;
cin >> num;
isPrime(num);
cout << "\n";
}
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/12_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions Week1/Srilekha_Vinjamara/13_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
c++ program to find a number raised to it's power
Approach: modular exponentiation/fast multiplication of a number with it's power
*/

#include <bits/stdc++.h>
using namespace std;

// function modExp is used to find modular exponentiation/fast multiplication of a number x to it's power y
long long modExp(long long x, long long y, long long p){
// ans = modular exponentiation final value i.e. result
long long ans = 1;

x = x % p;
if(x == 0)
return 0;
while(y > 0){
if(y & 1)
ans = (ans*x) % p;
x = (x*x) % p;
y = y >> 1;
}
return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);

// x = base, y = power, p = modulus value
long long x, y, p;
cin >> x >> y >> p;
cout << modExp(x, y, p);
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/13_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions Week1/Srilekha_Vinjamara/14_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
c++ program to find a prime number
Approach: if number is divisible by any number other than 1 and itself, flag and mark as prime (till range as square root of input)
*/

#include <iostream>
using namespace std;

int main() {
//code

// t = number of test cases
int t;
cin >> t;
while(t--){
long int num, flag = 0;
cin >> num;
for(int i = 2; (i*i) <= num; i++){
if(num % i == 0){
flag = 1;
break;
}
}
if(flag == 0)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/14_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions Week1/Srilekha_Vinjamara/15_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
c++ program to find pairs of primes whose product is less than or equal to the given input value
Approach: find all prime numbers, store them in a vector and then for every possible subset pair, if their product <= given input, print the pair
*/

#include <vector>
#include <iostream>
using namespace std;

// function isPrime is used to find if a number n is prime or not
bool isPrime(long int n)
{
int flag=0;
for(int i=2;(i*i)<=n;i++){
if(n%i==0){
flag=1;
break;
}
}
if(flag==0)
return true;
else
return false;
}

int main() {
//code
int t;
cin >> t;
while(t--){
// vec is a vector used to stor all prime numbers till given input range number: num
vector<int> vec;

long long num;
cin >> num;
for(int i = 2; i <= num; i++){
if(isPrime(i))
vec.push_back(i);
}
for(int i = 0; i < vec.size(); i++){
for(int j = 0; j < vec.size(); j++){
if(vec[i] * vec[j] <= num)
cout << vec[i] << " "
<< vec[j] << " ";
}
}
cout << "\n";
}
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/15_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions Week1/Srilekha_Vinjamara/16_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
c++ program to find if a number is a power of 2
Approach: using bitwise operations gives smallest worst case time complexity, power of 2 iff the bitwise and(&) of input number num and (num - 1) equal 0. In all other cases, the input number num isn't a power of 2
*/

#include <iostream>
using namespace std;

int main() {
//code
long long t;
cin >> t;
while(t--){
// num = user input number
long long num;

cin >> num;
if(num == 0)
cout << "NO\n";
else if((num & (num - 1)) == 0)
cout << "YES\n";
else
cout << "NO\n";
else if((num & (num - 1)) == 0)
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/16_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions Week1/Srilekha_Vinjamara/1_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
c++ program to print square of a number
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);

// num is a variable used to take input
float num;
cin >> num;
cout << num*num;
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/1_cpp_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions Week1/Srilekha_Vinjamara/2_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
c++ program to print multiplication table of a number
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);

// num is the number taken as input
int num;
cin >> num;
for(int i = 1; i <= 10; i++){
cout << num << " x " << i << " = " << num*i << "\n";
}
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/2_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions Week1/Srilekha_Vinjamara/3a_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
c++ program to print traingle pattern
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int num;

// num is the input taken from user
cin >> num;
for(int i = 1; i <= num; i++){
for(int j = 1; j <= i; j++)
cout << "* ";
cout << "\n";
}
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/3a_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions Week1/Srilekha_Vinjamara/3b_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
c++ program to print traingle pattern
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int num;

// num is the input taken from user
cin >> num;
for(int i = 1; i <= num; i++){
for(int k = num-1; k >= i; k--)
cout << " ";
for(int j = 1; j <= i; j++)
cout << "* ";
cout << "\n";
}
return 0;
}
Binary file added Week1/Srilekha_Vinjamara/3b_cpp_output..png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading