Computer Programming Lab Assignment 1
LAB ASSIGNMENT NO. 01
Task 1:
Task 1:
1.
Write a program to simulate an analog watch
(number 1 to 12 to be arranged in circular fashion with all the three pointers
for second minutes and hours ) on the screen. Use nested for loops.
Use (.) dot for seconds pointer
Use (*) star for minutes pointer
Use (#) hash for hours pointer.
Source Code:
#include <iostream>
using namespace std;
int main()
{
cout<<"(.) dot for seconds pointer, (*) star for minutes pointer, (#) hash for hours pointer"<<"\n \n \n";
for(int i=0;i<=0;i++)
{
for(int j=1;j<=1;j++)
{
cout<<" 12 "<<endl;
cout<<" 11 # 1 "<<endl;
cout<<" 10 # 2 "<<endl;
cout<<" 9 * * 3 "<<endl;
cout<<" 8 . 4 "<<endl;
cout<<" 7 . 5 "<<endl;
cout<<" 6 "<<endl;
}
}
}
Write a program
that prints the following empty diamond shape using for loop. You may use
output statements that print an asterisk (*).Ask user to enter an integer length of the diamond.
Source Code:
#include<iostream>
using namespace std;
int main()
{
int n,space=1,space2=1;
cout<<"Enter number:";
cin>>n;
space=n-1;
for (int i=1;i<=n; i++)
{
for(int j=1;j<=space;j++)
{
cout<<" ";
}
space--;
for( int k=1;k<=(2*i)-1;k++)
{
if(k==1||k==2*i-1)
cout<<"*";
else
cout<<" ";}
cout<<endl;
}
for(int i=1;i<=n-1;i++)
{
for(int j=1;j<=space2;j++)
{
cout<<" ";
}
space2++;
for(int k=1;k<=(2*(n-i)-1);k++)
{
if(k==1||k==2*(n-i)-1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
Write a
C++ program that Swap two elements in an Array
Source Code:
#include <iostream>
using namespace std;
int main()
{
int n, array[2];
cout<<"Enter first element of array: ";
cin>>array[0];
cout<<"Enter Second element of array: ";
cin>>array[1];
cout<<"Orignal array: "<<array[0]<<" "<<array[1]<<endl;
int temp;
for(int i=0;i<1;i++)
{
temp=array[0];
array[0]=array[1];
array[1]=temp;
}
cout<<"The Swapped Array: "<<array[0]<<" "<<array[1];
return 0;
}
Write a program that uses while loops to
perform the following steps:
a.
Prompt the user to input two integers:
firstNum and secondNum (firstNum must be less than secondNum).
b.
Output
all odd numbers between firstNum and secondNum.
c.
Output
the sum of all even numbers between firstNum and secondNum.
d.
Output
the numbers and their squares between 1 and 10.
e.
Output
the sum of the square of the odd numbers between firstNum and secondNum.
f.
Output
all uppercase letters.
Source Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int FirstNum,SecondNum,sum=0,SumSquare=0;
cout<<".........................................."<<endl;
cout<<"Enter First And Second Number: ";
cin>>FirstNum>>SecondNum;
cout<<".........................................."<<endl;
cout<<"Odd Numbers between FirstNumber and Second Number ";
while(FirstNum<SecondNum)
{
if(FirstNum%2!=0)
{
cout<<FirstNum<<" "<<endl;
SumSquare=SumSquare+pow(FirstNum,2);
}
else
{
sum=sum+FirstNum;
}
FirstNum++;
}
cout<<".........................................."<<endl;
cout<<"sum of even numbers: "<<sum<<endl;
cout<<".........................................."<<endl;
cout<<"Sum of Odd Numbers Square: "<<SumSquare<<endl;
cout<<".........................................."<<endl;
int i=1;
while(i<10)
{
cout<<"Square of "<<i<<": "<<i*i<<endl;
i++; }
cout<<".........................................."<<endl;
cout<<"Upper Case Letters"<<endl;
cout<<".........................................."<<endl;
char j='A';
while(j<='Z')
{
cout<<j<<" ";
j++;
}
cout<<endl;
cout<<".........................................."<<endl;
return 0;
} Write a program to read a positive integer
number ‘n’ and generate the number in following way.if the entered number is 4
the output will be as follows.
OUTPUT: 4! 3! 2! 1! 0 1! 2! 3! 4! 5!
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter Positive Number: ";
cin>>n;
for(int i=n;i>0;i--)
{
cout<<i<<"! ";
}
for(int i=0;i<=n+1;i++)
{
cout<<i<<"! ";
}
return 0;
}
Comments
Post a Comment