Physics Assignment Exercise 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Assignment

on

Computational Physics

Submitted to: Dr. Madiha


Submitted by: Naila Mehr
Roll No: 0101-201415
BS Education
Science Group
Semester 2

Lahore College for Women University


Q:1 Find out error if any into the following programs.
a) #include<iostream.h>
main()
{
int a;
cout<<“Enter any no.?”;
cin>>a;
if (a>10);
cout<<“Value is greater than 10”;
else
cout<<“Value is less than 10”;
}

Errors:
 ‘else’ is written without a previous ‘if’
 ‘if’ is not recognized because of ; at the end of if statement

b) #include<iostream.h>
main()
{
int a;
cout<<“Enter any number?”;
cin>>a;
switch(a>10);
}
{
case 1:
cout<<“One”; break;
case 2:
cout<<“Two”; break;
else
cout<<“value is greater than 2”;
}
}

Errors:
 Else should be replaced with “default”
 No if before else
 semi colon after switch
c)
#include<iostream.h>
main()
{
int a;
cout<<“Enter any no.?”;
cin>>a;
if (a>100)
cout<<“Value is greater than 100”;
cout<<“Ok”;
else
cout<<“Value is less than 100”;
}

Errors:
“No Errors”
Q:2 Write the output of the following programs.
a)
#include<iostream.h>
main()
{
int a,b,c;
a=b=10;
c=13;
if(a = = b & & a > b)
cout<<“Condition is true”<<endl;
else
{
cout<<“a is equal to b”<<endl;
cout<<“a is not greater than b”<<endl;
}
Cout<<“Ok”;
}
Output:
a is equal to b
a is not greater than b
Ok

b)
#include<iostream.h>
main()
{
int ac;
ac=15;
if(ac%2==0)
cout<<“Number is even”<<endl;
else
cout<<“Number is odd”<<endl;
cout<<“Ok”;
}
Output:
Number is odd
Ok

c)
#include<iostream.h>
main()
{
int ac,b=6,c=7;
ac=15;
if(ac%2==0)
if(b>c)
cout<<“b is greater than c and ac is even”<<endl;
else
cout<<“b is less than c and ac is even”<<endl;
else
cout<<“Number is odd”<<endl;
}
Output:

Number is odd

Q:5
Define the following

“if” statement:
“The if statement is used to
execute or ignore a set of
statements after
testing a condition”

“switch statement”:
“The switch statement is used as
a substitute of “nested if-else”
statements. It is used when
multiple choices are given and one
choice is to be selected.
“Nested if statement”:
“When an “if statement” is
used within another “if
statement”, it is called the
“nested if statement”. It is
used for multi-way decision
making.
Q: 4
Write a program in C++ to input a single letter in a
char variable. If “m” is input print “You are male”
otherwise print “You are female” by using conditional
expression operator.
Program:

#include<iostream.h>
main()
{
char OP;
cout<<Enter character;
cin>>OP;
switch(OP)
{
case “m”:
cout<<“You are male”;
default:
cout<<“You are female”;
}
getch
}

Q: 5
Write a program in C++ to input four integers. Find out
the largest value and then print it on the screen by
using nested if structure.
Program:

#include<iostream>
#include<stdio.h>
using namespace std;
int main( )
{
int a,b,c,d;
cout<<"enter 4 different variables";
cin>>a>>b>>c>>d;
if(a>b&&a>c&&a>d)
cout<<"the largest value is :"<<a;
else if (b>a&&b>c&&b>d)
cout<<"the largest value is :"<<b;
else if(c>a&&c>b&&c>d)
cout<<"the largest value is :"<<c;
else
cout<<"the largest value is :"<<d;
}
Q: 6 Write a program in C++ to input marks obtained by
a student in a subject. The total marks are 100. Find out
the grade of a student by using the if-else nested
structure. The grade is
If marks are equal to or greater than 90, grade is “A+”.
If marks are equal to or greater than 70 and less than
90, grade is “A”.
If marks are equal to or greater than 50 and less than
70, grade is “B”.
If marks are less than 50, grade is “F”.

Program:

#include<iostream.h>
main()
{
Int marks;
cout<<“Enter marks”;
cin>>“Marks”;
if(Marks>=90);
cout<<“A+”;
else if(marks>=70&&marks<90);
cout<<”A”;
else if(marks>=50&&marks<70);
cout<<“B”;
else if(marks<50);
cout<<“F”
else
cout<<“You are fail”;
}

Q: 7 Write a program in C++ to input a single character


and print a message “it’s a vowel” if it is a vowel
otherwise print message “it’s a consonant”. Use if-else
structure and OR (ll) operator.
Program:

#include<iostream.h>
int main()
{
char input;
cout<<“Input a single character”;
cin>>input;
if(input==‘a’||input==‘e’||input==‘i’||input==‘o’||
input==‘u’)
cout<<“It is a vowel”;
else
cout<<“It is a consonant”;
}
Q: 8 Write program 7 in C++ by using switch statement
only.
Program:
#include<iostream.h>
main()
{
char OP;
cin<<OP;
switch(OP)
{
case‘a’||‘A’;
cout<<“It’s a vowel”;
case‘e’||‘E’;
cout<<“It’s a vowel”;
case‘i’||‘I’;
cout<<“It’s a vowel”;
case‘o’||‘O’;
cout<<“It’s a vowel”;
case‘u’||‘U’;
cout<<“It’s a vowel”;
default;
cout<<“it’s a consonant”;
}
}

Q: 9 Write a program in C++ to input a single digit from


0-9 and print the input value in words. For example if
the input value is 0 , then print “Zero”. Use the switch
statement.
Program:
#include<stdio.h>
#include<iostream>
using namespace std;
main()
{
int a;
cout<<"enter number";
cin>>a;
switch(a)
{
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;
default:
cout<<"Number out of range";
}
}

You might also like