0% found this document useful (0 votes)
90 views29 pages

Program File of Computer: Science

The document contains code for 20 programs that demonstrate simple C++ programs using concepts like variables, data types, input/output, arithmetic and logical operators, conditional statements, loops and functions. The programs cover basic calculations like addition, subtraction, multiplication; calculations of areas of geometric shapes; if-else conditional statements to check conditions; switch case statements; and a basic calculator program. Each program is accompanied by sample input/output. The programs are intended to teach basic C++ programming concepts.

Uploaded by

Mƛnishƛ Kéim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views29 pages

Program File of Computer: Science

The document contains code for 20 programs that demonstrate simple C++ programs using concepts like variables, data types, input/output, arithmetic and logical operators, conditional statements, loops and functions. The programs cover basic calculations like addition, subtraction, multiplication; calculations of areas of geometric shapes; if-else conditional statements to check conditions; switch case statements; and a basic calculator program. Each program is accompanied by sample input/output. The programs are intended to teach basic C++ programming concepts.

Uploaded by

Mƛnishƛ Kéim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program File of Computer Science

Submitted by: Manisha Keim XI B 27

SIMPLE PROGRAMS

1. Addition of two numbers


// Addition of two numbers #include<iostream.h> #include<conio.h> void main() { int a,b,c; cout<<"Enter the value of a: "; cin>>a; cout<<"Enter the value of b: "; cin>>b; c=a+b; cout<<"Addition of two numbers: "; cout<<c; getch(); } OUTPUT: Enter the value of a: 4 Enter the value of b: 5 Addition of two numbers: 9

2. Subtraction of two numbers // Subtraction of two numbers #include<iostream.h> #include<conio.h> void main() { int a,b,c; cout<<"Enter the value of a"; cin>>a; cout<<"Enter the value of b"; cin>>b; c=a-b; cout<<"Subtraction of two numbers"; cout<<c; getch(); } OUTPUT: Enter the value of a6 Enter the value of b5 Addition of two numbers1

3. Multiplication of two numbers // Multiplication of two numbers

#include<iostream.h> #include<conio.h> void main() { int a,b,c; cout<<"Enter the value of a"; cin>>a; cout<<"Enter the value of b"; cin>>b; c=a*b; cout<<"Multiplication of two numbers"; cout<<c; getch(); } OUTPUT: Enter the value of a4 Enter the value of b5 Addition of two numbers20

AREA PROGRAMS

4. Area of triangle //Area of Triangle #include<iostream.h>

#include<conio.h> void main() { clrscr(); int b,h; float a; cout<<"Enter the base of triangle"; cin>>b; cout<<"Enter the height of triangle"; cin>>h; a=0.5*b*h; cout<<"Area of triangle"; cout<<a; getch(); } OUTPUT: Enter the base of triangle 4 Enter the height of triangle 5 Addition of two numbers 9

5. Area of Rectangle //Area of Rectangle #include<iostream.h> #include<conio.h> void main() { int l,b,a; cout<<"Enter the length";

cin>>l; cout<<"Enter the breadth"; cin>>b; a=l*b; cout<<"Area of Rectangle"; cout<<a; getch(); } OUTPUT: Enter the length 9 Enter the breadth 8 Area of Rectangle 72

6. Area of Circle //Area of Circle #include<iostream.h> #include<conio.h> void main() { float r,a; cout<<"Enter the radius of circle"; cin>>r; a=3.14*r*r; cout<<"Area of Circle"; cout<<a; getch(); } OUTPUT: Enter the radius of circle Area of Circle

7. Area of Square //Area of Square #include<iostream.h> #include<conio.h> void main() { int a,l; cout<<"Enter the length"; cin>>l; a=l*l; cout<<"Area of Square"; cout<<a; getch(); } OUTPUT: Enter the length 4 Area of Square16

8. Surface Area of Sphere //Surface Area of Sphere #include<iostream.h> #include<conio.h> void main() { int r; float E; cout<<Enter the radius of sphere; cin>>r; E=4*3.14*r*r; cout<<Surface Area of Sphere; cout<<E; getch(); } OUTPUT: Enter the radius of sphere 5 Surface area of Sphere 314

IF ELSE PROGRAMS
9. Calculating Age
//Calculating Age #include<iostream.h> #include<conio.h> void main() { int age; cout<<"Please input your age"; cin>>age; if(age<100) { cout<<"You are pretty young!\n"; } else if (age==100) { cout<<"You are old\n"; } getch(); }

OUTPUT: Please input your age 21 You are pretty young Please input your age 95 You are old

10.

Calculating Test Score

//Calculating Test Score


#include<iostream.h> #include<conio.h> void main() { int testScore; cout << "Enter your test score: "; cin >> testScore; if (testScore >= 90 ) cout << "Your grade is an A"; else if (testScore >= 80 ) cout << "Your grade is a B"; else if (testScore >= 70 ) cout << "Your grade is a C"; else if (testScore >= 60 ) cout << "Your grade is a D"; else cout << "Your grade is an F"; getch(); }

OUTPUT: Enter you test score 90 Your grade is an A

11. To check whether the given whole number is even or odd


//Odd or Even #include <iostream.h> #include<conio.h> void main() { clrscr(); int num; cout << "Enter a whole number: "; cin >> num; if ( num % 2 == 0 ) cout << "The number is even"; else cout << "The number is odd" ; getch(); }

OUTPUT: Enter a whole number: 7 The number is odd Enter a whole number: 10 The number is even

[Link] the smallest number from the two numbers

//Finding Smallest Number #include <iostream.h> #include<conio.h> void main() { clrscr(); float x, y, min; cout << "Enter two different numbers:\n"; if( cin >> x && cin >> y) { if( x < y ) min = x; else min = y; cout << "\nThe smaller number is: " << min; } else cout << "\nInvalid Input!"; getch(); } OUTPUT: Enter two different numbers: 56 65 The smaller number is: 56

13.

Teenage or Old

//Teenage or old #include<iostream.h> #include<conio.h> void main() { clrscr(); int age; cout << "Enter your Age:"; cin >> age; if (age >=10 && age<=50) { if (age >=10 && age <= 18) cout << "You are a Teenage"; else cout << "You not a Teenager"; } else cout << "You are more than 50 years"; getch(); } OUTPUT: Enter your age: 15 You are a Teenage

14.

You have to pay or not

#include <iostream.h> #include<conio.h> void main() { int age; cout << "Enter your age: "; cin >> age; if (age > 7) if (age >= 70) cout << "free"; else cout << "You have to pay"; else cout << "free"; getch(); }

OUTPUT: Enter your age: 65 You have to pay

15.

Menu for food

//Menu for food #include<iostream.h> #include<conio.h> void main() { clrscr(); int num; cout << "MENU\n\n"; cout << "1." << "\t" << "Lobster\n"; cout << "2." << "\t" << "Steak\n"; cout << "3." << "\t" << "Turkey\n"; cout << "4." << "\t" << "Hamburger\n"; cout << "5." << "\t" << "Vegetarian\n\n"; cout << "Choose your dinner entry: "; cin >> num; if (num == 1) { cout << "Lobster is my favorite! Dig in!\n\n"; } else if (num == 2) { cout << "Yummy! Steak is great...\n"; } else if (num == 3) { cout << "Turkey is healthier than steak! ...Enjoy!\n\n"; } else if (num == 4) { cout << "Hamburger is another form of steak. :-)\n\n"; } else if (num == 5) { cout << "Finally, a vegitarian is in the house!\n\n"; }

else { cout << "Invalid number, please enter a number" << " from the entries above.\n\n"; } getch(); } OUTPUT: MENU 1. Lobster 2. Steak 3. Turkey 4. Hamburger 5. Vegetarian Choose your dinner entry: 2 Yummy! Steak is great...

16. If Alphabet or not //Alphabet #include<iostream.h> #include<conio.h> void main()

{ clrscr(); char character; int number = 0; cout << "Enter a keyboard character: "; cin >> character; if ((character >= 'A') && (character <= 'Z')) { cout << "The character IS an alphabetic character!\n\n"; } else if ((character >= 'a') && (character <= 'z')) { cout << "The character IS an alphabetic character!\n\n"; } else if (number >= 0) { cout << "The character is NOT a alphabetic character.\n\n"; } getch(); } OUTPUT: Enter a keyboard character: a The character IS an alphabetic character!

17.

Whether the number is greater or less than 10

//Greater than or less than 10 #include<iostream.h>

#include<conio.h> void main() { clrscr(); int num; cout<<"Enter any number"; cin>>num; if(num>10) { cout<<"Number is greater than 10"; } else { cout<<"Number is less than 10"; } getch(); } OUTPUT: Enter any number89 Number is greater than 10

18.

Calculating age of next year

// Calculating age of next year #include <iostream.h> #include<conio.h> void main() { clrscr(); int a, b, c, d; cout << "Enter the current year.\n"; cin >> a; cout << "Enter your current age in years.\n"; cin >> b; cout << "Enter the year for which you wish to know your age.\n"; cin >> c; d = c - (a - b); if (d >= 0) { cout << "Your age in " << c << ": "; cout << d << "\n"; } else { cout << "You weren't even born in "; cout << c << "!\n"; } getch(); } OUTPUT: Enter the current year. 2011 Enter your current age in years. 15 Enter the year for which you wish to know your age. 2013 Your age in 2013: 17

19.

Temperature of the day

//Temperature #include<iostream.h> #include<conio.h> void main() { clrscr(); float temp; cout<<"What is the temperature outside?"; cin>>temp; if(temp < 65) { cout<<"It's a bit chilly out!\n"; } else { cout<<"How pleasant!"; } getch(); } OUTPUT: What is the temperature outside?23 It's a bit chilly out!

[Link] Calculator //Calculator #include<iostream.h> #include<conio.h> void main() { char ch; float number1, number2, result; cout<<"Enter two numbers:"; cin>>number1>>number2; cout<<"\n"<<"Enter the operator(+,_,*,/):"; cin>>ch; cout<<"\n"; if(ch == '*') result = number1 * number2; else if(ch == '/') result = number1 / number2; else if(ch == '+') result = number1 + number2; else if(ch == '-') result = number1 - number2; else cout<<"Invalid Operator"; cout<<"\n"The calculated result is:; cin>>result; getch(); } OUTPUT: Enter two numbers: 4 5 Enter the operator(+,_,*,/):+ The calculated result is:9

SWITCH PROGRAMS
21. Calculate Type of Areas
//Calculate Type of Areas #include<iostream.h> #include<math.h> int main() { float area,rad,len,bre,a,b,c,s; int ch; cout<<"Area Menu"<<"\n"; cout<<"[Link]"<<"\n"; cout<<"[Link]"<<"\n"; cout<<"[Link]"<<"\n"; cout<<"Enter your choice:"; cin>>ch; cout<<"\n"; switch(ch) { case 1:cout<<"Enter radius of the circle:"; cin>>rad; area=3.14*rad*rad; cout<<"\n"<<"The area of circle is:"<<area<<"\n"; break; case 2:cout<<"Enter length and breadth of rectangle:"; cin>>len>>bre; area=len*bre; cout<<"\n"<<"The area of rectangle is"<<area<<"\n"; break; case 3:cout<<"Enter three sides of a triangle:"; cin>>a>>b>>c; s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<"\n"<<"The area of triangle is"<<area<<"\n"; break; default: cout<<"Wrong choice!!!!"; break; } return 0; } OUTPUT: Area Menu [Link] [Link] [Link] Enter your choice: 2 Enter length and breadth of rectangle: 4 3 The area of rectangle is12 Area Menu [Link] [Link] [Link] Enter your choice:3 Enter three sides of a triangle:4 3 2.5 The area of triangle is3.74531

22. Prepare Calculator


//To Prepare Calculator #include<iostream.h> #include<conio.h> void main() { clrscr(); int result, val2; char ch; int val1; cout << "Enter operator(+,-,*,/) and number:"; cin >>ch >>val1>>val2; switch (ch) { case '+': result=val1+val2; break; case '-': result=val1-val2; break; case '*': result=val1*val2; break; case '/': if (val2 == 0) { cout <<"Error:Divide by zero\n"; cout <<" operation ignored\n"; } else result=val1/val2; break; default: cout <<"Unknown operator"<<ch<<'\n';

break; } cout << "Result: "<< result <<'\n'; getch(); } OUTPUT: Enter operator(+,-,*,/) and number:+ 4 5 Result: 9

23. Demonstrates Switch Statement


//Demonstrates Switch Statement #include <iostream.h> #include<conio.h> int main()

{ int number; cout << "Enter a number between 1 and 5: "; cin >> number; switch (number) { case 0: cout << "Too small, sorry!"; break; case 5: cout << "Good job!\n"; case 4: cout << "Nice Pick!\n"; case 3: cout << "Excellent!\n"; case 2: cout << "Masterful!\n"; case 1: cout << "Incredible!\n"; break; default: cout << "Too large!\n"; break; } cout << "\n\n"; return 0; } OUTPUT: Enter a number between 1 and 5: 3 Excellent! Masterful! Incredible! Enter a number between 1 and 5: 8 Too large!

24. Switch Program


//Switch Program #include <iostream.h> #include<conio.h> int main () { char permit; cout << "Are you sure you want to quit? (y/n) : ";

cin >> permit; switch (permit) { case 'y' : cout << "Hope to see you again!"; break; case 'n' : cout << "Welcome back!"; break; default: cout << "What? I don't get it!"; } return 0; } OUTPUT: Are you sure you want to quit? (y/n): y Hope to see you again! Are you sure you want to quit? (y/n): n Welcome back!

25. Program that counts vowels and consonants

You might also like