In: Computer Science
1)
A)
CODE
#include<iostream>
using namespace std;
int main()
{
//declaring variables
int i,sum=0;
//for loop for finding sum of intgers from 1 to
5
for(i=1;i<=5;i++)
{
//calculating sum
sum=sum+i;
}
//printing sum
cout<<"Sum of 1-5 is
"<<sum<<endl;
sum=0;
i=1;
//while loop for finding sum of intgers from 1 to
5
while(i<=5)
{
//calculating sum
sum=sum+i;
//incrementing value
i++;
}
//printing sum
cout<<"Sum of 1-5 is
"<<sum<<endl;
return 0;
}
OUTPUT
CODE SCREEN SHOT
B)
CODE
#include<iostream>
using namespace std;
int main()
{
//declaring variables
int i=1,fact=1;
//while loop for finding factorial of 5
while(i<=5)
{
//calculating factorial
fact=fact*i;
//incrementing value
i++;
}
//printing factorial
cout<<"Factorial of 5 is
"<<fact<<endl;
fact=1;
i=1;
//for loop for finding factorial 5
for(i=1;i<=5;i++)
{
//calculating factorial
fact=fact*i;
}
//printing factorial
cout<<"Factorial of 5 is
"<<fact<<endl;
return 0;
}
OUTPUT
CODE SCREEN SHOT
2)
CODE
#include<iostream>
using namespace std;
//function for print an array
void printArray(int functionArray[],int size)
{
//for loop
for(int i=0;i<size;i++)
{
//printing array
cout<<functionArray[i]<<endl;
}
}
int main()
{
//initializing an array
int array[5]={1,2,3,4,5};
//finding size of an array
int size = sizeof(array)/sizeof(array[0]);
//passing array and size to a function
printArray(array,size);
return 0;
}
OUTPUT
CODE SCREEN SHOT