DATA STRUCTURE Code PRACTICE (DSA)
DATA STRUCTURE CODE PRACTICE :
Data Structure is a way to store and organize data so that it can be used efficiently . For example, we can store a list of items using array,linked list,stack,queue,tree,graph.
#include <iostream>
using namespace std;
int main() {
int n=10;
int a=0,b=1;
cout<<a<<" "<< b<<" ";
for(int i=1;i<=10;i++){
int nextNumber=a+b;
cout<<nextNumber<<" ";
a=b;
b=nextNumber;
}
}
Prime Number
---------------------------------------------------------------------------------
#include <iostream> using namespace std; int main() { int n=7; bool isPrime=0; for(int i=2;i<n;i++){ if(n%i==0){ isPrime=1; break; //bcz if it not prime we need to break } } if(isPrime){ cout<<"Not a Prime"<<endl; }else{ cout<<"it's prime number"<<endl; } }
Subtract the product and sum digit
----------------------------------------------------------------------------------
#include <iostream> using namespace std; int main() { int n=7890; int product=1; int sum=0; while(n){ int a=n%10; product*=a;
sum+=a;
n=n/10; } cout<<abs(product-sum)<<endl;
}
Count Number if 1 bit
------------------------------------------------------ -----------------------
#include <iostream> using namespace std; int main() { int n=0000000000000001011; int count=0; while(n){ if(n&1){ count++; } n=n>>1; } cout<<count<<endl; return 0; }
o/p ====> 3
Reverse Integer
--------------------------------------------------------------------------------
#include <iostream>
#include<math.h>
using namespace std;
int main() {
int x=123,ans=0;
while(x){
int digit=x%10;
ans=ans*10+digit;
x/=10;
}
cout<<ans<<endl;
return 0;
}