Académique Documents
Professionnel Documents
Culture Documents
FIRST ASSIGNMENT
CODE:
#include<iostream>
using namespace std;
int sum(int );
int main()
{
int i;
cout<<"Enter any Number : ";
cin>>i;
cout<< Sum will be equal to "<<
for(int j=1;j<=i;j++)
{
Talha Sadiq
G1F14BSCS0083
Page 1 of 6
Cout<<j;
if(j != i){
cout << " + ";
}
}
cout<<" = ";
cout<<sum(i);
return 0;
}
int sum(int n)
{
if(n>0)
{
return n+sum(n-1);
}else{
return 0;
}
Talha Sadiq
G1F14BSCS0083
Page 2 of 6
}
OUTPUT:
Sum will be equal to 1 + 2 + 3 + 4 + 5 = 15
CODE:
#include<iostream>
using namespace std;
int decimal_binary(int );
int binary_decimal(int );
int main()
{
int a;
char c;
cout<<"D or d to Convert Binary to
Decimal .... "<<endl;
Talha Sadiq
G1F14BSCS0083
Page 3 of 6
";
cin>>a;
cout<<endl<<a<<" IN Binary Number. =
"<<binary_decimal(a)<<" in Decimal ";
}
else if(c=='B' || c=='b')
{
cout<<"Enter Decimal Number
";
cin>>a;
cout<<endl<<a<<" in Decimal Number.
"<<decimal_binary(a)<<" in Binary";
}
Talha Sadiq
G1F14BSCS0083
Page 4 of 6
return 0;
}
int binary_decimal(int x)
{
int rem,decimal=0,i=1;
while(x!=0)
{
rem = x % 10;
decimal = decimal + rem * i;
x = x / 10;
i = i * 2;
}
return decimal;
}
int decimal_binary(int x)
{
int rem,binary=0,i=1;
while(x!=0)
{
Talha Sadiq
G1F14BSCS0083
Page 5 of 6
rem=x%2;
x/=2;
binary+=rem*i;
i*=10;
}
return binary;
}
OUTPUT:
D or d to Convert Binary to Decimal ....
B or b to Convert Decimal to Binary....
D
Enter binary number : 110111
110111 in binary = 55 in decimal
Talha Sadiq
G1F14BSCS0083
Page 6 of 6