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

Whiteboardsss-3

Summary

Uploaded by

brittamnyazi
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)
8 views

Whiteboardsss-3

Summary

Uploaded by

brittamnyazi
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
You are on page 1/ 15

Write a program in C to

1. Display programming is easy


#include<stdio.h>
int main()
{
printf(“PROGRAMMING IS EASY\n”);
return 0;
}

2. Calculate the average of 3 numbers


#include<stdio.h>
int main()
{
int num1, num2,num3; float avg;
printf(“enter the 3 numbers \n”);
scanf(“%d%d%d”,&num1,&num2,&num3);
avg=(num1+num2+num3)/3;
printf(“the average of 3 numbers =%0.2f \n”,avg);
return 0;
}

3. Calculate the area of a circle


#include<stdio.h>
int main()
{
float radius, area;
const float pie = 3.142;
printf(“enter the radius of the circle \n”);
scanf(“%f”,&radius);
area=pie*radius^2;
printf(“the area of the circle =%0.2f \n”,area);
return 0;
}

C++
#include<iostream>
using namespace std;
int main()
{
cout<<”PROGRAMMING IS EASY\n”;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int x,y,z; float k;
cout<<” enter the 3 numbers”;
cin>>x>>y>>z;
k=(x+y+z)/3;
cout<<” the average of the 3 numbers = ”<< k;
return 0;
}
CONTROL STRUCTURES

1. Selection
a) Simple if statement (only gives an output if the condition met otherwise it
does nothing)
General form: if(condition is true) { output},
Example: #include<iostream>
using namespace std;
int main()
{
float marks;
if(marks>=40)
{
cout<<”passed”;
}
return 0;
}
b) If else statement (Gives an output whether the condition met OR not)
General form: if(condition) { cout<<” true output”;
else cout<<” false output”; }
Example: #include<iostream>
using namespace std;
int main()
{
float marks;
if(marks>=40)
{
cout<<”passed”;
}
else
{
cout<<”failed”;
}
return 0;
}

c) Nested if statement (used when the conditionalities are more than 2, e.g
when grading in an exam)
70 to 100 Distinction
50 to 69 Credit
40 to 49 Pass
0 to 39 Fail
>100 and < 0 Invalid entry

#include<iostream>
using namespace std;
int main()
{
float marks;
cout<<” ENTER MARKS\n”;
cin>>marks;

if(marks>=70&&marks<=100)
cout<<” Distinction\n”;
else if(marks>=50&&marks<=69)
cout<<” Credit\n”;
else if(marks>=40&&marks<=49)
cout<<” Pass\n”;
else if(marks>=0&&marks<=39)
cout<<” Fail\n”;
else cout<<” Invalid Entry\n”;
return 0;
}

2. Sequence
a. Switch statement
3. Iteration
a. for loop

general structure: for(initialize;condition; re-evaluate )


{
true statement block;
}
Example :
#include<iostream>
using namespace std;
int main()
{
int number;
for(number=200;number>=20; number=number-1 )
{
cout<<number<<endl;
}

return 0;
}

#include <iostream>
using namespace std;
int main(){
int score;
cout << "Enter the test score: ";
cin >> score;
switch (score/10) {
case 10:
case 9: cout << 'A' << endl; break;
case 8: cout << 'B' << endl; break;
case 7: cout << 'C' << endl; break;
case 6: cout << 'D' << endl; break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout << 'F' << endl; break;
default: cout << "Error: score is out of range.\n";
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
int marks;
cout << "Enter the test marks: ";
cin >> marks;
switch (marks) {
case (marks>=70&&marks<=100): cout<< “ distinction”<<endl; break;
case (marks>=60&&marks<=69): cout<< “ credit”<<endl; break;
case (marks>=40&&marks<=59): cout<< “ pass”<<endl; break;
case (marks>=0&&marks<=39): cout<< “ fail”<<endl; break;
default: cout << "Invalid Enntry.\n";
}
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int marks;
do {
cout << "Enter student's mark: ";
cin >> marks;

if (marks < 0 || marks > 100) {


cout << "Invalid entry" << endl;
} else {
int category = 0;
if (marks >= 70 && marks <= 100) category = 1;
else if (marks >= 50 && marks <= 69) category = 2;
else if (marks >= 40 && marks <= 49) category = 3;
switch (category) {
case 1:
cout << "Distinction" << endl;
break;
case 2:
cout << "Credit" << endl;
break;
case 3:
cout << "Pass" << endl;
break;
default:
cout << "Fail" << endl;
break;
}
}

} while (true);

return 0;
}
Work: using nested for loop, write a C++ program to display a 10 by 12 multiplication table.
The program must be tested , actually submit the code and the output

cols
1 2 3 4 5 6 7 8 9 10
1 1
2
3
4
5
6
7
8
9
10
11
12

#include<iostream>
using namespace std;
int main()
{
int col, row, colmax, rowmax; colmax=10; rowmax=12;

for(row=1;row<=rowmax;row++)
{
for(col=1;col<=colmax;col++)
{
cout<<row*col<<”\t ”;
}
cout<<endl;
}
return 0;
}

Escape Sequence Description


\n Newline (moves cursor to the next line)
\t Horizontal tab (moves the cursor to the next tab stop)
\r Carriage return (moves the cursor to the beginning of the current line)
\b Backspace (moves cursor one position to the left, erasing the previous character)
\f Form feed (moves the cursor to the start of the next page, typically used in printers)
\a Alert (produces a beep sound or other alert signal)
\\ Backslash (used to represent a backslash character \)
\' Single quote (used to represent a single quote ' inside a character or string literal)
\" Double quote (used to represent a double quote " inside a string literal)
\? Question mark (used to avoid ambiguity in trigraphs)
\v Vertical tab (moves the cursor vertically, often ignored in modern consoles)
\0 Null character (used to terminate C-style strings)

b. while loop

general structure: initialize;


while(condition )
{
true statement block;
re-evaluate;
}

Example: number=10000;
while(number>=50)
{
cout<<number<<endl;
number/=2;
}

c. do .. while loop

general structure: initialize;


{
true statement block;
re-evaluate;
}while(condition );

Example: number=100;
{
cout<<number<<endl;
number+=1;
}while(number>=1000);

number=1000;
{
cout<<number<<endl;
number+=1;
}while(number<=10);

FUNCTIONS – unique sub programs within a program.


-code re-usability

Global and local functions

Types
1. System defined functions int main() sgrt(), printf(), pow(),
2. User defined functions

int averagesss (int a, int b, int c, int d, int e, int f, int g)


{
int(a+b+c+d+e+f+g)/7;
}

x = averagesss(2,4,7,88,56,67,55,44);
y= averagesss(12,23,22,333,555,66,777);
z = averagesss(44,55,9,0,0,0,0);

Declare functions:

Function prototype: returntype functionname(parameter list);

Function body / declaration returntype functionname(parameter list)


{
Function body;
return ();
}

Datatype functionname (parameter list); // function prototype

int maxnum(int a, int b);

float avgmarks(float a, float b, float c);

Datatype functionname (parameter list)

{
function body;
return ();
}
#include<iostream>
using namespace std;
int maxnum(int a, int b)
{
If(a>b)
cout<<” the maximum number is”<<a<<endl;
else
cout<<”the maximum number is”<<b<<endl;
}
int main()
{
int t,u,v,g,j,k;
t=maxnum(22, 54);
u=maxnum(1000, 505);
v=maxnum(2, 5);
g=maxnum(x,y);
j=maxnum(-65, -200);
k=maxnum(22, -54);

cout<< “ the maximum value from function call t is”<<t<<endl;


cout<< “ the maximum value from function call u is”<<u<<endl;
cout<< “ the maximum value from function call v is”<<v<<endl;
cout<< “ the maximum value from function call g is”<<g<<endl;
cout<< “ the maximum value from function call j is”<<j<<endl;
cout<< “ the maximum value from function call k is”<<k<<endl;
return 0;
}
ARRAYS IN C++

-a collection of similar fundamental data types (int, float, double , char-string)

Datatypenamearray[size];

int kenyanvoter2027[23000000];

flaot marks[40];

int BBIT2021CAT1[6];

0 1 2 3 4 5
23 20 28 6 16 20

int BBIT2021CAT1[0] = 23
int BBIT2021CAT1[1] = 20
int BBIT2021CAT1[2] = 28
int BBIT2021CAT1[3] = 6
int BBIT2021CAT1[4] = 16
int BBIT2021CAT1[5] = 20

BBIT2021CAT1[0] = BBIT2021CAT1[0]-3
BBIT2021CAT1[1] = BBIT2021CAT1[1]-3
BBIT2021CAT1[2] = BBIT2021CAT1[2]-3
BBIT2021CAT1[3] = BBIT2021CAT1[3]-3
BBIT2021CAT1[4] = BBIT2021CAT1[4]-3
BBIT2021CAT1[5] = BBIT2021CAT1[5]-3
#include<iostream>
using namespace std;
int main()
{
int BBIT2021CAT1[6] = { 23,20,28,6,16,20};
int n, totmarks, avgmarks; totmarks = 0; avgmarks = 0;
for(n=0;n<6;n++)
{
totmarks = totmarks+BBIT2021CAT1[n];
}

avgmarks = totmarks/6;

cout<< “ the total marks of the array BBIT2021CAT1 is”<< totmarks<<endl;


cout<< “ the average marks of the array BBIT2021CAT1
is”<<avgmarks<<endl;

return 0;
}
#include<iostream>
using namespace std;
int main()
{
int BBIT2021CAT1[6];
int n, totmarks, avgmarks; totmarks = 0; avgmarks = 0;
cout<< “ enter the six values of the array”<<endl;
for(n=0;n<6;n++)
{
cin>>BBIT2021CAT1[n]>>endl;
}
for(n=0;n<6;n++)
{
totmarks = totmarks+BBIT2021CAT1[n];
}

avgmarks = totmarks/6;

cout<< “ the total marks of the array BBIT2021CAT1 is”<< totmarks<<endl;


cout<< “ the average marks of the array BBIT2021CAT1
is”<<avgmarks<<endl;

return 0;
}

You might also like