Dsa - Assignment 1
Dsa - Assignment 1
SET 1
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];
return 0;
}
OUTPUT :
5Q)
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
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;
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
1)Q
#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) Q
#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;
return 0;
}
OUTPUT :
4)Q
#include <iostream>
using namespace std;
int decrease(int num) {
int count1[10] = {0};
while (num > 0)
{
int digit = num % 10;
count1[digit]++;
num /= 10;
}
int new_num= 0;
for (int i = 9; i >= 0; --i)
{
while (count1[i] > 0)
{
new_num=new_num * 10 + i;
count1[i]--;
}
}
return new_num;
}
int main()
{
int n1, n2, n3;
cout << "Enter three 6-digit numbers: ";
cin >> n1 >> n2 >> n3;
int chan1 = decrease(n1);
int chan2 = decrease(n2);
int chan3 = decrease(n3);
int largest = chan1;
if (chan2 > largest)
{
largest = chan2;
}
if (chan3 > largest)
{
largest = chan3;
}
cout << "The largest of the modified numbers is: " << largest << endl;
return 0;
}
OUTPUT :
5) Q
#include <iostream>
using namespace std;
int fact(int n) {
int factO = 1;
for (int i = 1; i <= n; ++i)
{
factO *= i;
}
return factO;
}
int main()
{
int n;
cout << "Enter how many Fibonacci numbers you want: ";
cin >> n;
int a = 0, b = 1;
cout << "Number (n)\tFactorial (n!)" << endl;
for (int i = 0; i < n; ++i)
{
cout << b << "\t\t" << fact(b) << endl;
int next = a + b;
a = b;
b = next;
}
return 0;
}
OUTPUT ;
6)Q
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sentence;
cout << "Enter a sentence: ";
getline(cin, sentence);
int vowelCount = 0, nonAlphaCount = 0;
int firstVowelPosition = -1;
for (int i = 0; i < sentence.length(); i++)
{
char ch = sentence[i];
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
vowelCount++;
if (firstVowelPosition == -1)
{
firstVowelPosition = i + 1;
cout << "First vowel '" << ch << "' occurs at position: " << firstVowelPosition << endl;
}
}
else if (!isalpha(ch))
{
nonAlphaCount++;
}
}
cout << "Number of vowels: " << vowelCount << endl;
cout << "Number of non-alphabetic characters: " << nonAlphaCount << endl;
if (firstVowelPosition == -1)
{
cout << "No vowels found." << endl;
}
return 0;
}
7) Q
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, result;
cout << "Enter a string: ";
getline(cin,s);
result=result+s[0];
for (size_t i = 1; i < s.size(); ++i)
{
if (s[i] != s[i - 1])
{
result += s[i];
}
}
cout << "Original length= " << s.size() << endl;
cout << "Final length= " << result.size() << endl;
cout << "Final string: " << result << endl;
return 0;
}