0% found this document useful (0 votes)
18 views

C++ Assignmenta1

The documents contain examples of C++ programs that demonstrate the use of selection statements like if-else and switch case, repetition statements like for loops, functions, and arrays. The programs include calculating GPA based on grades, finding the Fibonacci series, determining the greatest number among three inputs, and more.

Uploaded by

Dejenie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

C++ Assignmenta1

The documents contain examples of C++ programs that demonstrate the use of selection statements like if-else and switch case, repetition statements like for loops, functions, and arrays. The programs include calculating GPA based on grades, finding the Fibonacci series, determining the greatest number among three inputs, and more.

Uploaded by

Dejenie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C++ Program which takes input a grade and display Grade

Points Average GPA


#include<iostream.h>
int main()
{
char grade; double gpa=0.0;
cout<<"Enter your Grade= ";
cin>>grade;
switch(grade)
{
case'A':
case'a':
gpa=4.0;
cout<<"your GPA is "<<gpa;
break;
case'B':
case'b':
gpa=3.0;
cout<<"your GPA is "<<gpa;
break;
case'C':
case'c':
gpa=2.0;
cout<<"your GPA is "<<gpa;
break;
case'D':
case'd':
gpa=1.0;
cout<<"your GPA is "<<gpa;
break;
case'F':
case'f':
gpa=0.0;
cout<<"your GPA is "<<gpa;
break;

default:
cout<<"invalid grade entered";
break;
}
return 0;
}
C++ program to find Fibonacci Series upto given range. This C++ program use simple
logic to get the concept of writing code in C++ for Fibonacci Series

1. #include<iostream.h>
2. int main()
3. {
4. int range, first = 0, second = 1, fibonicci=0;
5. cout << "Enter Range for Terms of Fibonacci Sequence: ";
6. cin >> range;
7. cout << "Fibonicci Series upto " << range << " Terms "<< endl;
8. for ( int c = 0 ; c < range ; c++ )
9. {
10. if ( c <= 1 )
11. fibonicci = c;
12. else
13. {
14. fibonicci = first + second;
15. first = second;
16. second = fibonicci;
17. }
18. cout << fibonicci <<" ";
19. }
20. return 0;
21. }
========================================================================================================================

This program shows the greatest number between three


numbers using if-else-if statement. It takes three numbers as
input from user and output the greatest number.
1. #include<iostream.h>
2. int main()
3. {
4. int num1,num2,num3;
5. cout<<" Enter value for first number";
6. cin>>num1;
7.
8. cout<<" Enter value for second number";
9. cin>>num2;
10.
11. cout<<" Enter value for third number";
12. cin>>num3;
13. if(num1>num2&&num1>num3)
14. {
15. cout<<" First number is greatest:"<<endl<<"whick
is= "<<num1;
16. }
17. else if(num2>num1&&num2>num3)
18. {
19. cout<<" Second number is greatest"<<endl<<"whick
is= "<<num2;
20. }
21. else
22. {
23. cout<<" Third number is greatest"<<endl<<"whick is=
"<<num3;
24. }
25. return 0;
26. }

#Include<include .h>
int main(int argc, char *argv[])
{
//Switches (case Structure) //
int marks;
cout<<"\t\nEnter the makrs = ";
cin>>marks;
cout<<"\t\nCongratulations Your Grade is = ";
switch (marks)
{
case 90:
cout<<"A\n\n\n";
break;
case 80:
cout<<"B\n\n\n";
break;
case 70:
cout<<"C\n\n";
break;
default:
cout<<"Sorry Try Again Student\n\n";
break;
}
system("PAUSE");
return 0;
}
In a company an employee is paid as under:
If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic
salary.
If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic
salary.
If the employee's salary is input by the user write a program to find his gross salary.

Write a program to enter the numbers till the user wants and at the end it should display the
maximum and minimum number entered
Write C++ program to print following pattern:
i) ********** ii) * iii) *
********** ** **
********** *** ***
********** **** ****
***** *****
iv) * v) 1 vi) 1
*** 222 212
***** 33333 32123
******* 4444444 4321234
********* 555555555 543212345

ANSWER
/Solution of (i)
#include<iostream>
using namespace std;

int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (ii)
#include<iostream>
using namespace std;

int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iii)
#include<iostream>
using namespace std;

int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iv)
#include<iostream>
using namespace std;

int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (v)
#include<iostream>
using namespace std;

int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}

return 0;
}

//Solution of (vi)
#include<iostream>
using namespace std;

int main()
{
int i,j,k,l;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=i;k>=1;k--)
cout<<k;
for(l=2;l<=i;l++)
cout<<l;
cout<<endl;
}

return 0;
}
Selection and Repetition Statement Examples
// A C++ program grqade status using switch statemet
1. #include <iostream.h>
2. int main () { // local variable declaration:
3. char grade = 'D';
4. switch(grade)
5. {
6. case 'A' :
7. cout << "Excellent!" << endl;
8. break;
9. case 'B' :
10. case 'C' :
11. cout << "Well done" << endl;
12. break;
13. case 'D' : cout << "You passed" << endl; break;
14. case 'F' :
15. cout << "Better try again" << endl; break;
16. default :
17. cout << "Invalid grade" << endl; }
18. cout << "Your grade is " << grade << endl; return 0;
19. }
A C++ program to calculate students grade using else if statemet
1. #include <iostream.h>
2. int main(){
3. int mark;
4. cout << "What mark did you get in the test?" << endl;
5. cin >> mark;
6. if(mark >= 90)
7. {
8. cout << "You got an A*" << endl;
9. }
10. else if(mark >= 80)
11. {
12. cout << "You got an A" << endl;
13. else if(mark >= 70)
14. {
15. cout << "You got an B" << endl;
16. }
17. else if(mark >= 60)
18. {
19. cout << "You got an C" << endl;
20. cout << "You Passed!" << endl;
21. }
22. else if(mark >= 50)
23. {
24. cout << "You got an D" << endl;
25. }
26. else if(mark >= 40)
27. {
28. cout << "You got an E" << endl;
29. }
30. else
31. {
32. cout << "You got an U" << endl;
33. }
34. return 0;
35. }

Functions Examples
Factorial of a number using function.
#include<iostream.h>
int factorial(int n);
int main ()
{
int n1,fact;
cout <<"Enter the number whose factorial has to be calculated" << endl;
cin >> n1;
fact=factorial(n1);
cout << "The factorial of " << n1 << " is : " << fact << endl;
return(0);
}
int factorial(int n)
{

int i=0,fact=1;

if(n<=1)

return(1);

else
{
for(i=1;i<=n;i++)
{
fact=fact*i;
}
return(fact);
}
}

Function Example2

// A function to add numbers.


#include<iostream>
int add(int n);
int main()
{
int number,result;
number=5;
cout << " The initial value of number : " << number << endl;
result=add(number);
cout << " The final value of number : " << number << endl;
cout << " The result is : " << result << endl;
return(0);
}

int add(int number)


{
number=number+100;
return(number);
}

A function to calculate the// A function to find out the average of the age a class of students.

#include<iostream.h>
int main ()
{
int age[10];
int i,sum=0, avg=0;
int max=0,min=100;
for(i=0;i<10;i++)
{
cout << "Enter the age of the student " << i+1 <<endl;
cin >> age[i];
}
for(i=0;i<10;i++)
{
sum=sum+age[i];
if(age[i]>max)
{
max=age[i];
}
if(age[i]<min)
{
min=age[i];
}
}
avg=sum/10;
cout << "Average age of the students of the class : " << avg << endl;
cout << "Maximum age of the student of the class : " << max << endl;
cout << "Minimum age of the student of the class : " << min << endl;
return(0);
}

===============================================================================================================================

#include<iostream>
using namespace std;
int main ()
{
int age[2][5]= { {12,13,14,15,15}, { 12,16,17,13,12}};
int i,j;
int sum=0,avg=0;
for(i=0;i<2;i++)
{
for(j=0;j<5;j++)
{
sum=sum+age[i][j];
}
avg=sum/5;
cout << "Average of the elements of the row " << i+1 << " is " << avg << endl;
sum=0;
}
return(0);
}

Array Examples
/*Write a program which stores the marks of five students.
#include<iostream.h>
#include<conio.h>
void main()
{
int marks[5]={75,85,80,95,90};
cout<<”The marks of the students are:”<<endl;
for (int i=0;i<5;i++)
cout<<”The marks “<<i<<”students is”<<marks[i]<<endl;
}
=============================================================================
/*Write a program which initializes a two dimensional array and print all the element*/
#include<iostream.h>
#include<conio.h>
void main()
{
int a[2][3]={1,2,3,4,5,6};
int i,j;
cout<<"The array is:\n";
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cout<<a[i][j];
cout<<endl;
}
getch();
}
==================================================================================
/*Write program which reads two matrix and then print a matrix which is addition of the two matrix */
#include<iostream.h>
#include<conio.h>
void main()
{
int a[2][3],b[2][2],s[2][2],i,j;
cout<<"Enter the first matrix:"<<endl;
for(i=0;i<=1;i++)
for(j=0;j<=2;j++)
{
cout<<"Enter"<<(i+1)<<(j+1)<<"elemet:";
cin>>a[i][j];
}
cout<<"Enter the second matrix:"<<endl;
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
{
cout<<"Enter"<<(i+1)<<(j+1)<<"elemet:";
cin>>b[i][j];
}
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
s[i][j]=a[i][j]+b[i][j];
cout<<"The addition of matrix is :"<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
cout<<s[i][j]<<'\t';
cout<<endl;
}
getch();
}
===============================================================
Exercises
/*Write program which reads two matrix and then print a matrix which is multiplication of the two
matrix */

You might also like