0% found this document useful (0 votes)
3 views6 pages

Lecture Notes in C and C++

The document provides an overview of C and C++ programming, including syntax, data types, input/output commands, and basic programming structures. It includes examples of code for various operations such as summing numbers, calculating averages, and using conditional statements. Additionally, it covers specific functions and commands for handling user input and output in both languages.

Uploaded by

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

Lecture Notes in C and C++

The document provides an overview of C and C++ programming, including syntax, data types, input/output commands, and basic programming structures. It includes examples of code for various operations such as summing numbers, calculating averages, and using conditional statements. Additionally, it covers specific functions and commands for handling user input and output in both languages.

Uploaded by

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

C programming

Dennis Ritchie
1979
bell laboratory

C++
enhance version C programming

B, BCPL

Structure C++ Program

#include<header files.h>
main()
{

#include
- pre-processor directive
at column 0

header files
stdio iostream
- I/O commands

conio - consul i/o


- screen commands
string
math
ctype

printf() - C programming
- monitor - for display/print
syntax:
printf("<statement>");

cout() - C++ programming


syntax:
cout<<"statement";

New Line Command


\n
next row but column 0

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 *

2. print your favorite anime


character

INPUT Command
scanf() - C
keyboard
syntax:
scanf("%<parameter", &<vn>);

cin() - C++
syntx:
cin>>vn;

variable declaration
<dt> <vn>;

parameters data types variable declaration


%d integer -65000 - + 65000 int x; scanf("%d",&x); cin>>x;
%/d long integer -inf - + inf long int y; scanf("%/d", &y); cin>>y;
%f float -65000.00 - + 65000.00 float z; scanf("%f", &z); cin>>z;
%/f double -inf.00 - + inf.00 double w; scanf("%/f",&w); cin>>w;
%c char char m scanf("%c",&m); cin>>m;
%s string char n[20]; scanf("%s",&n);

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;
}

3. Input: stud name, nou, rate per unit


output: TF, TTF
computations:
lab fee = 7% of tf
misc fee = 30% of tf

gets() - get string


syntax:
gets(<vn>);

fgets() - file get string


fgets(<vn>, sizeof(<vn>), stdin);

#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";

c. Nested else..if statement

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:

switch (<vn>) switch (x) => switch (x==1)


{
case <value1>: statement/s;break;
case <value2>: statement/s;break;
..
..
case <valuen>: statement/s;break;
default:
statement/s;
}

switch (noa) noa=2


{
case 1:att=100;break;
case 2:att=90;break;
case 3:att=80;break;
default:att=0;
}

ex: input: number


output: even or odd number
#include <iostream>
using namespace std;
int main() {

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;
}

You might also like