Hacking Equipments | C/C++ Coding | Updates:: Did you tried Our Online ? AdobePhotoshop |

Goto Top

[Solutions] Variable - Operator & Expressions Assignment 1

These are solved questions for the assignment number 1 of variable - operator - expressions ,

Q1: Write a Program to print "SALAM WORLD" on Screen.

Code:

#include <stdio.h>
int  main(){

 printf("SALAM WORLD");

 return 0;

}

Logic & Note: 

  • Used Printf() function to print from directory of stdio.h 



Q2 :Write a Program to Print the following Output using only 1 statement.

Student               Marks
1                        100
2                        50
3                        30

Code :

#include <iostream.h>
void main(){
 cout<<"Student\t\tMarks\n1\t\t100\n2\t\t50\n3\t\t30\n";
}

Logic & Note: 

  •  \t , \n , \r , \b these are some commonly used Escape Sequences. e.g \n is used to go to next line and \t used for 1 tab . 
  • cout<< it is a stream printer . 


Q3 :Write a Code to display a Number 4569.988463 upto 2 decimal places on screen , the number should be displayed as : 4569.98 (* The program should not contain more than 2 statements)

 Code :

#include <stdio.h>
void main(){
 float f=4569.988463;
 printf("This Is The Number :=: %0.2f",f);
}

Logic & Note: 

  • Used void main to save a statement for return 0; 
  • Now basically %f is Format specifier used to display in printf function with double quotes, as i've used as %0.2f i am indicating it that round off mantissa up to 2 digits , don't care about the 0.2 0 part because it is stretched up to the number length but if you give it greater the rest will results to print space character. 
Q4 : Write a program to print Area of a circle.


Code :

#include <iostream.h>
int main(){
 double r,area; // similar to float but can hold upto 14 decimal places , 8 bytes 
 cout<<"Enter Radius :=:";
 cin>>r;
 cout<<"\n";
 area=r*r;
 cout<<"Area Of The Circle :=:"<<area<<"\n\n";
 return 0;
}

Logic & Note: 

  • Area= radius^2 . 
  • double is my favourite datatype , i use it instead of float. 
  • cout<< used to putting string/number to console , cin>> getting input from console . 



Q5 :Write A program to get two numbers from user and Print their sum on screen.

Code :

#include <iostream.h>
int main(){
 double a,b,sum; // numbers can be float or integers

 cout<<"Number 1 :=:"; cin>>a; cout<<"\n";
 cout<<"Number 2 :=:"; cin>>b; cout<<"\n";

 sum=a+b;

 cout<<"Sum :=:"<<sum<<"\n\n";

 return 0;
}

Logic & Note: 

  • sum= a+b for adding , & numbers can be integer/float/double so don't take a risk i've said two numbers !! not integers or floats 
  • Use inputting as i've used in a single line it will give a good impact/and less line of code. 


Q6:Write a program the get temperature in centigrade and Convert it into fahrenheit .

Code :

#include <iostream.h>
int main(){
 double cen,fah; // Numbers can be float or integers

 cout<<"Temperature in Centigrade :=:"; cin>>cen; cout<<"\n";

 fah = (5/9)*cen+32; // According to DMAS , multiplication 1st then addition so no brackets used

 cout<<"Temperature in Fahrenheit :=:"<<fah<<"\n\n";

 return 0;
}

Logic & Note : 

  •  fahrenheit = ((5/9)*Centigrade)+32 , used to find keep in mind that DMAS rule will always apply when performing calculation but parenthesis part will always run first because it have greater precision, for more precision info visit : Here 


Q7:Write a program That inputs number and Show its ASCII value.

Code :

#include <iostream.h>
int main(){
 char c;
 int d;

 cout<<"Enter Character :=:"; cin>>c; cout<<"\n";

 d = c; 

 cout<<"Value in ASCII :=:"<<d<<"\n\n";

 //cout<<"Value in ASCII :=:"<<(int)c<<"\n\n";

 return 0;
}

Logic & Note : 

  • As c contains a character and each character have ASCII value basically when saved in memory, 
  • i've assigned character to integer it will always perform function with its ASCII value so , i got ascii value. 
  • There are More flexible ways of doing it is type casting ,, putting data type in parentheses as i've done (int) . in last line. 


Q8:Write a Program To input two values and Swap Values of that two variables and Print them .

Code :

#include <iostream.h>
int main(){
 int a,b,temp;

 cout<<"Enter Value a :=:"; cin>>a; cout<<"\n";
 cout<<"Enter Value b :=:"; cin>>b; cout<<"\n";

 temp=a;
 a=b;
 b=temp;

 cout<<"Value a :=:"<<a<<"\n\n";
 cout<<"Value b :=:"<<b<<"\n\n";
 
 return 0;
}

Logic & Note: 

  • What i've done is just used another variable temp , assigned value of 'a' to 'temp' , and now i've saved value of 'a' now i can change it , now assigning 'b' to 'a' , and finally assigning 'temp' to 'b' , which is actually 'a' . 


Q9 :Write a Program which get number and Print the Number is +ve or -ve on screen. (*using ternary operator)

Code :

#include <iostream.h>
int main(){
 int Num;

 cout<<"Enter Your Number :=:"; cin>>Num; cout<<"\n";
 
 (Num>0)? cout<<"::Number is Positive::\n": cout<<"::Number is Negative::\n";

 return 0;
}

Logic & Note : 

  • if the number will be greater than zero it is positive otherwise it is negative. 
  • ternary operator have following syntax: (condition)?run if true:run if false; variable=(condition)?run if true:run if false do not use ; with middle part its an error . 
Q10:Write a Program which get number and print whether it is +ve or -ve and it is even or odd .(*using ternary operator) .

Code :

#include <iostream.h>
int main(){
 int Num;

 cout<<"Enter Your Number :=:"; cin>>Num; cout<<"\n";
 
 (Num>0)? cout<<"::Number is Positive::\n": cout<<"::Number is Negative::\n";
 (Num%2==0)? cout<<"::Number is Even::\n" : cout<<"::Number is Odd::\n";

 return 0;
}

Logic & Note : 

  • if a number remainder with 2 will be zero then it will be even otherwise if 1 then it is odd. (% is reminder sign)


Corrections & errors are accepted kindly share , More Assignments coming soon :p

0 Comments :

Post a Comment

Having Confusion ,oH Dear ask me in comments!!

Related Posts Plugin for WordPress, Blogger...
 

About Admin

I am a pro-programmer of C++ ,php i can crack some softwares and am a web desighner .I AM also on .


| Solve Byte © 2011 - 2016. All Rights Reserved | Back To Top |