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

19

The document contains a series of C++ programming examples covering various topics such as creating multiple main functions, returning values from main, using input parameters, switch statements, and calculating areas, factorials, and prime numbers. It includes code snippets demonstrating functions for counting, checking odd/even numbers, and converting Celsius to Fahrenheit. Additionally, it discusses limitations on calculating factorials and provides methods for summing even numbers.

Uploaded by

loveumusic31
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)
8 views9 pages

19

The document contains a series of C++ programming examples covering various topics such as creating multiple main functions, returning values from main, using input parameters, switch statements, and calculating areas, factorials, and prime numbers. It includes code snippets demonstrating functions for counting, checking odd/even numbers, and converting Celsius to Fahrenheit. Additionally, it discusses limitations on calculating factorials and provides methods for summing even numbers.

Uploaded by

loveumusic31
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/ 9

1) Can we create more than one main function in a single program?

2) Can we return 1,-1 in the main() function?


3) Why no input paramters inside main()? Can we put input
parameters inside main() function?
4) Can we write any expression inside the case of switch statement?
5) Function to display area of circle
6) Number is odd or even
7) Factorial of number
8) Number is prime or not
9) Print all prime numbers from 1 to n
10) Why can't we calculate factorial above 13?
11) Reverse an integer
12) Set the ith bit
13) Convert celsius to fahrenheit.

//address of operator
#include<iostream>
using namespace std;
int main(){
int a = 5;
cout<<"Address of a is : "<<&a; // a hexadecimal value is printed
which is the address of variable a.
return 0;
}
//counting 1ton
#include<iostream>
using namespace std;
void printCounting(int n){
for(int i = 1;i<=n;i++){
cout<<i<<" ";
}
}
int main(){
int n = 5;
printCounting(n);
return 0;
}
//order of functions
#include<iostream>
using namespace std;
int add(int a,int b); // function declaration
int main(){
cout<<add(2,3); // function invoke
return 0;
}
int add(int a,int b){ // function defintion
return (a+b);
}
//area of circle
#include<iostream>
using namespace std;
float areaCircle(float r){
return 3.14*r*r;
}
int main(){
float r = 2.5;
cout<<"Area of circle is "<<areaCircle(r)<<endl;
return 0;
}
//oddoreven
#include<iostream>
using namespace std;
bool oddEven(int num){
if(num%2==0){
return true;
}
else
return false;
}
int main(){
int num = 3;
if(oddEven(num))
cout<<"Number is even ";
else
cout<<"Number is odd ";
return 0;
}
//factorial
#include<iostream>
using namespace std;
int factorial(int num){
int fact = 1;
for(int i = 1;i<=num;i++){
fact = fact * i;
}
return fact;
}
int main(){
cout<<factorial(11);
return 0;
}
//primeornot
#include<iostream>
using namespace std;
bool checkPrime(int num){
int factor = 0;
if(num<2){
return false;
}
for(int i = 2;i<num;i++){
if(num%i==0){
factor++;
break;
}
}
if(factor==1){
return false;
}
else{
return true;
}
}
int main(){
int num = 71;
if(checkPrime(num)){
cout<<"Prime "<<endl;
}
else{
cout<<"Not Prime"<<endl;
}
return 0;
}
//primeno from 1ton
#include<iostream>
using namespace std;
bool checkPrime(int num){
int factor = 0;
if(num<2){
return false;
}
for(int i = 2;i<num;i++){
if(num%i==0){
factor++;
break;
}
}
if(factor==1){
return false;
}
else{
return true;
}
}
void printPrime(int n){
for(int i = 2;i<=n;i++){
if(checkPrime(i)){
cout<<i<<endl;
}
}
}
int main(){
int n = 100;
printPrime(n);
return 0;
}
//maxof 3nos
#include<iostream>
using namespace std;
int maxi(int a,int b,int c){
if(a>b && a>c){
return a;
}
else if(b>a && b>c){
return b;
}
else{
return c;
}
}
int main(){
int a = 7,b = 5, c = 4;
cout<<maxi(a,b,c);
return 0;
}
//sum of even number upto n
#include<iostream>
using namespace std;
// Method - 1 but % is a heavy operation
int printEven(int n){
int sum = 0;
for(int i = 1;i<=n;i++){
if(i%2==0)
sum = sum + i;
}
return sum;
}
// Method - 2
int printEven1(int n){
int sum = 0;
for(int i = 2;i<=n;i=i+2){ // Even number starts from 2 and adding 2
to even number will give even number
sum = sum + i;
}
return sum;
}
int main(){
int n = 10;
cout<<printEven(n)<<endl;
cout<<printEven1(n);
}

You might also like