Showing posts with label CPP Programming. Show all posts
Showing posts with label CPP Programming. Show all posts

Saturday, July 23, 2016

istringstream Example in C++

#include<bits/stdc++.h>

using namespace std;

int main()
{
 string s;
 getline(cin,s);
 int a[10],n,i;
 n=0;
 
 istringstream p(s);
 
 while(p>>a[n]) n++;
 
 for(i=0; i<n; i++)
 {
  cout<<a[i]+5<<" ";
 }
 cout<<endl;
}

Tuesday, May 24, 2016

Perfect Numbers Code in C++

#include<iostream>
#include<cmath>

using namespace std;

int isPerfect(int n)
{
 int i,sum;
 sum=0;
 for(i=1; i<n; i++)
 {
  if(n%i==0) sum+=i;
 }
 
 return sum;
}

int main()
{
 int n;
 cin>>n;
 if(isPerfect(n)==n) cout<<"This is perfect"<<endl;
 else cout<<"Not perfect"<<endl;
 
 cout<<"Some perfect nums are :D \n";
 for(int i=2; i<100; i++)
 {
  if(isPerfect(i)==i) cout<<i<<" ";
 }

 return 0;
}

Saturday, April 2, 2016

STL next_permutation(array,array+size)

#include<bits/stdc++.h>

using namespace std;

int main()
{
 char a[10];
 int l;
 cin>>a;
 l=strlen(a);
 sort(a,a+l);
 do{
  cout<<a<<endl;
 }
 while(next_permutation(a,a+l));
 return 0;
}

Saturday, February 13, 2016

Merit List Generator Program Coded in C++

#include<iostream>

using namespace std;

int main()
{
int st;
cout<<"Enter student number :"<<endl;


cin>>st;
int a[st],b[st],i,j,temp;
string name[st];

for(i=0; i<st; i++)
{

cout<<"Enter student nick name and marks separated by space:\t";
cin>>name[i];
cin>>a[i];

b[i]=a[i];
}

cout<<"\n\nYour result sheet is below :\n";
cout<<"---------------------------\n"<<endl;

for(i=0; i<st-1; i++)
{
for(j=0 ; j<st-i-1; j++)
{
if(a[j]<a[j+1])
{
temp= a[j];
a[j]=a[j+1];
a[j+1] = temp;

}
}
}

for(i=0; i<st; i++)
{
cout<<"Roll "<<i+1<<": ";
for(j=0; j<st; j++)
{
if(a[i] == b[j])
{
cout<<name[j]<<"\t";
cout<<a[i]<<endl;
}
}
}
}

Monday, February 8, 2016

STL: Vector example, a simple program

#include<vector>
#include<bits/stdc++.h>

using namespace std;

int main()
{
string s[10];
vector<string> name;         //you can use your own data type
vector<string>::iterator i;

for(int j=0; j<10; j++)
{
getline(cin,s[j]);
if(s[j]=="#") break;
name.push_back(s[j]);
}
for(i=name.begin(); i!=name.end(); ++i)
{
cout<<*i<<endl;
}
}

STL: Set example, a simple program

#include<set>
#include<bits/stdc++.h>

using namespace std;

int main()
{
string s[50];
set<string>myset;
set<string>::iterator i;


int k;
for(k=0; k<50; k++)
{
getline(cin,s[k]);
if(s[k]=="#") break;
}

for(int i =0 ;i<50; i++)
{
myset.insert(s[i]);
}

for(i=myset.begin(); i!=myset.end(); ++i)
{
cout<<*i<<endl;
}

return 0;
}