0% found this document useful (0 votes)
16 views9 pages

CPP Day 1 - Programming

The document provides an introduction to programming basics in C++, including input/output, data types, decision making, loops, and functions. It includes code examples to demonstrate basic programming concepts like checking if a number is positive/negative, even/odd, finding the greatest of two or three numbers, calculating sums, factorials and powers using loops, checking if a number is prime, and defining functions to calculate the area of a square.

Uploaded by

deshmukhanurag13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
16 views9 pages

CPP Day 1 - Programming

The document provides an introduction to programming basics in C++, including input/output, data types, decision making, loops, and functions. It includes code examples to demonstrate basic programming concepts like checking if a number is positive/negative, even/odd, finding the greatest of two or three numbers, calculating sums, factorials and powers using loops, checking if a number is prime, and defining functions to calculate the area of a square.

Uploaded by

deshmukhanurag13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

1

Day 1: Programming Basics


Program in C++

#include <iostream>
using namespace std;

int main()
{

return 0;
}

Input and Output in C++

#include <iostream>
using namespace std;

int main()
{
int a;
cin >> a;

cout << a;
return 0;
}

Data Types
1. Int
2. Char
3. Float
4. Double
Operators in C++
1. Addition ( + )
2. Subtraction ( - )
3. Multiplication (*)
4. Division ( / )
5. Modulo Division ( % )

2023-24
2

Decision Making in C++


If / else
if(condition)
{
// Statements to execute if
// condition is true
}

Check if a Number is Positive or Negative

#include <iostream>
using namespace std;

int main()
{
int num = 15;

// Condition
if (num > 0)
cout << "Positive";
else if (num < 0)
cout << "Negative";
else
cout << "Zero";

return 0;
}

2023-24
3

Check if a Number is Even or Odd

#include <iostream>
using namespace std;
int main ()
{
int num = 14;

//checking whether the number is even or odd


if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";

return 0;
}

Greatest of Two Numbers

#include <iostream>
using namespace std;

int main ()
{
int num1 = 15, num2 = 20;

if (num1 > num2)


cout << num1 << " is greater" ;
else if ( num1 < num2)
cout << num2 << " is greater" ;
else
cout << "Both are Equal";

return 0;
}

2023-24
4

Greatest of Three Numbers

#include <iostream>
using namespace std;
int main ()
{
int first = 10, second = 20, third = 30;

//comparing first with other numbers


if ((first >= second) && (first >= third))
cout << first << " is the greatest ";

//comparing Second with other numbers


else if ((second >= first) && (second >= third))
cout << second << " is the greatest";

else
cout << third << " is the greatest";

return 0;
}

2023-24
5

Loops in C++
1. For Loop
for (initialization; condition; increment)
{
// body of the loop
// statements we want to execute
}

#include <iostream>
using namespace std;

int main()
{
for (int i = 0; i < 5; i++)
{
cout << "Hello World\n";
}
return 0;
}

Sum of First N Natural Numbers

#include<bits/stdc++.h>
using namespace std;

int main()
{
int n = 5;

int sum=0;

// from 1 to n
for(int i=1; i<=n; i++)
{
sum = sum + i;
}

cout << sum;

return 0;
}

2023-24
6

Factorial of a Number

#include <iostream>
using namespace std;

int main()
{
int n = 5;
int fact = 1;

for(int i = 1; i <= n; ++i)


{
fact = fact * i;
}
cout << "Factorial is "<< fact;

return 0;
}

Power of a Number

#include <iostream>
using namespace std;

int main()
{
int num = 2;
int power = 5;

int ans = 1;

for(int i=0; i < power; i++)


{
ans = ans * num;
}

cout << ans ;

return 0;
}

2023-24
7

Prime Number

#include <bits/stdc++.h>
using namespace std;

int main()
{
int num = 13;
// 1 and the number itself
int divisors = 2;

for ( int i= 2; i < num; i++)


{
if( num % i == 0)
{
divisors ++;
}
}

if( divisors == 2 )
cout<< "It is Prime";
else
cout<< "It is not Prime";
return 0;
}

O(n/2) Complexity

for ( int i= 2; i < num/2; i++)


{
if( num % i == 0)
{
divisors ++;
}
}

2023-24
8

O(sqrt(n)) Complexity

for ( int i= 2; i < sqrt(num); i++)


{
if( num % i == 0)
{
divisors ++;
}
}

Functions in C++

2023-24
9

Calculate Area of a Square

#include <bits/stdc++.h>
using namespace std;

void calArea(int a)
{
int area = a * a;
cout <<"Area is " << area << endl;
}

int main()
{
calArea( 5 );
calArea( 7 );

return 0;
}

Returning the Function

#include <iostream>
using namespace std;

int calArea(int a)
{
int area = a * a;

return area;
}

int main()
{

cout << calArea( 5 ) << endl;


cout<< calArea( 7 ) << endl;

return 0;
}

2023-24

You might also like