0% found this document useful (0 votes)
30 views9 pages

Ex 1

The document contains 7 examples of C++ code demonstrating basic programming concepts like loops, functions, conditionals, and math operations. The examples include programs to print multiplication tables, find greatest common divisors, print pyramids of stars, check for prime numbers, define and call functions, calculate change, and validate and calculate the area of a triangle.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
30 views9 pages

Ex 1

The document contains 7 examples of C++ code demonstrating basic programming concepts like loops, functions, conditionals, and math operations. The examples include programs to print multiplication tables, find greatest common divisors, print pyramids of stars, check for prime numbers, define and call functions, calculate change, and validate and calculate the area of a triangle.
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 9

Chathurika Suraweera Lab 5 Ex1 #include<iostream> using namespace std; int main() { for(int i=1;i<=9;i++) { int N=0; for(int

j=1;j<=9;j++) { N=N+i; cout<<N<<"\t"; } cout<<endl; } return 0; }

Ex2
# include<iostream> using namespace std; int main() { int num1,num2; int lnum,snum; int answr; int mod; cout<<"Enter first number"<<endl; cin>>num1; cout<<"Enter second number"<<endl;

cin>>num2; if(num1>num2) { lnum=num1; snum=num2; } else { lnum=num2; snum=num1; } do { answr=lnum/snum; mod=lnum%snum; cout<<lnum<<"/"<<snum<<"-"<<answr<<endl; cout<<lnum<<"%"<<snum<<"-"<<mod<<endl; lnum=snum; snum=mod; } while(snum>=0); return 0; }

Ex3

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

cout<<"Enter the number pyramid of stars you wish to see "; cin>>n; for( int i=1;i<=n;i+=2) { for(int j=n;j>i;j-=2) cout<<" "; for(int k=1;k<=i;++k) { cout<<"*"; } cout<<endl; } return 0; }

Ex 4
#include<iostream> using namespace std ; int main () { for (int i=2; i<100; i++) { bool prime=true; for (int j=2; j*j<=i; j++) { if (i % j == 0) { prime=false; break; } } if(prime) cout << i << " "; } cout<<endl; return 0;

Function Ex5
# include<iostream> using namespace std; double summation(double n1, double n2); double substraction(double n1, double n2); double multiply(double n1, double n2); double divition(double n1,double n2); int main() { double num1; double num2; double anwr; int n; cout<<"1.call sum function"<<endl; cout<<"2.call substract function"<<endl; cout<<"3.call multiply function"<<endl; cout<<"4.call divide function"<<endl; cin>>n; cout<<"enter two numbers"<<endl; cin>>num1>>num2; switch (n) { case 1: anwr=summation(num1,num2); break; case 2: anwr= substraction(num1,num2); break; case 3: anwr=multiply(num1,num2); break; case 4 : anwr=divition(num1, num2) ; break; default :

cout<<"good bye"<<endl; } cout<<"The answer is"<<anwr<<endl; } double summation(double n1, double n2) { return n1+n2; } double substraction(double n1, double n2) { return n1-n2; } double multiply(double n1, double n2) { return n1*n2; } double divition(double n1,double n2) { return n1/n2; }

Ex 6
# include<iostream> using namespace std; double cal_change(double price ); int main() { double price; cout<<"Emter the price"<<endl; cin>>price; cal_change(price ); } double cal_change(double value) { int change = int(value*100)%100; int quarters=change/25; int newchange=change-quarters*25; int dimes=newchange/10; newchange=newchange-dimes*10; int nickels=newchange/5; newchange=newchange-nickels*5; int pennies=newchange/1; cout<<"Total change due: "<<change<<" cents"<<endl; cout<<"Your change is"<<endl; cout<<" "<<int (value)<<"dollars"<<endl; cout<<" "<<quarters<< "quarter(s)"<<endl; cout<<" "<<dimes<< "dime(s)"<<endl; cout<<" "<<nickels<< "nickel(s)"<<endl; cout<<" "<<pennies<< "pennie(s)"<<endl; return 0;}

Ex 7
#include<iostream> #include<cmath> using namespace std; bool validate(double NUM1,double NUM2,double N3); double cal_area(double NUM1,double NUM2,double N3); int main() { double a,b,c; cout<<"Enter 3 sides of the traingle :"<<endl; cin>>a>>b>>c; bool possible=validate(a,b,c); if(possible) { double N= cal_area(a,b,c); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout<<"Area is :"<<N<<endl; } else cout<<"Value you enterd are not possible to make traingle"<<endl; return 0; } bool validate(double NUM1,double NUM2,double N3) { double N=NUM1+NUM2; if(N>N3) { return true; } else return false; } double cal_area(double NUM1,double NUM2,double N3) { double area; double S=0.5*(NUM1+NUM2+N3); area=sqrt(S*(S-NUM1)*(S-NUM2)*(S-N3)); return area; }

You might also like