Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

DSA ASSIGNMENT 1

SHAMIL SHIHAB P K
B190775EC
ECEB

1.
Code:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float tomato,potato,coconut,beans,jaambakka;
float ptomato = 5,ppotato = 6, pcoconut = 7, pbeans = 3, pjaambak
ka = 15;
float sum = 0;
cout<<"enter the quantity neede of each \n tomato \n potato \n co
conut \n beans \n jaambakka\n";
cin>>tomato;
cin>>potato;
cin>>coconut;
cin>>beans;
cin>>jaambakka;
sum = tomato*ptomato+potato*ppotato+coconut*pcoconut+beans*pbeans
+jaambakka*pjaambakka;
cout<<setw(20)<<left<<"ITEM";
cout<<setw(15)<<"QUANTITY(kg)";
cout<<setw(5)<<right<<"PRICE"<<endl;
cout<<"--------------------------------------------"<<endl;
cout<<setw(20)<<left<<"tomato"; cout<<setw(15)<<tomato;
cout<<setw(5)<<right<<setprecision(4)<<tomato*ptomato<<"rs"<<endl;
cout<<setw(20)<<left<<"potato"; cout<<setw(15)<<potato;
cout<<setw(5)<<right<<setprecision(4)<<potato*ppotato<<"rs"<<endl;
cout<<setw(20)<<left<<"coconut"; cout<<setw(15)<<coconut;
cout<<setw(5)<<right<<setprecision(4)<<coconut*coconut<<"rs"<<end
l;
cout<<setw(20)<<left<<"beans"; cout<<setw(15)<<beans;
cout<<setw(5)<<right<<setprecision(4)<<beans*pbeans<<"rs"<<endl;
cout<<setw(20)<<left<<"jaambakka"; cout<<setw(15)<<jaambakka;
cout<<setw(5)<<right<<setprecision(4)<<jaambakka*pjaambakka<<"rs"
<<endl;

cout<<"--------------------------------------------"<<endl;
cout<<setw(20)<<left<<"SUM";
cout<<setw(20)<<right<<setprecision(4)<<sum<<"rs"<<endl;
return 0;
}
2.
#include<iostream>
using namespace std;
int main(){
int marks;
cout<<"enter marks obtained \n";
cin>>marks;
cout<<"grade obtained is ";
switch(marks/10){
case 10:
case 9 : cout<<"S";
break;
case 8 : cout<<"A";
break;
case 7 : cout<<"B";
break;
case 6 : cout<<"C";
break;
case 5 : cout<<"D";
break;
case 4 : cout<<"E";
break;
case 3 :
case 2 :
case 1 :
case 0 : cout<<"F";
break;
}
return 0;
}
3.
#include<iostream>
using namespace std;
int main(){
char op;
float num1, num2;
cout << "Enter operator: +, -, *, /: ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
cout<<num1<<" "<<op<<" "<<num2<<" = ";
switch(op)
{ case '+': cout<< num1 + num2; break;
case '-': cout<< num1 - num2; break;
case '*': cout<< num1 * num2; break;
case '/': cout<< num1 / num2; break;
default: cout << "Error"; break;
}
return 0;
}
4.
#include<iostream>
using namespace std;
int main()
{
int arr1[3][3],i,j;
cout<<"Input elements in the matrix :\n"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ cout<<"array ["<<i<<"]["<<j<<"] : ";
cin>>arr1[i][j];

}
}
cout<<"The matrix is : ";
for(i=0;i<3;i++){
cout<<endl;
for(j=0;j<3;j++)
cout<<arr1[i][j]<<" ";
}
return 0;
}
5.
#include<iostream>
using namespace std;
int main()
{ for(int i=0;i<7;i++)
{
for(int j=0;j<=i;j++)
{ cout<<"*";
}
cout<<endl;
}
return 0;
}
6.
#include<iostream>
using namespace std;
int main()
{ cout<< " AUGUST,2014"<<endl;
cout<<"Sun Mon Tue Wed Thu Fri Sat "<<endl;
cout<<"31 01 02"<<endl;
for(int i=3;i<31;i++)
{ int d=7;
while(d-- && i<31)
{ if(i<10)
{ cout<<"0"<<i;
}
else cout<<i;
i++;
cout<<" ";
}
i--;
cout<<endl;
}
return 0;
}
12.
#include<iostream>
using namespace std;
int main()
{ int n,newnum=0,sum=0;
cout<<"Enter a 6 digit number : ";
cin>>n;
while(n)
{ newnum=(newnum*10)+(n%10);
sum+=n%10;
n/=10;
}
cout<<"Reversed Number : "<<newnum<<endl;
cout<<"Sum of digits = "<<sum;
return 0;
}
Set-2
1.
#include<iostream>
#include<cmath>
using namespace std;
float factorial(int n){
if(n==1) return 1;
return n*factorial(n-1);
}

int main(){
int n,x;
cout<<"Enter the number of elements : ";
cin>>n;
cout<<"Enter x value : ";
cin>>x;
float sum=x;
for(int i=2;i<=n;i++){
int powe=2*i-1;
float val=pow(x,powe);
val/=factorial(powe-1);
if(i%2==1){
sum-=val; }
else sum+=val;
}
cout<<"Sum = "<<sum;
return 0;
}
2.
#include<iostream>
using namespace std;

int main(){
int x;
cout<<"Enter number of rows that you want to print : ";
cin>>x;

for(int i=1;i<=x;i++){

for(int j=1;j<=(x-i)*3;j++){
cout<<" ";
}
for(int j=1;j<=i;j++){
cout<<"x^"<<i<<"+"<<j<<" ";
}
cout<<endl;
}
return 0;
}
3.
#include<iostream>
using namespace std;
int main()
{ int n;
cout<<"Number of rows to be printed : ";
cin>>n;
for(int i=0;i<=n;i++){
for(int j=0;j<n-i;j++)
{ cout<<" ";
}
for(int j=i;j>=0;j--){
cout<<j;
}
for(int j=1;j<=i;j++){
cout<<j;
}
cout<<endl;
}
return 0;
}
5.
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n;
cout<<"Enter the number of fibonacci series to be printed : ";
cin>>n;
long long fact=1;
long long a,b,fib=0;
cout<<setw(10)<<"Number(n)"<<setw(10)<<"Factorial(n!)"<<endl;
for(int i=1;i<=n;i++){
if(i<=2){
a=1;
b=1;
fib=1;
fact=1;
}
else{
fib=a+b;
for(int i=b+1;i<=fib;i++){
fact*=i;
}
a=b;
b=fib;
}
cout<<setw(10)<<fib<<setw(10)<<fact<<endl;
}
return 0;
}
6.
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
cout<<"Enter the sentence : ";
getline(cin,s);
int vowel_count=0,non_alpha=0,first_vowel=0;
for(int i=0;i<s.length();i++){
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u
' || s[i]=='A' || s[i]=='E' ||s[i]=='I' || s[i]=='O' || s[i]=='U'){
if(vowel_count==0){
first_vowel=i;
}
vowel_count++;
}
else if(!isalpha(s[i])){
non_alpha++;
}
}
cout<<"Non-alphabetic characters : "<<non_alpha<<endl;
if(vowel_count==0){
cout<<"No vowels"<<endl;
}
else{
cout<<"Number of vowels: "<<vowel_count<<endl;
cout<<"The index of the first vowel is : "<<first_vowel<<en
dl;
}
return 0;
}
7.

#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<"Enter a string:";
string str,newstr="";
cin>>str;
int i=0,n=str.length();
while(i<n){
if(i==n-1){
newstr+=str[i];
break;
}
else if(str[i]==str[i+1]){
i++;
}
else{
newstr+=str[i]; i++;
}
}
cout<<"Modified string is: "<<newstr<<endl;
cout<<"Initial length is : "<<n<<endl;
cout<<"Modified string length is : "<<newstr.length();
return 0;
}

You might also like