ca
ca
1) An unsigned variable type of int can hold zero and positive numbers and a signed
int holds negative zero and positive numbers.
When we know that the value we are storing will always be a negative number
2)
#include <iostream>
using namespace std;
int main(){
int a ,flag;
char name[30];
flag = true;
double b;
cout<<"enter in the integer\n";
cin>>a;
cout<<"enter in a char variable can be your name\n";
cin>>name;
cout<<"enter in a double character\n";
cin>>b;
cout<<"the value of flag is "<<flag<<endl;
cout<<"your name is "<<name<<" and the integer entered is"<<a<<" and the double
"<<b<<endl;
return 0;
}
Output
3) #include <iostream>
using namespace std;
int main(){
int a,b,c,d;
cout<<"enter in the first and secong integer\n";
cin>>a>>b;
cout<<"thier sum is "<<a+b<<endl;
cout<<"thier product is "<<a*b<<endl;
return 0;
}
Output
4)
#include <iostream>
using namespace std;
int main(){
string name;
cout<<"enter in your name\n";
cin>>name;
cout<<"hello "<<name<<endl;
return 0;
}
output
LESSON 2 FUNCTIONS
1)
#include <iostream>
#include <ostream>
using namespace std;
void product(int a,int b){
cout<<"enter in the first integer\n";
cin>>a;
cout<<"enter in the second integer\n";
cin>>b;
cout<<"the product btw the two numbers are "<<a*b<<endl;
}
int main(){
int a,b;
product(a,b);
return 0;
}
OUTPUT
2)
#include <iostream>
using namespace std;
void quotient(int a,double b){
cout<<"thier quotient is "<<a/b<<endl;
}
int main(){
int a,c;
double b;
cout<<"enter in two integers\n";
cin>>a>>b;
quotient(a,b);
return 0;
}
Output
#include <iostream>
using namespace std;
void addition (int a,int b){
cout<<"the sum of the two numbers are"<<a+b<<endl;
}
void subtraction(int a,int b){
cout<<"the subtraction of the two numbers are"<<a-b<<endl;
}
void multiplication(int a,int b){
cout<<"the multiplication of the two numbers are"<<a*b<<endl;
}
void quotient(int a,int b){
cout<<"the quotient of the two numbers are"<<a/b<<endl;
}
int main(){
int a,b;
char c;
cout<<"enter in the first and second number\n";
cin>>a>>b;
cout<<"enter in the sign\n";
cin>>c;
switch(c){
case '+':
addition(a,b);
break;
case '-':
subtraction(a,b) ;
break;
case '*':
multiplication(a,b);
break;
case '/':
quotient(a,b);
break;
}
return 0;
}
Out put
1)
#include <iostream>
using namespace std;
int check(int a){
int flag;
flag=true;
while(flag=true){
if(a>=0){
return true;
}
else{
return false;
}
}
}
int main(){
int a,d;
cout<<"enter in the integer\n";
cin>>a;
d=check(a);
cout<<d;
if(d==1){
cout<<"\ntrue";
}
else{
cout<<"\nfalse";
}
return 0;
}
OUTPUT
2)
#include <iostream>
using namespace std;
int check(int a){
int flag;
flag=true;
while(flag=true){
if(a>=0){
return true;
}
else{
return false;
}
}
}
int main(){
int a,d;
while(a>0){
cout<<"enter in the integer\n";
cin>>a;
}
d=check(a);
cout<<d;
if(d==1){
cout<<"\ntrue";
}
else{
cout<<"\nfalse";
}
return 0;
}
OUTPUT
3)
#include <iostream>
using namespace std;
int factorial(int num1){
if (num1>1){
return num1 * factorial(num1-1);
}
else{
return 1;
}
}
int main(){
int num1,fact;
cout<<"enter in the value to calculate its factorial\n";
cin>>num1;
cout<<"the factorial of "<<num1<<" = "<<factorial(num1)<<endl;
return 0;
output
LESSON 4 POINTERS
1)
You can't point to half a byte. Or 0.099999999998 of one. It's as simple
as that. It makes no sense to have a non-integral value of a memory
address, which is why such nonsensical operation is not allowed.
2)
Suppose that address value is 100 value after adding 3 will be more on
3*sizeof(float), that is 3*4=12 and ewsukt is 112.
That is 100+4*3=112
3)
#include <stdio.h>
void swap(int *a,int *b);
int main(){
int*p1,*p2,a,b;
printf("enter in the first value\n");
scanf("%d",&a);
p1=&a;
printf("enter in the second number\n");
scanf("%d",&b);
p2=&b;
printf("before swap a = %d and b = %d",a,b);
swap(p1,p2);
return 0;
}
void swap(int *a,int *b){
printf("\nafter swap a = %d and b = %d",*b,*a);
Output
4)
#include <stdio.h>
void even_check(int *a){
int i;
for(i=0;i<10;i++){
if(a[i]%2==0){
printf("%d\n",a[i]);
a++;
}
}
}
int main(){
int *ptr1,i;
int arr[]={1,2,3,4,5,6,7,8,9,10};
ptr1=&arr;
even_check(ptr1);
return 0;
}
Output
Lesson 5 memory
1) Each time the function calls itself it uses more of stack memory .If
the function run to many time it can eat up all the available
memory resulting to a stack overflow
2) Dynamically allocated memory
3) Use garbage collection only
4)
Lesson 6 scope and extent
1) Global variables are created when the program starts and
destroyed when the program ends
2) Variables created in the function are called local
variables .these variables are created when the program
enter into the block and destroyed when the program exit
the block
3) X is a local variable (its declared in the fxn)
Lesson 7 Reference
1) The reference empty_reference and unnamed
_reference have not been assigned
2) X_ref =4
3)After a function returns its stack frame is obliterated
and local variables no longer exist. Therefore, a
reference to a local variable (which is basically a
pointer) would point into garbage memory on the
stack
4)
#include <iostream>
using namespace std;
void swap(int *a,int *b);
int main(){
int *ptr1,*ptr2,num1,num2;
cout<<"enter in the first number\n";
cin>>num1;
ptr1=&num1;
cout<<"enter in the second numnber\n";
cin>>num2;
ptr2=&num2;
swap(ptr1,ptr2);
return 0;
}
void swap(int*a,int *b){
cout<<"before swap num1 = "<<*a<<" and num2 = "<<*b;
cout<<"\nafter swap num1 = "<<*b<<" and num2 = "<<*a;
}
Output
5)
#include <stdio.h>
int arr1[]={7,3,5,2,1,4,6,9,10,8};
void ascending(int arr[]){
int a,i,j;
for (i=0;i<10;i++){
for (j = i + 1; j < 10; j++)
{
a = arr[i];
arr[i] =arr[j];
arr[j] = a;}
}
}
for(i=0;i<j;i++){
printf("%d\n",arr[i]);
}
}
int main(){
ascending(arr1);
return 0;
}
LESSON 8 : ARRAYS
1)
#include <stdio.h>
void enter(char * a[]);
int main(){
int i;
char * name[]={"BESONG"," ",
"BRYAN"," ","EBAI"};
enter(name);
return 0;
}
void enter(char * a[]){
int i;
for(i=0;i<5;i++){
printf("%s",a[i]);
}
}
Output