Lecture Notes in C and C++
Lecture Notes in C and C++
Dennis Ritchie
1979
bell laboratory
C++
enhance version C programming
B, BCPL
#include<header files.h>
main()
{
#include
- pre-processor directive
at column 0
header files
stdio iostream
- I/O commands
printf() - C programming
- monitor - for display/print
syntax:
printf("<statement>");
Tab
\t
Comment
1. Line Comment
//statement
2. Block comment
/*statement*/
Activity
1. Your Initials
FN,MN,LN (first letter only)
- printed horizontally
- in 5x5 dimensions using *
INPUT Command
scanf() - C
keyboard
syntax:
scanf("%<parameter", &<vn>);
cin() - C++
syntx:
cin>>vn;
variable declaration
<dt> <vn>;
Input: 2 numbers
output: sum and average
#include <iostream>
using namespace std;
int main() {
int n1,n2,s;
float ave;
//input
cout<<"Input First Number = ";
cin>>n1;
cout<<"Input Second Number = ";
cin>>n2;
//process
s=n1+n2;
ave=(float)s/2;
//output
cout<<"Sum = "<<s<<endl;
cout<<"Average = "<<ave;
return 0;
}
input: 3 numbers
output:
cube root of the difference of the average of all inputted values
from the square root of the product of 2nd and 3rd values
Amol +15
#include <stdio.h>
#include <math.h>
int main() {
float n1,n2,n3;
float mn,sb,ans;
printf("Input Three Number = ");
scanf("%f %f %f",&n1,&n2,&n3);
//process
mn=(float)pow(n2*n3,0.5);
//mn=(float)sqrt(n2*n3);
sb=(float)(n1+n2+n3)/3;
ans=pow(mn-sb,0.333);
//output
printf("Minuend = %0.2f",mn);
printf("\nSubtrahend = %0.2f",sb);
printf("\nAnswer = %0.2f",ans);
return 0;
}
#include <iostream>
#include <math.h>
using namespace std;
int main() {
float n1,n2,n3;
float mn,sb,ans;
cout<<"Input Three Number = ";
cin>>n1>>n2>>n3;
//process
mn=(float)pow(n2*n3,0.5);
//mn=(float)sqrt(n2*n3);
sb=(float)(n1+n2+n3)/3;
ans=pow(mn-sb,0.333);
//output
cout<<"Minuend = "<<mn;
cout<<"\nSubtrahend = "<<sb;
cout<<"\nAnswer = "<<ans;
return 0;
}
#include <iostream>
using namespace std;
int main() {
char sn[20];
int nou;
float rpu,tf,lf,mf,ttf;
//input
cout<<"Input Student Name = ";
//cin>>sn;
fgets(sn, sizeof(sn), stdin);
cout<<"Input no of units and rate per unit = ";
cin>>nou>>rpu;
//process
tf=nou*rpu;
lf=0.07*tf;
mf=0.3*tf;
ttf=tf+lf+mf;
//output
cout<<"Student Name = "<<sn<<endl;
cout<<"Tuition Fee = "<<tf<<endl;
cout<<"Total Tution Fee = "<<ttf<<endl;
return 0;
}
Conditional Statement
syntax:
<vn> relops <value>
A. if statement
a. if (<condition>)
statement/s;
if (age>19)
cout<<"Adult Stage";
b. else..if statement
if (<condition>)
statement/s;
else
statement/s;
if (x=='a')
cout<<x;
else
cout<<"Invalid Input";
if (<condition1>)
statement/s;
else if (<condition2>)
statement/s;
..
..
..
else
statement/s;
if (noa==1)
att=100;
else if (noa==2)
att=90;
else if (noa==3)
att=80;
else
att=0;
B. Switch statement
syntax:
int num,k;
//input
cout<<"Input Number = ";
cin>>num;
//process and output
//if (num%2==0)
// cout<<num<<" is an even number";
//else
//cout<<num<<" is an odd number";
k=num%2;
switch (k)
{
case 0:cout<<num<<" is an even number";break;
case 1:cout<<num<<" is an odd number";break;
}
return 0;
}
#include <stdio.h>
int main() {
int num;
//input
printf("Input Number = ");
scanf("%d",&num);
//process and output
if (num%2==0)
printf("%d is an even number",num);
else
printf("%d is an odd number",num);
return 0;
}