0% found this document useful (0 votes)
11 views

Programming Experiment

Uploaded by

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

Programming Experiment

Uploaded by

akliluasebot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Madda Walabu University

College of Computing
Department of Computer Science
Computer Programming
Laboratory Practice
Lab. Inst. Andualem D.
Experiment 1/cout Experiment 4/subtraction
#include<iostream> #include<iostream>
using namespace std; using namespace std;
#include<conio.h> #include<conio.h>
int main() { int main() {
cout<<"Hello World\n"; int m,n,sub;
cout<<"This is my first C++ programming"; cout<<"This program will calculate the Two
getch(); numbers"<<endl;
return 0; cout<<"pls enter the value of the m and n number\n";
} cin>>m>>n;
Experiment 2/cout sub=m-n;
#include<iostream> cout<<"The sub of Two numbers is:\t"<<sub<<endl;
using namespace std; getch ();
#include<conio.h> return 0;}
int main() { Experiment 5/multiplication
cout<<"\tMy Name is:- Mr. Johan Bruz\n"; #include<iostream>//library file
cout<<"\tSex:-Male"<<endl; #include<conio.h>
cout<<"\tAge:- 25\n"; using namespace std;
cout<<"\tAddress: Austria”<<endl; int main(){
getch(); int g,h,mult;
return 0; cout<<"This program will calculate the Two
Experiment 3/addition numbers"<<endl;
#include<iostream>//library file cout<<"pls enter the value of the g and h number\n";
#include<conio.h> cin>>m>>n;
using namespace std; mult=g*h;
int main(){ cout<<"The product of Two numbers
int m,n,sum; is:\t"<<mult<<endl;
cout<<"This program will calculate the Two getch ();
numbers"<<endl; return 0;
cout<<"pls enter the value of the m and n number\n"; }
cin>>m>>n; Experiment 6/division
sum=m+n; #include<iostream>//library file
cout<<"The sum of Two numbers is:\t"<<sum<<endl; #include<conio.h>
getch(); using namespace std;
getch(); int main(){
return 0; int j,f;
} float div;
cout<<"This program will calculate the Two
numbers"<<endl;
cout<<"pls enter the value of the j and f number\n";
cin>>j>>f;
div=j/f;
cout<<"The division of Two numbers is:\t"<<div<<endl;
getch ();

Compiled By:- Andualem D. Computer Programming pg. 1


return 0;//end of the terminator of program div=m/n;
} mod=m%n;
Experiment 7/average cout<<"The result of sum is:\t"<<sum<<endl;
#include<iostream>//library file cout<<"The result of sub is:\t"<<sub<<endl;
#include<conio.h> cout<<"The result of avg is:\t"<<avg<<endl;
using namespace std; cout<<"The result of mult is:\t"<<mult<<endl;
int main(){ cout<<"The result of div is:\t"<<div<<endl;
int m,n; cout<<"The result of mod is:\t"<<mod<<endl;
float,avg; getch ();
cout<<"This program will calculate the Two return 0;//end of the terminator of program
numbers"<<endl; }
cout<<"pls enter the value of the m and n number\n"; Experiment 10/smallest number
cin>>m>>n; #include<iostream>
avg=(m+n)/2; using namespace std;
cout<<"The average of Two numbers #include<conio.h>
is:\t"<<avg<<endl; int main (){
getch (); int a,b,c,min;
return 0; cout<<"pls enter the 1st numbers\n";
} cin>>a;
Experiment 8/mod cout<<"pls enter the 2nd numbers\n";
#include<iostream>//library file cin>>b;
#include<conio.h> cout<<"pls enter the 3rd numbers\n";
using namespace std; cin>>c;
int main(){ min=a<b?(a<c?a:c):(b<c?b:c);
int t,r,mod; cout<<"the smallest number is="<<min<<endl;
cout<<"This program will calculate the Two getch();
numbers"<<endl; return 0;
cout<<"pls enter the value of the t and r number\n"; }
cin>>t>>r; Experiment 11/largest number
mod=t%r; #include<iostream>
cout<<"The mod of Two numbers is:\t"<<mod<<endl; using namespace std;
getch (); #include<conio.h>
return 0; int main (){
} int a,b,c,max;
Experiment 9/All operators(+ ,-, /, x, %) cout<<"pls enter the 1st numbers\n";
#include<iostream>//library file cin>>a;
#include<conio.h> cout<<"pls enter the 2nd numbers\n";
using namespace std; cin>>b;
int main()//function cout<<"pls enter the 3rd numbers\n";
{ cin>>c;
int m,n,sum,sub,avg,mult,mod; max=a>b?(a>c?a:c):(b>c?b:c);
float div; cout<<"the largest number is="<<max<<endl;
cout<<"This program will calculate the Two getch();
numbers"<<endl; return 0;
cout<<"pls enter the value of the m and n number\n"; }
cin>>m>>n;
sum=m+n;
sub=m-n;
avg=sum/2;
mult=m*n;

Compiled By:- Andualem D. Computer Programming pg. 2


Experiment 12/max and min Experiment 15/Area and circumference
#include<iostream> #include<iostream.h>
#include<conio.h> #include<conio.h>
using namespace std; int main (){
int main(){ float radius;
double a,b,c; float circumference;
double Maximum, Minimum; float area;
cout<<"enter the a Value\t\n"; cout << "Please enter the radius of a circle: ";
cin>>a; cin >> radius;
cout<<"enter the b Value\t\n"; cout << "\n";
cin>>b; circumference = 2 * 3.1416 * radius;
cout<<"enter the C Value\t\n"; area = 3.1416 * radius * radius;
cin>>c; cout << "*Area and Circumference of A Circle*" << "\n"
Maximum=a>b?(a>c?a:c):(b>c?b:c); cout << "\tRadius= " << radius << "\n"
Minimum=a<b? (a<c?a:c):(b<c?b:c); cout << "\tArea= " << area << "\n"
cout<<"The Maximum number is:"<<Maximum<<endl; cout << "\tCircumference= " << circumference << "\n";
cout<<"The Minimum number is:"<<Minimum<<endl; getch();
getch(); return 0;
return 0; }
} Experiment 16/number double itself
Experiment 13/assign #include <iostream>
#include <iostream> using namespace std;
using namespace std; #include<conio.h>
#include<conio.h> int main ()
int main (){ {
int a, b, result; Int i;
a = 5; cout<< "Please enter an integer value: ";
b = 2; cin>>i;
a = a + 1; cout<< "The value you entered is “ <<i;
result = a - b; cout<< " and its double is " <<i*2 << ".\n";
cout<< result; getch();
getch(); return 0;
return 0; }
} Experiment 17/Area of Circle
Experiment 14/Area #include<iostream>
#include<iostream> #include<conio.h>
using namespace std; #define PI 3.14
#include<conio.h> using namespace std;
int main(){ int main()
int width; {
int length; int radius;
int area; float area;
width=100; cout<<"enter the of radius:"<<radius<<"\n";
length=50; area=PI*radius*radius;
area=width*length; cout<<"area of circle is:"<<area;
cout<<”The area is:”<<area; getch();
return 0; return 0;
} }

Compiled By:- Andualem D. Computer Programming pg. 3


Experiment 18/ Prefix and Postfix: Experiment 21/switch operator
#include<iostream> #include<iostream>
#include<conio.h> using namespace std;
using namespace std; #include<conio.h>
int main(){ int main(){
int a=10; int Num1, Num2;
cout<<"pre increament :"<<++a<<"\n"; char optr;
cout<<"post increament:"<<a++<<"\n"; cout <<"enter two numbers"<<endl;
cout<<"pre decreament :"<<--a<<"\n"; cin >> Num1>>Num2;
cout<<"post decreament:"<<a--<<"\n"; cout <<"enter one of the these opts '+' , '*' , '/' , '%'
getch(); "<<endl;
return 0; cin>> optr;
} switch (optr){
Experiment 19/size of data type case '+':
#include <iostream> cout<<Num1 + Num2;
#include<conio.h> break;
using namespace std; case '-':
int main(){ cout<<Num1 - Num2;
int a,b,c; break;
a = sizeof(char); case '*':
b = sizeof(int); cout<<Num1 * Num2;
c = sizeof(double); break;
cout<<"Size of char\t"<<a<<endl; case '/':
cout<<"Size of int\t"<<b<<endl; cout<<Num1 / Num2;
cout<<"Size of double\t"<<c<<endl; break;
getch(); default:
return 0; cout<<"unknown opt:"<<optr;
} break;
Experiment 20/Revers number }
#include<iostream> getch();
using namespace std; return 0;
#include<conio.h> }
int main(){ Experiment 22/if else
int a,b,d=0; #include<iostream>
cout<<"enter any number:"; using namespace std;
cin>>a; #include<conio.h>
while(a>0){ int main()
b=a%10; {
a=a/10; int age;
d=(d*10)+b; cout<<"Pls Enter your age\n";
} cin>>age;
cout<<"revers is:"<<d; if(age>18){
getch(); cout<<"you are adult";
return 0; }
} else
cout<<"you are a kid";
getch();
return 0;
}

Compiled By:- Andualem D. Computer Programming pg. 4


Experiment 23/if else Experiment 25/for loop sum
#include<iostream> #include<iostream>
using namespace std; using namespace std;
#include<conio.h> #include<conio.h>
int main(){ int main(){
int i; int n, Sum=0;
cout<<"show Even and Odd number\n"; cout<<"Enter any number"<<endl;
cout<<"Enter integer:\n"; cin>>n;
cin>>i; for(int i=0; i<=n;i++)
if(i%2==0) Sum=Sum+i;
cout<<"The number is Even"; cout<<"The Sum is: "<<Sum;
else getch();
cout<<"The number is Odd"; return 0;
getch(); }
return 0; Experiment 26/for loop
} #include<iostream>
Experiment 24/if else if using namespace std;
#include<iostream> #include<conio.h>
using namespace std; int main(){
#include<conio.h> for(int i=0; i<=10;i++){
int main(){ cout<<"#"<<" ";
int grade; }
cout<<"enter your grade\n"; cout<<endl;
cin>>grade; getch();
if(grade>100) return 0;
{ }
cout<<"your score is: A"; Experiment 27/for loop pattern
} #include<iostream>
else if(grade>85) using namespace std;
{ #include<conio.h>
cout<<"your score is: B"; int main(){
} for(int i=1; i<=10;i++){
else if(grade>70){ for(int j=1; j<=i;j++){
cout<<"your score is: C"; cout<<"#"<<" ";}
} cout<<endl;}
else if(grade>50){ getch();
cout<<"your score is: D"; return 0;
} }
else Experiment 28/continue
cout<<"your score is: F"; #include<iostream>
getch(); #include<conio.h>
return 0; using namespace std;
} int main(){
for(int n=10;n>0;n--){
if(n==5)
continue; //cause a jump to n--
cout<<n<<",";}
getch();
return 0;
}

Compiled By:- Andualem D. Computer Programming pg. 5


Experiment 29/break sum=0;
#include<iostream> while(number<=100){
#include<conio.h> sum+=number;
using namespace std; number++;
int main(){ }
for(int n=10;n>0;n--){ cout<<sum;
cout<<n<<","; getch();
if(n==3){ return 0;
cout<<"count down aboart!!"; }
break; Experiment 33/do while
}} #include<iostream>
getch(); using namespace std;
return 0; #include<conio.h>>
} int main()
Experiment 30/pattern character {
#include<iostream> int b;
#include<conio.h> b=1;
using namespace std; do{
int main() cout<<b<<endl;
{ b++;
char i,j; }
for(int i='A';i<='H';i++){ while(b<=15);
for(int j='A';j<=i;j++){ getch();
cout<<char(i)<<" "; return 0;
} }
cout<<endl; Experiment 34/do while
} #include<iostream>
getch(); using namespace std;
return 0; #include<conio.h>>
} int main()
Experiment 31/whileloop {
#include<iostream> int number = 0;
#include<conio.h> int sum = 0;
using namespace std; do {
int main(){ sum += number;
int x; cout << "Enter a number: ";
x=1; cin >> number;
while(x<=10){ }
cout<<x<<endl; while (number >= 0);
x++;} cout << "\nThe sum is " << sum << endl;
getch(); getch();
return 0; return 0;
} }
Experiment 32/while sum
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
int number,sum;
number=1;

Compiled By:- Andualem D. Computer Programming pg. 6


Experiment 35/do while Experiment 38/factorials
#include<iostream> #include<iostream>
using namespace std; #include<conio.h>
#include<conio.h>> using namespace std;
int main(){ int main(){
// Local variable float fact,y;
int x = 1; cout<<"This program will calculate factorial
do { number\n";
cout << "X is: " << x << endl; cout<<"enter the value of y\n";
x = x + 1; cin>>y;
} while (x < 5); fact=1;
getch(); while (y>=1){
return 0; fact=fact*y;
} y=y-1;
Experiment 36/do while }
#include<iostream> cout<<"Factorial Number result is:"<<fact;
using namespace std; getch();
#include<conio.h>> return 0;
int main(){ }
int a = 1; Experiment 39/Array
do { #include<iostream>
int b = 1; #include<conio.h>
do { using namespace std;
cout << a << "\n"; int main(){
b++; char mybuffer [100];
} while (b <= 3); cout << "What's your name? ";
a++; cin.getline (mybuffer,100);
} while (a <= 3); cout << "Hello " << mybuffer << ".\n";
getch(); cout << "Which is your favourite team? ";
return 0; cin.getline (mybuffer,100);
} cout << "I like " << mybuffer << " too.\n";
Experiment 37/do while multiplication getch();
#include<iostream> return 0;
using namespace std; }
#include<conio.h> Experiment 40/Array
using namespace std; #include<iostream>
int main(){ #include<conio.h>
int n, count = 0, limit; using namespace std;
cout << "Enter the value to find the multiplication int main(){
table: "; struct cd_collection
cin >> n; {
cout << "Enter the maximum limit for multiplication char title[25];
table: "; char artist[20];
cin >> limit; int num_songs;
do{ float price;
cout << n << "*" << count << " = " << n*count <<endl; char date_purchased[9];
count++;} } cd1 = {"Red Moon Men","Sams and the Sneeds", 12,
while(count <= limit); 11.95,"08/13/93"};
return 0; cout<<"\n Here is the info about cd1"<<endl;
} cout<<cd1.title<<endl;

Compiled By:- Andualem D. Computer Programming pg. 7


cout<<cd1.artist<<endl; return 0;
cout<<cd1.num_songs<<endl; }
cout<<cd1.price<<endl; Experiment 43/Array do
cout<<cd1.date_purchased<<endl; #include<iostream>
getch(); using namespace std;
return 0; #include<conio.h>
} using namespace std;
Experiment 41/Array int main(){
#include<iostream> int i = 0;
using namespace std; int array[] = {2,7,19,5,8};
#include<conio.h> do{
int main (){ cout << array[i] << endl;
int arr[5]; i++;
cout<<"Enter the First element for the array\n"; }while(i <= 4);
cin>>arr[0]; return 0;
cout<<"Enter the Second element of the array\n"; }
cin>>arr[1]; Experiment 44/Pointer
cout<<"Enter the Third element\n"; #include<iostream>
cin>>arr[2]; #include<conio.h>
cout<<"Enter the Fourth element\n"; using namespace std;
cin>>arr[3]; int main (){
cout<<"Enter the Fifth element\n"; int value1 = 5, value2 = 15;
cin>>arr[4]; int *p1, *p2;
cout<<"The inserted values of the array are\n"; p1 = &value1; // p1 = address of value1
cout<<arr[0]<<"\t"<<arr[1]<<"\t"<<arr[2]<<"\t"<<arr[3 p2 = &value2; // p2 = address of value2
]<<"\t"<<arr[4]; *p1 = 10; // value pointed to by p1=10
getch(); *p2 = *p1; // value pointed to by p2= value
return 0; p1 = p2; // p1 = p2 (pointer value copied)
} *p1 = 20; // value pointed to by p1 = 20
Experiment 42/Array forloop cout<<"value1=="<< value1<< "/ value2=="<<value2;
#include<iostream> getch();
using namespace std; return 0;
#include<conio.h> }
int main (){ Experiment 45/Pointer and Array
int A[10]; #include<iostream>
for (int i=0;i<=9;i++){ #include<conio.h>
cin>>A[i]; using namespace std;
} int main(){
int temp; int a[5] = {2,4,6,8,22};
for (int i=0;i<=9;i++){ int *p = a;
for (int j=0;j<=9;j++){ int i = 0;
if(A[i]<A[j]){ cout<<a[i]<<" "<<*p;
temp=A[i]; getch();
A[i]=A[j]; return 0;
A[j]=temp; }
}}}
for (int i=0;i<=9;i++){
cout<<A[i]<<"\t";
}
getch();

Compiled By:- Andualem D. Computer Programming pg. 8


Experiment 46/Structure Experiment 48/Structure
#include<iostream> #include<iostream>
#include<conio.h> #include<conio.h>
using namespace std; using namespace std;
int main(){ struct Person
struct student {
{ char name[50];
int roll_no; int age;
char name[30]; float salary;
int age; }
int marks; int main(){
}; Person p1;
struct student p1 = {1,"Brown",14,78}; cout << "Enter Full name: ";
cout<<p1.roll_no<<" "<< p1.name<<" "<<p1.age<<" cin.get(p1.name, 50);
"<<p1.marks<<"\n"; cout << "Enter age: ";
getch(); cin >> p1.age;
return 0; cout << "Enter salary: ";
} cin >> p1.salary;
Experiment 47/Structure cout << "\nDisplaying Information." << endl;
#include<iostream> cout << "Name: " << p1.name << endl;
#include<conio.h> cout <<"Age: " << p1.age << endl;
using namespace std; cout << "Salary: " << p1.salary;
struct stu{ getch();
int roll; return 0;
float marks; }
char name[50]; Experiment 49/Function
} #include<iostream>
p[10]; #include<conio.h>
int main(){ using namespace std;
cout<<"please enter information of students: class Student {
"<<endl<<endl; public:
for (int i=0; i<3; ++i){ double marks1, marks2;
p[i].roll = i+1; };
cout<<"For Roll no. "<<p[i].roll<<":"<<endl; // function that returns object of Student
cout<<"please enter name: "; Student createStudent() {
cin>>p[i].name; Student student;
cout<<"please enter marks: "; // Initialize member variables of Student
cin>>p[i].marks; student.marks1 = 96.5;
cout<<endl;} student.marks2 = 75.0;
cout<<endl<<"showing information: "<<endl; // print member variables of Student
for(int i=0; i<3; ++i){ cout << "Marks 1 = " << student.marks1 << endl;
cout<<"Roll No.: "<<i+1<<endl; cout << "Marks 2 = " << student.marks2 << endl;
cout<<"Nmae: "<<p[i].name<<endl; return student;
cout<<"Marks: "<<p[i].marks<<endl<<endl; }
} int main() {
system("pause"); Student student1;
} // Call function
getch(); student1 = createStudent();
return 0; getch();
} return 0;}

Compiled By:- Andualem D. Computer Programming pg. 9


Experiment 50/Function // change value of var to 16
#include<iostream> *pointVar = 16;
#include<conio.h> cout << "var = " << var << endl;
using namespace std; // print *pointVar
class Student { cout << "*pointVar = " << *pointVar << endl;
public: getch();
double marks; return 0;}
// constructor to initialize marks Experiment 53/ references
Student(double m) { #include<iostream>
marks = m; #include<conio.h>
}}; using namespace std;
// function that has objects as parameters int main() {
void calculateAverage(Student s1, Student s2) { int a=10;
// calculate the average of marks of s1 and s2 int &value=a;
double average = (s1.marks + s2.marks) / 2; std::cout << value << std::endl;
cout << "Average Marks = " << average << endl;} getch();
int main() { return 0;}
Student student1(88.0), student2(56.0); Experiment 54/ references
// pass the objects as arguments #include<iostream>
calculateAverage(student1, student2); #include<conio.h>
getch(); using namespace std;
return 0;} int main() {
Experiment 51/Pointer int a=70; // variable initialization
#include<iostream> int &b=a;
#include<conio.h> int &c=a;
using namespace std; std::cout << "Value of a is :" <<a<< std::endl;
int main(){ std::cout << "Value of b is :" <<b<< std::endl;
int var1 = 3; std::cout << "Value of c is :" <<c<< std::endl;
int var2 = 24; getch();
int var3 = 17; return 0;}
cout << "Address of var1: "<< &var1 << endl; Experiment 55/ references
cout << "Address of var2: " << &var2 << endl; #include<iostream>
cout << "Address of var3: " << &var3 << endl; #include<conio.h>
getch(); using namespace std;
return 0;} int main() {
Experiment 52/ Pointed by Pointers int a=9; // variable initialization
#include<iostream> int b=10; // variable initialization
#include<conio.h> swap(a, b); // function calling
using namespace std; std::cout << "value of a is :" <<a<< std::endl;
int main() { std::cout << "value of b is :" <<b<< std::endl;
int var = 5; return 0; }
int* pointVar; void swap(int &p, int &q) // function definition
pointVar = &var; {
cout<<"var = " <<var <<endl; int temp; // variable declaration
cout<<"*pointVar = " << *pointVar <<endl<<endl; temp=p;
cout<<"Changing value of var to 7:" << endl; p=q;
var = 7; q=temp;
cout<<"var = " << var << endl; }
cout<<"*pointVar = " << *pointVar<<endl<<endl;
cout<<"Changing value of *pointVar to 16:"<< endl;

Compiled By:- Andualem D. Computer Programming pg. 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy