0% found this document useful (0 votes)
8 views15 pages

Dsa - Assignment 1

The document contains multiple C++ programming assignments by Uparapu Jayachandra from ECE 04 batch. Each assignment includes code snippets for various tasks such as calculating vegetable costs, grading based on marks, performing mathematical operations, handling matrices, generating patterns, and calculating factorials and power series. Additionally, it includes input validation and output formatting for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

Dsa - Assignment 1

The document contains multiple C++ programming assignments by Uparapu Jayachandra from ECE 04 batch. Each assignment includes code snippets for various tasks such as calculating vegetable costs, grading based on marks, performing mathematical operations, handling matrices, generating patterns, and calculating factorials and power series. Additionally, it includes input validation and output formatting for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

DSA - ASSIGNMENT 1

NAME : UPARAPU JAYACHANDRA


BATCH : ECE 04

1)Q
#include<iostream>
#include<iomanip>
#include <string>
using namespace std;
int main()
{
string item[5]={"brinjal","bitter guard","bottle guard","onion","tomato"};
int quantity[5]={};
int price[5]={11,22,69,88,25};

double bill[5]={};
double totalprice=0.0;
for(int i=0;i<5;i++)
{
cout<<"please enter the qunatity of each vegetables "<<item[i]<<"s needed"<<endl;
cin>>quantity[i];
bill[i]=quantity[i]*price[i];
totalprice=bill[i]+totalprice;
}
cout<<setw(15)<<"vegetables"<<setw(20)<<"price/kg"<<setw(7)<<"quantity"<<setw(7)<<"cost"<<endl;
for(int j=0;j<5;j++)
{
cout<<setw(15)<<item[j]<<setw(20)<<price[j]<<setw(7)<<quantity[j]<<setw(7)<<bill[j]<<endl;
}
cout<<setw(15)<<"Total price"<<setw(20)<<setprecision(2)<<totalprice<<endl;
return 0;
}
OUTPUT :
2)Q

#include<iostream>
using namespace std;
int main()
{
int marks;
cout<<"ENTER THE MARKS OBTAINED OUT OF 100"<<endl;
cin>>marks;
switch(marks/10)
{
case (10):
case(9): cout<<"Grade='S'"<<endl;
break;
case(8): cout<<"Grade='A'"<<endl;
break;
case(7): cout<<"Grade='B'"<<endl;
break;
case(6): cout<<"Grade='C'"<<endl;
break;
case(5): cout<<"Grade='D'"<<endl;
break;
case(4): cout<<"Grade='E'"<<endl;
break;
case(3): cout<<"Grade='F'"<<endl;
break;
case(2):cout<<"Grade='F'"<<endl;
break;
case(1):cout<<"Grade='F'"<<endl;
break;
default:cout<<"Invalid marks entered";
break;
}
return 0;
}
OUTPUT
3Q)
#include <iostream>
using namespace std;
int main()
{
char opp;
double n1, n2, sum , subtraction , multiplication , result ;
cout << "please Enter the mathematical operation you want to perform (+, -, *, /): ";
cin >> opp;
cout << "please Enter the first number: ";
cin >> n1;
cout << "please Enter the second number: ";
cin >> n2;
switch (opp)
{
case '+':
sum = n1 + n2;
cout << "The result calaculated is: " << sum << endl;
break;
case '-':
subtraction = n1 - n2;
cout << "The result calaculated is: " << subtraction << endl;
break;
case '*':
multiplication = n1 * n2;
cout << "The result calaculated is: " << multiplication << endl;
break;
case '/':
if (n2 != 0)
{
result = n1 / n2;
cout << "The result calaculated is: " << result << endl;
} else
cout << "Division not possible as denominator is 0" << endl;
break;
default:
cout << "Invalid input" << endl;
}
return 0;
}
OUTPUT

4Q)
#include <iostream>
using namespace std;

int main()
{
int matrix1[3][3];

cout << "Enter the elements for a 3x3 matrix"<<endl;


for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "PLEASE Enter the value for position (" << i + 1 << ", " << j + 1 << "): ";
cin >> matrix1[i][j];
}
}

cout << "\nTHE REQUIRED 3x3MATRIX IS "<<endl;


for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matrix1[i][j] << " ";
}
cout << endl;
}

return 0;
}
OUTPUT :

5Q)
#include <iostream>
using namespace std;
int main()
{
int rows = 5;

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


{
for (int k = 1; k <= i; k++)
{
cout << "*";
}
cout << endl;
}
return 0;
}

OUTPUT:
5Q)
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << "Calendar for month of August 2014\n\n";
cout << "Sun Mon Tue Wed Thu Fri Sat\n";

int startingDay = 5;
int daysInaMonth = 31;

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


{
cout << " ";
}

for (int day = 1; day <= daysInaMonth; day++)


{
cout << setw(5) << day;
if ((day + startingDay) % 7 == 0)
{
cout << endl;
}
}
cout << endl;

return 0;
}

OUTPUT:

6Q)
#include <iostream>
using namespace std;

int main()
{
int n, rev = 0, sum= 0;
cout << "Enter a 6-digit number: ";
cin >> n;
if (n < 100000 || n > 999999)
{
cout << "Enter a valid 6 digit number" << endl;
return 1;
}

int temp = n;
while (temp > 0)
{
int digit = temp % 10;
rev = rev * 10 + digit;
sum=sum+digit;
temp /= 10;
}
cout << "The Reversed Number= " << rev << endl;
cout << "Sum of Digits of given number= " << sum << endl;
return 0;
}

OUTPUT :

Set 2
#include <iostream>
using namespace std;
long long factorial(int num)
{
long long facto = 1;
for (int i = 1; i <= num; i++)
{
facto=facto*i;
}
return facto;
}
int pow(int m1,int m2)
{
int powertothe =1;
for(int i=1;i<=m2;i++)
{
powertothe=powertothe*m1;
}
return powertothe;
}
int main()
{
int n;
double x, sum = 0.0;
cout << "Enter the value of x ";
cin >> x;
cout << "Enter the number of terms ";
cin >> n;
for (int i = 1; i <= n; i++)
{
sum=sum+(pow(x,i)/factorial(i));
}
cout << "The sum of the series is= " << sum << endl;
return 0;
}

OUTPUT
2)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x;
cout << "Enter the valid value of x ";
cin >> x;
for (int i = 1; i <= 5; i++)
{
for (int gap = 5; gap > i; gap--)
{
cout << " ";
}
for (int j = 1; j <= i; j++)
{
cout << "x^" << i << "+" << j << " ";
}
cout << endl;
}
return 0;
}

Output

3)Q
#include <iostream>
using namespace std;

int main()
{
int rows = 6;

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


{

for (int j = 0; j < rows - i - 1; j++)


{
cout << " ";
}

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


{
cout << j;
}

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


{
cout << j;
}

cout << endl;


}

return 0;
}

OUTPUT :

You might also like