In: Computer Science
area = Length x width
2.a function named volume to calculate the volume of a sphere
volume = 4/3 x 3.14 x radius x radius x radius
3.a function named volume to calculate the volume of a cuboid
volume = Length x width x ht
4. Use a loop structure to calculate the sum of all odd numbers from 1 to 17
5) Use a loop structure to calculate the product of all even numbers from 1 to 13.
6.Write a program, which uses a selection structure (if/else) to calculate and print the student grades.
83 and above is an A
63 and above is a B
53 and above is a C
40 and above is a D
below 40 is fail.
7. Write statements that assign random integers to the variable n in the following ranges: Use the random function to generate the numbers. Put the statements in a Java Program and run it.
Taka = 164*US;
# 1. Calculation of area of rectangle
#include<bits/stdc++.h>
using namespace std;
int area(int length, int width) // function for calculating the area
{
return length*width;
}
int main()
{
int length;
int width;
cout<<"Enter the length of rectangle: ";
cin>>length;
cout<<"Enter the width of rectangle: ";
cin>>width;
cout<<"the area of rectangle is "<<area(length, width);
}
#2. Program for calculating the volume of sphere
#include<bits/stdc++.h>
using namespace std;
double volume(int r) // function for calculating the volume of sphere
{
double vol = (4/3)*3.14*r*r*r;
return vol;
}
int main()
{
int rad;
cout<<"Enter the radius of the sphere: ";
cin>>rad;
cout<<"the area of rectangle is "<<volume(rad);
}
#3 Program to find the volume of the cuboid
#include<bits/stdc++.h>
using namespace std;
int volume(int l, int w, int h)
{
return l*w*h;
}
int main()
{
int length, width, ht;
cout<<"Enter the length of the cuboid: ";
cin>>length;
cout<<"Ether the width of cuboid: ";
cin>>width;
cout<<"Enter the height of cuboid: ";
cin>>ht;
cout<<"the area of rectangle is "<<volume(length, width, ht);
}
#4. Sum of odd number from 1 to 17
#include<bits/stdc++.h>
using namespace std;
int main()
{
int sum =0;
for(int i=1; i<=17; i++){
if(i%2==1){ // it will check whether i is odd or not
sum += i; // adding the odd values
}
}
cout<<sum;
}
#5. Product of all even number from 1 to 13
#include<bits/stdc++.h>
using namespace std;
int main()
{
int product = 1;
for(int i=1; i<=13; i++)
{
if(i%2==0){ // it will check whether i is even or not
product *= i;
}
}
cout<<product;
}
#6 Calculation and printing the grades:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int marks;
cout<<"Enter your marks: ";
cin>>marks;
if(marks>=83){
cout<<"Your grade is : A";
}
else if(marks>=63){
cout<<"Your grade is : B";
}
else if(marks>=53){
cout<<"Your grade is : C";
}
else if(marks>=40){
cout<<"Your grade is : D";
}
else{
cout<<"You are Fail";
}
}
#7. Random Number in given range
import java.io.*;
import java.util.Random; // for Random() function
class GFG {
public static void main (String[] args) {
Random rand = new Random();
// Generate random integers in range 1 to 51
int randInt1 = rand.nextInt(51)+1;
System.out.println("Random Integers range 1 to 51: "+randInt1);
// Generate random integers in range 1 to 900
int randInt2 = rand.nextInt(900)+1;
System.out.println("Random Integers range 1 to 900: "+randInt2);
//Generate random integers in range 0 to 391
int randInt3 = rand.nextInt(392);
System.out.println("Random Integers range 0 to 391: "+randInt3);
//Generate random integers in range 1000 to 5128
int randInt4 = 1000 + rand.nextInt(4128) + 1000;
System.out.println("Random Integers range 1000 to 5127: "+randInt4);
//Generate random integers in range -17 to 22
int randInt5 = rand.nextInt(40)-17;
System.out.println("Random Integers range -17 to 22: "+randInt5);
}
}
#8. coin Tossing
import java.io.*;
import java.util.Random;
class GFG {
public static void main (String[] args) {
Random rand = new Random();
int res = 0;
int headCount = 0;
int tailCount = 0;
for(int i=0; i<100; i++){
res = rand.nextInt(2); // it will generate only 0 and 1
if(res==0){
tailCount++;
}
else{
headCount++;
}
}
System.out.println("Head Count is: "+headCount);
System.out.println("Tail count is: "+tailCount);
}
}
#9. Dollar to Pakistani rupees
#include <iostream>
using namespace std;
int main() {
int dollar;
cout<<"Enter your Dollar value: ";
cin>>dollar;
int taka = 164*dollar;
cout<<dollar<<" Dollar is "<<taka<<" Pakistani taka.";
return 0;
}