0% found this document useful (0 votes)
45 views7 pages

21CS8133 Labassignment1

The document contains a student's name and roll number followed by code snippets and output for problems 1, 2, 3, 4 and 5. It provides functions for large number addition, multiplication, factorials and code indentation.

Uploaded by

Sai Praneeth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views7 pages

21CS8133 Labassignment1

The document contains a student's name and roll number followed by code snippets and output for problems 1, 2, 3, 4 and 5. It provides functions for large number addition, multiplication, factorials and code indentation.

Uploaded by

Sai Praneeth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME : MAREDDY D V S PRANEETH

ROLL NO :21CS8133

ANSWER FOR 1,2,3,4

/* header.h */
#include<iostream>

#include<string>

using namespace std;

string largeAdd(string num1,string num2)

string ans="";

while (num1.size()>num2.size())

num2='0'+num2;

while (num1.size()<num2.size())

num1='0'+num1;

int a=0;

for (int i =num1.size()-1 ; i>=0; i--)

a=((num1[i]-'0')+(num2[i]-'0')+a);

ans=char(a%10+'0') + ans;

a /=10;

if (a==1)

ans='1'+ans;
}

return ans;

string largeMult(string n1,string n2)

int k=0;

string res="0";

for (int i =n2.size()-1 ; i>=0 ; i--)

string mid="";

for (int j =n1.size()-1 ; j>=0 ; j--)

k=((n1[j]-'0')*(n2[i]-'0'))+k;

mid=char(k%10+'0')+mid;

k /=10;

mid=char(k+'0')+mid;

if (i!=(n2.size()-1))

for ( int t = 0; t<n2.size()-1-i; t++)

mid=mid+'0';

res=largeAdd(res,mid);

k=0;

return res;

string largeFact(string a, string num = "1"){

if(a == num)
return num;

return largeMult(num ,largeFact(a, largeAdd(num, "1")));

/* largeNum.cpp */

#include<iostream>

#include"header.h"

using namespace std;

int main(){

string num1,num2,num3;

cout<<"enter the first number\t";

cin>>num1;

cout<<"enter the second number\t";

cin>>num2;

cout<<"the sum of given two numbers is: "<<largeAdd(num1,num2)<<endl;

cout<<"the product of given two numbers is: "<<largeMult(num1,num2)<<endl;

cout<<"enter a number to calculate factorial"<<endl;

cin>>num3;

cout<<"the factorial of the given number"<<largeFact(num3,"1")<<endl;

return 0;

/* OUTPUT*/
ANSWER FOR 5
/* Indentation */

#include<iostream>

#include<string.h>

#include<iomanip>

using namespace std;

#define max 100

string x;

string code;

void indent()

int i,k=0;

char symbol;

for(i=0;i<code.length();i++)

symbol=code[i];

switch(symbol)

case ')':

if(code[i+1]==';')
{

cout<<");\n";

}else{

cout<<")\n";

break;

case ';':

if(code[i-1]==')')

if(code[i+1]=='}')

k--;

cout<<setw(5*k);

else{

cout<<setw(5*k);

break;

else if(code[i+1]=='}')

cout<<";\n";

k--;

cout<<setw(5*k);

break;

else{

cout<<";\n";

cout<<setw(5*k);

break;
case '{':

cout<<"{\n";

k++;

cout<<setw(5*k);

break;

case '}':

cout<<"}\n";

k--;

cout<<setw(5*k);

break;

default:

cout<<symbol;

break;

int main()

cout<<"\nEnter the no. of lines in the code: ";

int n;

cin>>n;

cout<<"\nEnter the unindented code: \n";

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

getline(cin,x);

int a=x.length();

for(int j=0;j<a;j++)

code.push_back(x[j]);

}
cout<<"\nIndented code is:\n";

indent();

return 0;

/*OUTPUT*/

You might also like