C Programs Answers

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

THE UNIVERSITY OF LAHORE

Department of Computer Engineering


Programming Assignments for Introduction
to Computing
1. Write a program to perform the arithmetic operations by using all
arithmetic operators. Also print the results on the screen.
Answer:
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
void main()
{
int a,b,div,mul,add,sub;
cout<<"enter 1st value:";
cin>>a;
cout<<"enter 2nd value:";
cin>>b;
div=a%b;
mul=a*b;
add=a+b;
sub=a%b;
cout<<"\n division:"<<div;
cout<<"\n multiplication:"<<mul;
cout<<"\n addition:"<<add;
cout<<"\n subtraction:"<<sub;
getch();
}

2. Write a program to calculate the radius of circle (radius=r2)

3. Write a program in which values are assigning to two different


variables. Interchange these values using three variables and show the
result on the screen.

4. Write a program to assign the numeric value to a variable year.


Calculate the number of months and print the result on the screen.

5. Write a program in which values are assigning to two different


variables. Interchange these values using two variables and show the
result on the screen.

6. Write a program to input a number. If the number is divisible by 3 then


print the message on the screen that the number is divisible by three.
Use if statement.
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
void main()
{
int a;
cout<<"enter any number:";
cin>>a;
if(a%3==0)
cout<<"the number is divisible by 3";
else
cout<<"number is not divisible by 3";
getch();
}
7. Write a program to input two integer values and find out whether the
first or second number is greater.

8. Write a program to calculate the electricity bill.

9. Write a program to input a number from the keyboard. Use if-else


statement to find out whether the number is less than or greater than
100.
Answer:
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
void main()
{
int a;
cout<<"enter any number:";
cin>>a;
if(a>=100)
cout<<"the number is greater than 100";
else
cout<<"the number is less than 100";
getch();
}

10. Write a program to input three integers and find maximum from these
integers.
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
void main()
{

int a,b,c;
cout<<"enter 1st integer:";
cin>>a;
cout<<"enter 2nd integer:";
cin>>b;
cout<<"enter 3rd integer:";
cin>>c;
if((a>=b)&(a>=c))
{
cout<<a<<"is greater than"<<b<<"and"<<c<<endl;
}
else
if((b>=a)&(b>=c))
{
cout<<b<<"is greater than"<<a<<"and"<<c<<endl;
}
else
if((c>=a)&(c>=b))
{
cout<<c<<"is greater than"<<a<<"and"<<b<<endl;
}
else{}
getch();
}

11. Write a program to input three integers and find minimum from these
integers.
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
void main()
{
int a,b,c;
cout<<"enter 1st integer:";
cin>>a;
cout<<"enter 2nd integer:";
cin>>b;

cout<<"enter 3rd integer:";


cin>>c;
if((a<=b)&(a<=c))
{
cout<<a<<"is less than"<<b<<"and"<<c<<endl;
}
else
if((b<=a)&(b<=c))
{
cout<<b<<"is less than"<<a<<"and"<<c<<endl;
}
else
if((c<=a)&(c<=b))
{
cout<<c<<"is less than"<<a<<"and"<<b<<endl;
}
else{}
getch();
}

12. Write a program which tells that number is Even or Odd.

13. Write a C++ program to generate the results as shown below:


Results: =======Quizzes===============
Enter the score of the first quiz: 90
Enter the score of the second quiz: 75
Enter the score of the third quiz: 91
=======Mid-term==============
Enter the score of the mid-term: 80
=======Final=================
Enter the score of the final: 89
Quiz Total:
256
Mid-term :
80
Final
:
89

Total:
425

Answer
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a, b, c, d, e, f, g;
cout<<"Results"<<endl<<"========Quiz========"<<endl;
cout<<"Enter the score of the first quiz:";
cin>>a;
cout<<"Enter the score of the secind quiz:";
cin>>b;
cout<<"Enter tne score of the third quiz:";
cin>>c;
cout<<"========Mid-Term========"<<endl;
cout<<"Enter the score of mid-term:";
cin>>d;
cout<<"========Final========"<<endl;
cout<<"Enter the score of the final:";
cin>>e;
f=a+b+c;
cout<<"Quiz Total: "<<f<<endl<<"Mid-Term: "<<d<<endl<<"Final:
"<<e<<endl;
g=d+e+d;
cout<<"............................"<<endl<<"Total:
"<<g<<endl;
getch();
}

14. Write a C++ program that prompts the user to input three integer
values and find the greatest value of the three values.
Example:
Enter 3 integer vales separated by space: 10 15 20
The greatest value is: 20

Answer

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"Enter 1st integer:";
cin>>a;
cout<<"Enter the 2nd integer:";
cin>>b;
cout<<"Enter the 3rd integer:";
cin>>c;
cout<<"Enter interger seperated by space: "<<a<<" "<<b<<" "<<c<<endl;
if ((a>=b)&(a>=c))
{
cout<<"The greatest value is "<<a<<endl;
}
else if ((b>=a)&(b>=c))
{
cout<<"The greatest value is "<<b<<endl;
}

else if ((c>=b)&(c>=a))
{
cout<<"The greatest value is "<<c<<endl;
}
else{}
getch();
}

15. Write a program that determines a students grade. The program will
read three types of scores (quiz, mid-term, and final scores) and
determine the grade based on the following rules:
-if the average score =90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
Answer

#include<iostream.h>
#include<conio.h>
void main()
{

float x=0;
float y=0;
float z=0;
float avg=0;
cout<<"Enter 3 score(quiz, mid-term and final) vales seperaed
by space:";
cin>>x>>y>>z;
avg=(x+y+z)/3;
if (avg>=90)
{
cout<<"GRADE=A";
}
else if ((avg<90)&(avg>=70))
{
cout<<"GRADE=B";
}
else if ((avg<70)&(avg>=50))

{
cout<<"GRADE=C";
}
else if (avg<50)
{
cout<<"GRADE=F";
}
getch();
}

You might also like