0% found this document useful (0 votes)
24 views

Lab 01 1317 DSA

The document contains 11 coding exercises related to data structures and algorithms in C++. The exercises cover topics like swapping values without a third variable, reversing digits in a number, calculating circle properties from user input, finding sums of ranges of numbers, calculating factorials iteratively and recursively, and analyzing/manipulating strings. For each exercise, the document provides the full source code of a C++ program implementing the required logic.

Uploaded by

Hira Shahbaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lab 01 1317 DSA

The document contains 11 coding exercises related to data structures and algorithms in C++. The exercises cover topics like swapping values without a third variable, reversing digits in a number, calculating circle properties from user input, finding sums of ranges of numbers, calculating factorials iteratively and recursively, and analyzing/manipulating strings. For each exercise, the document provides the full source code of a C++ program implementing the required logic.

Uploaded by

Hira Shahbaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Hira Shahbaz

Lab -01
Data Structures and Algorithms
//Exercise-1: Get two values from the user and swap them.Using a
Third Variable

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int firstnumber,secondnumber,temp;

cout<<"Enter a number : ";

cin>>firstnumber;

cout<<"Enter second number : ";

cin>>secondnumber;

temp=firstnumber;

firstnumber=secondnumber;

cout<<"\n\nNOW\n\nFirst number is : "<<firstnumber<<endl;

cout<<"Second Number is : "<<temp;

getch();
return 0;

//Exercise-1: Get two values from the user and swap them.Without
using a third variable

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int firstNumber,secondNumber;

cout<<"Enter an Integer : ";

cin>>firstNumber;

cout<<"Enter a second Integer : ";


cin>>secondNumber;

firstNumber=firstNumber+secondNumber;

secondNumber=firstNumber-secondNumber;

firstNumber=firstNumber-secondNumber;

cout<<"\n\n***\n\n First Number is : "<<firstNumber<<endl;

cout<<"Second Number is : "<<secondNumber;

getch();

return 0;

//Exercise-2: Ask user to enter a three digit number. Then display the
number in reverse order.Without using loop.

#include<iostream>
#include<conio.h>

using namespace std;

int main()

int number,remainder;

cout<<"Enter a three Digit Number : ";

cin>>number;//410

cout<<"Reversing this number : ";

remainder=number%10;//0

number=number/10;//41

cout<<remainder;//0

remainder=number%10;//1

cout<<remainder;

number=number/10;//4

cout<<number;

getch();

return 0;

}
//Exercise-2: Ask user to enter a three digit number. Then display the
number in reverse order.With using loop.

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int number,remainder=0;

cout<<"Enter any three Digit Number : ";

cin>>number;//123

while(number!=0)

{
remainder=remainder*10;

remainder=remainder+number%10;

number=number/10;

cout<<"Reversed Number is : "<<remainder;

getch();

return 0;

//A program that takes an n digits integer from user and shows the
digits on the screen separately i.e. if user enters 6572, it displays 6,5,7,2
separately and a total of individual numbers as well e.g 20 in given case.

#include<iostream>

#include<conio.h>
#include<math.h>

using namespace std;

int main ()

int number,counter=0,sum=0,result,temp=0,power;

cout<<"Enter a Number : ";

cin>>number;

temp=number;

while(number!=0)

number=number/10;

counter++;

cout<<"Size of Number That you have entered is :


"<<counter<<endl;

number=temp;

while(number != 0)

power=pow(10.0,counter-1);

result=number/power;
number=number%power;

cout<<result<<" , ";

sum=sum+result;

counter--;

cout<<endl<<"Sum of all digits are : "<<sum;

//A program that takes an n digits integer from user and shows the
digits on the screen separately i.e. if user enters 6572, it displays 6,5,7,2
separately and a total of individual numbers as well e.g 20 in given case.

#include<iostream>

#include<conio.h>

using namespace std;


int main()

int number=0,counter=0,reverse=0,sum=0;

cout<<"Enter a Number : ";

cin>>number;

while(number !=0)

reverse=reverse*10;

reverse=reverse+number%10;

sum=sum+number%10;

number=number/10;

counter++;

while(reverse!=0)

cout<<reverse%10;

if(counter>1)

cout<<" , ";
counter--;

reverse=reverse/10;

cout<<endl<<"Sum of digits are : "<<sum;

return 0;

//Exercise no 4 :Write a program that takes radius of a circle from


the user and calculates the diameter, circumference and area of the
circle and display the result. diameter = radius * 2 circumference = 2 *
3.14 * radius ; area = 3.14 * radius * radius

#include<iostream>
#include<conio.h>

using namespace std;

int main()

int radius;

cout<<"Enter The radius of Circle : ";cin>>radius;cout<<endl;

cout<<"Diameter of circle is : "<<radius*radius<<endl<<endl;

cout<<"Circumference of circle is :
"<<2*3.141592654*radius<<endl<<endl;

cout<<"Area of circle is : "<<3.141592654*radius*radius;

//Exercise-5:Write a program which calculates and displays the sum of


first 100 integers.
#include<iostream>

#include<conio.h>

using namespace std;

int main()

int sum=0;

for(int i=1;i<=100;i++)

sum=sum+i;

cout<<"Sum of 1-100 numbers is : "<<sum;

}
/*Exercise-6: Write a program that calculates sum of even numbers for
a given upper limit of integers.

The user should not be able to give upper limit greater than 1000.

The program should contain two functions.

The first function GetUpperLimit takes the input from the user and
second function SumOfEvencalculates the sum of even number up to
given upper limit.*/

#include<iostream>

#include<conio.h>

using namespace std;

int getsUpperLimit()

int input;

cout<<"Enter any number less than 1000 : ";cin>>input;

if(input>1000)

"Entered Number Should be Less Than 1000";

return input;

int sumofEvenCalculates()
{

int take,sum=0;

take=getsUpperLimit();

if(take%2==0)

for(int i=0;i<=take;i++)

if(i%2==0)

sum=sum+i;

cout<<"Sum of even Numbers Upto entered limit is :


"<<sum;

else{

take=take-1;

for(int i=0;i<=take;i++)

{
if(i%2==0)

sum=sum+i;

cout<<"Sum of even Numbers Upto entered limit is :


"<<sum;

int main()

sumofEvenCalculates();

return 0;

}
//Exercise-7: Write a function that calculates the factorial of a given
number by using iteration and recursion.

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{ int number,factorial=1;

cout<<"Enter a Number whose Factorial is to be calculated : ";

cin>>number;

for(int i=number;i>0;i--)

factorial*=i;

}
cout<<"Factorial of given number is : "<<factorial;

//Exercise-8:Write a program which takes an integer input from


user and displays its table. The table is displayed up to the
multiplier entered by the user.

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int integer,w=1;

cout<<"Enter any Integer : ";

cin>>integer;

while(w<=12)
{

cout<<integer<<" * "<<w<<" = "<<integer*w<<endl;

w++;

/*Exercise-9:Write a program that allows the user to guess a character


from ‘a’ to ‘z’.

The user should allow maximum five tries for guessing.

If the user guesses the number on the first try then he/she gets score
10,000.
On second, third, fourth and fifth try gets 8,000, 6,000, 4,000, 2,000
respectively and 0 if he/she could not guess the number.*/

#include<iostream>

#include<conio.h>

using namespace std;

int main()

char a;

for(int i=1;i<=5;i++)

cout<<"Enter any alphabet of your guess : ";cin>>a;

if(a=='M' || a=='m')

cout<<"You got it : ) ";

if(i==1)

cout<<"Congratulations ! You win 10,000";

break;

else if(i==2)
{

cout<<"Congratulations ! You win 8,000";

break;

else if(i==3)

cout<<"Congratulations ! You win 6,000";

break;

else if(i==4)

cout<<"Congratulations ! You win 4,000";

break;

else if(i==5)

cout<<"Congratulations ! You win 2,000";

break;

}
}

else

if(i==5)

break;

cout<<"Oops ! You have "<<i+1<<" attempt\n";

cout<<"Try Again! \n";

}
//Exercise-10: Write a program to display string from backward e.g. if
user enters “Hello World” the program should display “ dlroW olleH”.

#include<iostream>

#include<conio.h>

using namespace std;

int main()

char array[80];

int i=0;

cout<<"Enter any line : ";

cin.getline(array,80);

while(array[i]>'\0')

i++;

cout<<"Reversed Line is : ";

for(int j=i-1;j>=0;j--)

cout<<array[j];
}

return 0;

//Exercise-11:Write a program to count number of words in string e.g.


if user enters “This is a string” the program should display 4.

#include<iostream>

#include<conio.h>

using namespace std;

int main()

char array[80];

int counter=1,i;
cout<<"Enter any line :";cin.getline(array,80);

for(i=0;array[i] != '\0';i++)

if (array[i]== ' ')

counter++;

cout<<"Number of words in line are : "<<counter;

return 0;

You might also like