Decision Making Looping Statements Functions in C
Decision Making Looping Statements Functions in C
Decision Making Looping Statements Functions in C
#Program 1
// Create a function
void myFunction() {
cout << "I just got executed!";
}
int main()
{
myFunction(); // call the function
return 0;
}
#Program 2
void myFunction() {
cout << "I just got executed!\n";
}
int main()
{
myFunction();
myFunction();
myFunction();
return 0;
}
#Program 3 [Problem]
int main()
{
myFunction();
return 0;
}
void myFunction() {
cout << "I just got executed!";
}
// Error
#Program 4 [Solution]
// Function declaration
void myFunction();
// Function definition
void myFunction() {
cout << "I just got executed!";
}
#Program 5
int main() {
myFunction(10);
myFunction("Ramsheth");
myFunction("Balbharti");
return 0;
}
#Program 6
int myFunction(int x)
{
return 5 + x;
}
int main() {
cout << myFunction(3);
return 0;
}
// Outputs 8
#Program 7
#include <iostream>
using namespace std;
int sum(int a, int b)
{
int result;
result = a + b;
cout << "Total value is :" << result << endl;
}
int main ()
{
// calling a function to add the values.
sum(100,200);
return 0;
}
// Outputs
Total value is :300
#Program 8
Output
Value of X before modifying: 3
Value of X after modifying: 4
if(2<3)
{
cout << "2 is less than three";
}
#Program 2
if(2>3)
{
cout<< "2 is greater than 3";
}
else
{
cout<< "3 is greater than 2";
}
#Program 3
if(2>3)
{
cout<< "2 is greater than 3";
}
else if(2==3)
{
cout<< "2 is equal to 3";
}
else
{
cout<< "3 is greater than 2";
}
#Program 4
Int a=7
switch (a) {
case 9:
cout << "Freshman\n";
break;
case 10:
cout << "Sophomore\n";
break;
case 11:
cout << "Junior\n";
break;
case 12:
cout << "Senior\n";
break;
default:
cout << "Invalid\n";
break;
}
#Program 5
#Program 6
#Program 7
Write a program in C++ to accept a grade and declare the equivalent description
:
Grade Description
E Excellent
V Very Good
G Good
A Average
F Fail
Loops
#Program1
int main()
{
int n = 7;
cout << findSum(n);
return 0;
}
#Program2
int i;
for (i = 1; i <= 11; i++)
{
cout<<i;
}
#Program3
int i;
for (i = 1; i >= 11; i++)
{
cout<<i;
}
#Program4
int a=5;
for (int i = 1; i < 11; i++)
{
cout<<a*i;
}
#Program5
int a=5;
string z;
cout<<”enter your name”;
cin>>z;
while(a<10)
{
cout<<z;
}
#Program6
int a=5;
string z;
cout<<”enter your name”;
cin>>z;
while(a<10)
{
cout<<z;
a++;
}
#Program7
int a=5;
string z;
cout<<”enter your name”;
cin>>z;
while(a<10)
{
cout<<z;
a- -;
}
#Program8
int a=5;
do
{
cout<<a;
a++;
}while(a<10);
#Program9
int a=5;
string z;
cout<<”enter your name”;
cin>>z;
do
{
cout<<a;
a++;
}while(a>10);
#Program10
int a=15;
string z;
cout<<”enter your name”;
cin>>z;
do
{
cout<<a;
a++;
}while(a>10);
Class Notes
Programs to revise
1. Write c++ program to accept number and check whether it is positive or negative.
#include <iostream>
using namespace std;
int main()
{
int num = 96;
if (num > 0)
{
cout << "The number is positive";
}
else
{
cout << "The number is negative";
}
return 0;
}
2. Write c++ program to accept number and Check Whether a Number is Even or
Odd.
#include <iostream>
using namespace std;
int main ()
{
int number;
cout << "Enter a number:";
cin >> number;
if (number % 2 == 0)
{
cout << number << " : Even";
}
else
{
cout << number << " : Odd";
}
return 0;
}
3. Write a C++ Program to find the maximum between three numbers. Using the
switch statement.
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Looping Statements
4. Write a program to print the multiplication table of any given number.
#include <iostream>
int main()
int n;
cin >> n;
cout << n << " * " << i << " = " << n * i << endl;
return 0;
}
5. c++ program to print 1 to 10 numbers
#include <iostream>
int main()
for(int i=1;i<=10;i++) //for loop used to increament each time loop runs.
cout<<i<<endl;
return 0;
#include <iostream>
int main ()
int i,sum = 0;
for(i=1;i<=10;i++)
sum = sum + i;
}
cout<<"The sum of 1 to is";
return 0;
Functions
7. c++ program to returns max of two input numbers
#include <iostream>
if (x > y)
return x;
else
return y;
// returns integer
int main()
#include <iostream>
// display a number
int main()
int num1 = 5;
displayNum(num1, num2);
return 0;