Assignment#02
Name: Nawaf Rasheed
Reg.no: 50816
Program: BS(SE)
Date: 05 August 2020
Subject: Programming Fundamentals LAB
Question: 01:
Answer:
include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// True if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
Output
Enter an integer: -7
-7 is odd.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num);
return 0;
}
Output
Enter an integer: 33
33 is odd.
Question:02:
Answer:
#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0) {
if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
} else
printf("You entered a positive number.");
return 0;
}
#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num < 0.0)
printf("You entered a negative number.");
else if (num > 0.0)
printf("You entered a positive number.");
else
printf("You entered 0.");
return 0;
}
Output 1
Enter a number: 12.3
You entered a positive number.
Output 2
Enter a number: 0
You entered 0.
Question:03:
Answer:
#include <iostream>
using namespace std;
int main()
{
int i,sum=0;
cout << "\n\n Find the first 10 natural numbers:\n";
cout << "---------------------------------------\n";
cout << " The natural numbers are: \n";
for (i = 1; i <= 10; i++)
{
cout << i << " ";
sum=sum+i;
}
cout << "\n The sum of first 10 natural numbers: "<<sum << endl;
}
Sample Output:
Find the first 10 natural numbers:
---------------------------------------
The natural numbers are:
1 2 3 4 5 6 7 8 9 10
The sum of first 10 natural numbers: 55
Question.04:
Answer:
#include <iostream>
using namespace std;
int main()
{
int i, ctr, cub;
cout << "\n\n Display the cube of the numbers upto a given integer:\n";
cout << "----------------------------------------------------------\n";
cout << "Input the number of terms : ";
cin >> ctr;
for (i = 1; i <= ctr; i++)
{
cub = i * i * i;
cout << "Number is : " << i << " and the cube of " << i << " is: " <<
cub << endl;
}
}
Display the cube of the numbers upto a given integer:
----------------------------------------------------------
Input the number of terms : 5
Number is : 1 and the cube of 1 is: 1
Number is : 2 and the cube of 2 is: 8
Number is : 3 and the cube of 3 is: 27
Number is : 4 and the cube of 4 is: 64
Number is : 5 and the cube of 5 is: 125
Question.05:
Answer:
#include <stdio.h>
void main()
int j,n;
printf("Input the number (Table to be calculated) : ");
scanf("%d",&n);
printf("\n");
for(j=1;j<=10;j++)
{
printf("%d X %d = %d \n",n,j,n*j);
Sample Output:
Input the number (Table to be calculated) : 15
15 X 1 = 15
15 X 2 = 30
15 X 3 = 45
15 X 4 = 60
15 X 5 = 75
15 X 6 = 90
15 X 7 = 105
15 X 8 = 120
15 X 9 = 135
15 X 10 = 150
Question.06:
Answer:
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
The output of all these programs above will be the same.
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
Question.08:
Answer:
#include <stdio.h>
void main()
{ int p,c,m,t,mp;
printf("Eligibility Criteria :\n");
printf("Marks in Maths >=65\n");
printf("and Marks in Phy >=55\n");
printf("and Marks in Chem>=50\n");
printf("and Total in all three subject >=180\n");
printf("or Total in Maths and Physics >=140\n");
printf("-------------------------------------\n");
printf("Input the marks obtained in Physics :");
scanf("%d",&p);
printf("Input the marks obtained in Chemistry :");
scanf("%d",&c);
printf("Input the marks obtained in Mathematics :");
scanf("%d",&m);
printf("Total marks of Maths, Physics and Chemistry : %d\n",m+p+c);
printf("Total marks of Maths and Physics : %d\n",m+p);
if (m>=65)
if(p>=55)
if(c>=50)
if((m+p+c)>=180||(m+p)>=140)
printf("The candidate is eligible for admission.\n");
else
printf("The candidate is not eligible.\n");
else
printf("The candidate is not eligible.\n");
else
printf("The candidate is not eligible.\n");
else
printf("The candidate is not eligible.\n");
Copy
Sample Output:
Eligibility Criteria :
Marks in Maths >=65
and Marks in Phy >=55
and Marks in Chem>=50
and Total in all three subject >=180
or Total in Maths and Physics >=140
-------------------------------------
Input the marks obtained in Physics :65
Input the marks obtained in Chemistry :51
Input the marks obtained in Mathematics :72
Total marks of Maths, Physics and Chemistry : 188
Total marks of Maths and Physics : 137
The candidate is eligible for admission.