0% found this document useful (0 votes)
4 views19 pages

C++ Code Examples for Beginners

Uploaded by

sailor.gamer95
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

C++ Code Examples for Beginners

Uploaded by

sailor.gamer95
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// this code is for printting out your name

#include <iostream>

using namespace std;

int main()

string name;

cout<<"Enter your name\n";

cin>>name;

cout<<name;

return 0;

// this code is for inserting numbers


#include <iostream>

using namespace std;

int main()

int x;

float y;

cout<<"Enter any integer number:\n";

cin>>x;

cout<<"Enter any number:\n";

cin>>y;

cout<<x<<endl<<y;

return 0;

// this code is for adding

#include <iostream>
using namespace std;

int main()

float x;

float y;

int z;

cout<<"Enter any integer number:\n";

cin>>x;

cout<<"Enter any number:\n";

cin>>y;

z=x+y;

cout<<z;

return 0;

// this code is for insrting your name 10 times

#include <iostream>

using namespace std;


int main()

string x;

cout<<"Enter your name\n";

cin>>x;

for(int counter=0;counter <10;counter=counter+1)

cout<<x<<endl;

return 0;

// this code is for adding numbers from 1 to 10

#include <iostream>

using namespace std;

int main()
{

int x=0;

int i;

for( i=1;i<=10;i++)

x=x+i;

cout<<x<<endl;

cout<<i;

return 0;

// this code is for scaling grades

#include <iostream>

using namespace std;

int main()

{
int grade;

cout<<"Enter grade\n";

cin>>grade;

if(grade<=100 && grade>=90 )

cout<<"A\n";

else if(grade<90 && grade>=80 )

cout<<"B\n";

else if(grade<80 && grade>=70 )

cout<<"C\n";

else if(grade<70 && grade>=60 )

cout<<"D\n";

else if(grade<60 && grade>=50 )

cout<<"E\n";

else

cout<<"Fail\n";

return 0;

// this code to print positive or negative using if statement

#include <iostream>

using namespace std;

int main()

int x;
cout<<"Enter any integer number:\n";

cin>>x;

if(x>=0)

cout<<"The inserted number is +ve.";

else

cout<<"The inserted number is -ve.";

return 0;

// this code to print even or odd using if statement

#include <iostream>

using namespace std;

int main()

int x;

int y;
cout<<"Enter any integer number:\n";

cin>>x;

y=x%2;

if(y==0)

cout<<"The inserted nubmer is even.";

else

cout<<"The inserted nubmer is odd.";

return 0;

// this code to print even or odd using if statement

#include <iostream>

using namespace std;

int main()

int x;

int y;

cout<<"Enter any integer number:\n";


cin>>x;

if(x%4==3)

cout<<"Ahmad";

else if(x%4==2)

cout<<"Osama";

else if(x%4==1)

cout<<"Muhammad";

else if(x%4==0)

cout<<"Omran";

return 0;

// this code to print even or odd using if statement

#include <iostream>

using namespace std;

int main()

int x;

int y;

cout<<"Enter any integer number:\n";

cin>>x;
//if(x>0 || x==0)

if(x>=0)

cout<<"The inserted number is positive.";

else

cout<<"The inserted number is negative.";

return 0;

// this code using nesteled if statement

#include <iostream>

using namespace std;

int main()

int x;

int y;

cout<<"Enter any integer number:\n";

cin>>x;

if(x!=0)
{

if(x>0)

cout<<"The inserted number is positive.";

else

cout<<"The inserted number is negative.";

else

cout<<"The inserted number is Zero.";

return 0;

// this code using nesteled if statement

#include <iostream>

using namespace std;

int main()

int x;

int y;

int z;

cout<<"Enter three integer numbers:\n";

cin>>x>>y>>z;

if(x>y)
{

if(y>z)

cout<<"The largest number is "<<x;

else

if(z>x)

cout<<"The largest number is "<<z; }

else

if(y>z)

cout<<"The largest number is "<<y;

else

cout<<"The largest number is "<<z; }

return 0;

}
// Calling function

#include <iostream>

using namespace std;

int add(int x,int y);

int sub(int x,int y);

int main()

int a;

int b;

cout<<"Enter any two integer values:\n";

cin>>a>>b;

cout<<a<<"+"<<b<<"= "<<add(a,b)<<endl;

cout<<a<<"-"<<b<<"= "<<sub(a,b)<<endl;
return 0;

int add(int x,int y)

int z;

z=x+y;

return z;

int sub(int x,int y)

int z=x-y;

return z;

}
// Calling function

#include <iostream>

using namespace std;

int add(int x,int y);

float add(float x,float y);

int sub(int x,int y);

int main()

int a;

int b;

float x,y;

cout<<"Enter any two integer values:\n";

cin>>a>>b;

cout<<"Enter any two values:\n";

cin>>x>>y;
cout<<a<<"+"<<b<<"= "<<add(a,b)<<endl;

cout<<a<<"-"<<b<<"= "<<sub(a,b)<<endl;

cout<<x<<"+"<<y<<"= "<<add(x,y)<<endl;

return 0;

int add(int x,int y)

int z;

z=x+y;

return z;

int sub(int x,int y)

int z=x-y;

return z;

float add(float x,float y)

return x+y;

}
// Calling function

#include <iostream>

using namespace std;

int add(int x,int y);

float add(float x,float y);

int sub(int x,int y);

void sub(float x,float y);

int main()

int a;

int b;

float x,y;

cout<<"Enter any two integer values:\n";

cin>>a>>b;

cout<<"Enter any two values:\n";

cin>>x>>y;

cout<<a<<"+"<<b<<"= "<<add(a,b)<<endl;
cout<<a<<"-"<<b<<"= "<<sub(a,b)<<endl;

cout<<x<<"+"<<y<<"= "<<add(x,y)<<endl;

sub(x,y);

return 0;

int add(int x,int y)

int z;

z=x+y;

return z;

int sub(int x,int y)

int z=x-y;

return z;

float add(float x,float y)

return x+y;

void sub(float x,float y)

float z;
z=x-y;

// return z; Error

cout<<x<<"-"<<y<<"= "<<z<<endl;

You might also like