-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFUNCTION.CPP
More file actions
100 lines (82 loc) · 3.16 KB
/
FUNCTION.CPP
File metadata and controls
100 lines (82 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// This program converts a binary number to a decimal number using a function.
#include<iostream>
using namespace std;
/*void BinToDec(int BinNo){ //function definition
int n = BinNo; int DecNo = 0; int pow=1;
while(n>0){
int LastDigit = n%10; //to get the last digit of the binary number
DecNo = DecNo + LastDigit*pow; //to add the value of the last digit to the decimal number
pow = pow*2; //to calculate the power of 2 for the next digit
n = n/10; //to remove the last digit of the binary number
}
cout << "The decimal no is: " << DecNo << endl;
}
int main(){
int BinNo;
cout << "Enter a binary number: ";
cin >> BinNo;
BinToDec(BinNo); //function call
return 0;
}
// This program converts a binary number to a decimal number using a function.
void BinToDec(int DecNo){
int n = DecNo; int BinNo = 0; int pow = 1;
while(n>0){
int rem = n%2;
BinNo = BinNo + rem*pow;
pow = pow*10;
n = n/2;
}
cout << "The binary equivalent is: " << BinNo << endl;
}int main(){
int DecNo;
cout << "Enter a decimal number: ";
cin >> DecNo;
BinToDec(DecNo); //function call
return 0;
}
// adding two numbers using a function
int sum(int a,int b){ //a and b are parameters
return a+b;} //parameters are variables jiske andar koi bhi value store ho sakti h
int main(){
cout<<sum(4,9)<<endl; //4,9 are arguments(are fixed value)
return 0;
}
// to print if a no. is even or odd using a function
void EvenOdd(int n){
if(n%2==0){
cout << n << " is an even number." << endl;
}
else{
cout << n << " is an odd number." << endl;
}}int main(){
int n; cout<<"enter a no. :"; cin>>n;
EvenOdd(n);
return 0;}
// to calculate the factorial of a no. using a function
void func(){
int factorial=1; int n; cout<<"enter a no. :"; cin>>n;
for(int i=1;i<=n;i++){
factorial = factorial*i;
}
cout << "The factorial of " << n << " is: " << factorial << endl;
}int main(){
func();
return 0;}
// check no is prime or not using a function
void func(){
int n; bool isPrime = true; cout<<"enter a no. :"; cin>>n;
for(int i=2;i<=n-1;i++){
if(n%i==0){
isPrime = false;
break;
}
}
if(isPrime){
cout << n << " is a prime number." << endl;
}
else{
cout << n << " is not a prime number." << endl;
}}int main(){
func();
return 0;} */