0% found this document useful (0 votes)
58 views10 pages

CS-305 Assignment - 2: NAME:-Himangi Shukla ROLL NO: - 0905CS191086 3 Sem, 2 Year

This document contains the solutions to 5 questions for a C programming assignment submitted by Himangi Shukla with roll number 0905CS191086. The questions cover topics like printing a number in words, differences between local and global variables, finding the largest number among 3 inputs, checking if a number is prime, and creating a basic calculator program with functions. Code snippets with explanations are provided as the solution for each question.
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)
58 views10 pages

CS-305 Assignment - 2: NAME:-Himangi Shukla ROLL NO: - 0905CS191086 3 Sem, 2 Year

This document contains the solutions to 5 questions for a C programming assignment submitted by Himangi Shukla with roll number 0905CS191086. The questions cover topics like printing a number in words, differences between local and global variables, finding the largest number among 3 inputs, checking if a number is prime, and creating a basic calculator program with functions. Code snippets with explanations are provided as the solution for each question.
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/ 10

CS-305

ASSIGNMENT - 2

NAME:- Himangi shukla

ROLL NO:- 0905CS191086

3RD SEM , 2ND YEAR


Q1.)

SOLN.

#include <iostream>

using namespace std;

int main()

int n, num = 0, i;

cout << "\n\n Print a number in words:\n";

cout << "-----------------------------\n";

cout << " Input any number: ";

cin >> n;

while (n != 0) {

num = (num * 10) + (n % 10);

n /= 10;

for (i = num; i > 0; i = i / 10) {

switch (i % 10) {

case 0:

cout << "Zero ";

break;

case 1:

cout << "One ";


break;

case 2:

cout << "Two ";

break;

case 3:

cout << "Three ";

break;

case 4:

cout << "Four ";

break;

case 5:

cout << "Five ";

break;

case 6:

cout << "Six ";

break;

case 7:

cout << "Seven ";

break;

case 8:

cout << "Eight ";

break;

case 9:

cout << "Nine ";

break;
}

cout << endl;

Q2)

SOLN. #include<iostream>
Using namespace std;

// defining the global variable


Int a=10;

Int main()
{
//local variable
Int a=15;
cout<<"local a: "<<a<<" Global a: "<<::a;

// Re-defining global variable by using ::


::a=20;
cout<<"\nlocal a: "<<a<<" Global a: "<<::a;
return0;
}

DIFFERENCE:-
The main difference between local and global variable is that the local variable is declared
inside a function while the global variable is declared outside the function in the program.

Q3)
SOLN #include<iostream>

Using namespace std;

Int find Biggest(int,int,int);//function prototype

Int main()
{
doublenum1, num2,num3; //declare the variables
cout<<"Enter the first number to compare: ";
cin>>num1;//get input from user for num1
cout<<"Enter the second number to compare: ";
cin>>num2;//get input from user for num2
cout<<"Enter the third number to compare: ";
cin>>num3;//get input from user for num3
intresult=findBiggest(num1,num2,num3);//function call
cout<<"Biggest number is: "<<result;//display the output

return0;
}
intfindBiggest(intnum1, intnum2, intnum3){//function definition
intbiggest;
if(num1>=num2){
if(num1>=num3){
returnnum1;
}
else{
returnnum3;
}
}
else{
if(num2>num3){
returnnum2;
}
else{
returnnum3;
}
}
}

Q4)
SOLN. #include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int num,i,count=0;

cout<<"Enter the positive integer\n";


//Takes input from user
cin>>num;
i=2;
do{

//condition for non-prime


if(num%i==0)
{
count=1;
break;
}
i++;
}while(i<=num/2);

if(num==1){
cout<<"you entered "<<num<<"\n";
cout<<num<<" is neither a prime nor a composite number ";
}
else{
if(count==0){
cout<<"you entered" <<num<<"\n\n";
cout<<num<<" is a prime number ";
}
else{
cout<<"you entered" <<num<<"\n\n";
cout<<num<<" is not a prime number ";
}
}
getch();
return 0;
}

Q5)
SOLN. #include <stdio.h>

/**
* Function declarations for calculator
*/
float add(float num1, float num2);
float sub(float num1, float num2);
float mult(float num1, float num2);
float div(float num1, float num2);

int main()
{
char op;
float num1, num2, result=0.0f;

/* Print welcome message */


printf("WELCOME TO SIMPLE CALCULATOR\n");
printf("----------------------------\n");
printf("Enter [number 1] [+ - * /] [number 2]\n");

/* Input two number and operator from user */


scanf("%f %c %f", &num1, &op, &num2);

switch(op)
{
case '+':
result = add(num1, num2);
break;

case '-':
result = sub(num1, num2);
break;

case '*':
result = mult(num1, num2);
break;

case '/':
result = div(num1, num2);
break;

default:
printf("Invalid operator");
}

/* Print the result */


printf("%.2f %c %.2f = %.2f", num1, op, num2, result);

return 0;
}

/**
* Function to add two numbers
*/
float add(float num1, float num2)
{
return num1 + num2;
}

/**
* Function to subtract two numbers
*/
float sub(float num1, float num2)
{
return num1 - num2;
}

/**
* Function to multiply two numbers
*/
float mult(float num1, float num2)
{
return num1 * num2;
}

/**
* Function to divide two numbers
*/
float div(float num1, float num2)
{
return num1 / num2;
}

You might also like