0% found this document useful (0 votes)
26 views18 pages

Decision Making Looping Statements Functions in C

Uploaded by

romitgaikwad
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)
26 views18 pages

Decision Making Looping Statements Functions in C

Uploaded by

romitgaikwad
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/ 18

Functions

#Program 1

// Create a function
void myFunction() {
cout << "I just got executed!";
}
int main()
{
myFunction(); // call the function
return 0;
}

// Outputs "I just got executed!"

#Program 2

void myFunction() {
cout << "I just got executed!\n";
}
int main()
{
myFunction();
myFunction();
myFunction();
return 0;
}

// I just got executed!


// I just got executed!
// I just got executed!

#Program 3 [Problem]

int main()
{
myFunction();
return 0;
}
void myFunction() {
cout << "I just got executed!";
}
// Error
#Program 4 [Solution]

// Function declaration
void myFunction();

// The main method


int main() {
myFunction(); // call the function
return 0;
}

// Function definition
void myFunction() {
cout << "I just got executed!";
}

#Program 5

void myFunction(int fname)


{
cout << fname << " School\n";
}

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

// C++ Program to demonstrate


// Call by value
#include <iostream>
using namespace std;
// Function to change value of x
int fun(int x)
{
x = x + 1;
return x;
}
int main()
{
int x = 3;
cout << "Value of X before modifying: " << x << endl;
x = fun(x);
cout << "Value of X after modifying: " << x << endl;
return 0;
}

Output
Value of X before modifying: 3
Value of X after modifying: 4

Decision Making Statements


#Program 1

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

Write a C++ program to check whether an alphabet is a vowel or a consonant.

#Program 6

Write a C++ program to calculate profit and loss on a transaction.


Test Data :
CP =500, SP=700
Expected Output :
You can booked your profit amount : 200

#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

Write a Program to Find the Sum of the First N Natural Numbers

// C++ program to find


// Sum of first
// n natural numbers.
#include <iostream>
using namespace std;

// Function to find sum


int findSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i;
return sum;
}

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

Decision Making Statements

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()
{

double n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

// check if n1 is the largest number


if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;

// check if n2 is the largest number


else if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;

// if neither n1 nor n2 are the largest, n3 is the largest


else
cout << "Largest number: " << n3;

return 0;
}
Looping Statements
4. Write a program to print the multiplication table of any given number.

#include <iostream>

using namespace std;

int main()

int n;

cout << "Enter a positive integer: ";

cin >> n;

// run a loop from 1 to 10

// print the multiplication table

for (int i = 1; i <= 10; ++i) {

cout << n << " * " << i << " = " << n * i << endl;

return 0;

}
5. c++ program to print 1 to 10 numbers

#include <iostream>

using namespace std;

int main()

cout<<"The first 10 numbers are as follows: \n";

for(int i=1;i<=10;i++) //for loop used to increament each time loop runs.

cout<<i<<endl;

return 0;

6. c++ program to print 1 to 10 numbers

#include <iostream>

using namespace std;

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

// C++ Program to demonstrate working of a function

#include <iostream>

using namespace std;

// Following function that takes two parameters 'x' and 'y'

// as input and returns max of two input numbers

int max(int x, int y)

if (x > y)

return x;

else

return y;

// main function that doesn't receive any parameter and

// returns integer

int main()

int a = 10, b = 20;

// Calling above function to find max of 'a' and 'b'

int m = max(a, b);

cout << "m is " << m;


return 0;

8. c++ program to print a text

// program to print a text

#include <iostream>

using namespace std;

// display a number

void displayNum(int n1, float n2) {

cout << "The int number is " << n1;

cout << "The double number is " << n2;

int main()

int num1 = 5;

double num2 = 5.5;

// calling the function

displayNum(num1, num2);

return 0;

You might also like