0% found this document useful (0 votes)
43 views13 pages

CS 022 Lab 1

The document contains solutions to 8 questions in C++ programming. Each question solution contains the code to solve the problem. The solutions cover topics like input/output, strings, loops, functions, and mathematical operations.
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)
43 views13 pages

CS 022 Lab 1

The document contains solutions to 8 questions in C++ programming. Each question solution contains the code to solve the problem. The solutions cover topics like input/output, strings, loops, functions, and mathematical operations.
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/ 13

Lab Assignment 1

Solution

Roll Number 23/11/EC/022


Name SHIVAM MODANWAL
Branch CSE

Q1: SOLUTION
#include <iostream>
using namespace std ;
int main(){
string s="This is my first c++ program";
cout<<s;
return 0;
}
Q2:SOLUTION
#include <iostream>
int main(){
std::string s="Hello World!, This is my first c++
program";
std::cout<<s;
return 0;
}
Q3:SOLUTION
#include<iostream>
using namespace std;
int main(){
cout<<"size of int is ";
cout<<sizeof(int);
cout<<"\nsize of char is ";
cout<<sizeof(char);
cout<<"\nsize of float is ";
cout<<sizeof(float);
cout<<"\nsize of double is "<<sizeof(double)<<endl;
return 0;
}
Q4:SOLUTION
#include<iostream>
using namespace std;
int main(){
int x,y,z,Output=0;
cout<<"enter 3 number and get which one is minimum"<<endl;
cout<<"enter the num1\n";
cin>>x;
cout<<"enter the num2\n";
cin>>y;
cout<<"enter the num3\n";
cin>>z;
if(x<y && x<z){
Output=x;
}
if(y<x && y<z){
Output=y;
}
if(z<y && z<x){
Output=z;
}
cout<<"Output = "<<Output;
return 0;
}
Q5:SOLUTION
#include<iostream>
using namespace std ;
int main () {
string s;
cout<<"Enter string : ";
getline(cin,s);
for(int i=0;i<s.length();i++){
if(s[i]== ' '){
cout<<"\n";
}else{
cout<<s[i];
}
}
return 0;
}
Q6:SOLUTION
#include <iostream>
using namespace std;
int main(){
int n, sum=0;
cout <<"Enter n: ";
cin >> n;
for ( int i = 1; i<=n; i++){
sum+=i;
cout << i;
if(i < n)
{
cout << " + ";
}
}
cout <<" = "<<sum;
return 0 ;
}
Q7:SOLUTION
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int a, b;
cout <<"Enter a positive number a (At least two digit number): ";
cin >> a; cout <<"Enter a positive number b (less than the length of a): ";
cin >> b;
int cnt = 0;
int dig[8];
while( a != 0)
{
int d = a%10;
a = a/10;
dig[cnt] = d;
cnt++;
}
int x = (dig[b])*pow(10,(b)) ;
int y = (dig[b-1])*pow(10,(b-1));
cout << "OUTPUT = " << x - y <<"\n";
return 0;
}
Q8:SOLUTION
#include<iostream>
#include<cmath>
using namespace std;
int factorial(int a){
if(a==1||a==0){
return 1;
}
return a*factorial(a-1);
}
int main(){
float x,n;
cout<<"enter the value of X,n"<<endl;
cin>>x>>n;
cout <<endl;
float sum=0;
for(int i=0;i<n;i++){
if(i%2!=0){
sum=sum-(pow(x,i+1))/factorial(i+1);
}
if(i%2==0){
sum=sum+(pow(x,i+1))/factorial(i+1);
}
}
cout<<sum;
return 0;
}

You might also like