5
5
EXPERIMENT - 5
Task 1: Identify output for below blocks of code without using Codeblocks. Give
justification.
a. #include<iostream> b. #include<iostream>
void main() void main()
{ {
int c=1; int c=0;
while(1) while(c<=5)
{ {
if(c==5) c++;
break; if(c==3)
cout<<c; continue;
c++; cout<<c;
} }
} }
OUTPUT: 12345 OUTPUT: 12456
c. void main() d. void main() {
{ int i,j,x=0;
int i,j,k;
for(i=0;i<5;++i)
{ for (i = 1; i <= 5; i++) {
for(j=0;j<i;++j)
for (j = 1; j <= (5 - i) ;j++) cout<<" ";
x+=(i+j-1);
cout<<x; for (k = 1; k <= i; k++)
break; cout<<i;
} cout<<“\n”;}
cout<<x;
}
}
OUTPUT: 00 OUTPUT:
1
22
333
4444
55555
Task2: Write a C++ program to print multiplication table for 1 to 10.
Expected Output:
PROGRAM:
#include <iostream>
using namespace std;
int main()
{
int i,j,res,num;
cout<<"Tables from 1-10"<<endl;
for(i=1;i<=10;i++)
{
num=i;
for(j=1;j<=10;j++)
{
res=num*j;
cout<<res<<" ";
}
cout<<endl;
}
cout<<endl;
return 0;
}
Task 4: Write a C++ program to print all the prime numbers from 1 to 100.
PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int i, chk=0, j;
cout<<"Prime Numbers Between 1 to 100 are:\n";
for(i=1; i<=100; i++)
{
for(j=2; j<i; j++)
{
if(i%j==0)
{
chk++;
break;
}
}
if(chk==0 && i!=1)
cout<<i<<endl;
chk = 0;
}
cout<<endl;
return 0;
}