Function Overloading in C++

Function Overloading is an example of polymorphism feature. It is a feature in C++ where two or more functions can have the same name but different parameters. void test1(){ } int test1(int a) { } float test1(int a,float b){ } All three functions are overloaded functions because argument(s) passed to these functions are different. Overloaded…

Virtual Functions Question

Create three classes Person, Professor and Student. The class Person should have data members name and age. The classes Professor and Student should inherit from the class Person. The class Professor should have two integer members: publications and cur_id. There will be two member functions: getdata and putdata. The function getdata should get the input…

C Language

#include<stdio.h> /* Program to print Hello World */ int main() { printf(“Hello World”); return 0; } A C program basically consist of following parts:: Pre-processor Commands Comment Functions variables Statements and expressions Pre-processor Commands : First line of the program #include is a pre-processor commands.  Preprocessor directives are lines included in the code of programs preceded…

7. Floyd’s Triangle C Language

1 2 3 4 5 6 7 8 9 10 int main() { int n, i, c, a =1; printf(“Enter the number of rows :: \n”); scanf(“%d”,&n); for(i =1; i <= n; i++) { for(c =1; c <= i; c++) { printf("%d ",a); a++; } printf("\n"); } return0; }

7. Floyd’s Triangle C++

#include<iostream> using namespace std; int main() { int n, i, c, a =1; cout<<“Enter the number of rows :: “; cin>>n; for(i =1; i<= n; i++) { for(c =1; c<= i; c++) { cout<<a<<” “; a++; } cout<<endl; } return 0; }

Pyramid and Pattern in C Language

Pyramid and Pattern 1) * ** *** **** ***** #include<stdio.h> int main(){ int i, j,rows; printf(“Enter number of rows: “); scanf(“%d”,&rows); for(i=1;i<=4;i++) { for(j=1;j<=i;j++) { printf(“*”); } printf(“\n”); } return 0; } 2) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 #include<stdio.h> int main(){ int i, j,rows; printf(“Enter…

Pyramid and Pattern in C++

1) * ** *** **** ***** #include<iostream> using namespace std; int main(){ int i, j,rows; cout<<“Enter number of rows: “; cin>>rows; for(i=1;i<=rows;i++) { for(j=1;j<=i;j++) { cout<<“*”; } cout<<“\n”; } return 0; } 2) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 #include<iostream> using namespace std; int main(){ int…

9. Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.

#include<iostream> using namespace std; int main() { int num, cpos=0, cneg=0, czero=0; char opt; do{ cout<<“Enter any num”; cin>>num; if(num==0) czero++; else if(num<0) cneg++; else cpos++; cout<<“Do you want to continue Y/N “; cin>>opt; }while(opt==’Y’|| opt==’y’); cout<<“Positive = “<<cpos<<endl; cout<<“Negative = “<<cneg<<endl; cout<<“Zero = “<<czero<<endl; } or #include<iostream> using namespace std; int main() { int…

8. Write a program to calculate HCF of Two given number.

#include<iostream> using namespace std; int main() { int i, num1, num2, min, hcf=1; cout<<“Enter any two numbers to find HCF: “; cin>>num1>>num2; // Find minimum between two numbers min = (num1<num2) ? num1 : num2; for(i=1; i<=min; i++) { if(num1%i==0 && num2%i==0) { hcf = i; } } cout<<“HCF = “<<hcf; return 0; }

7. Write a program to check given number is prime or not.

#include<iostream> using namespace std; int main() { int num,i=2; cout<<“Enter any number :: ” ; cin>>num; while(i<num) { if(num%i== 0) { cout<<num<<” is not a prime number. “; break; } i++; } if(i==num) cout<<num<<” is a prime number.”; } OR #include<iostream> using namespace std; int main() { int num,i=2,flag=1; cout<<“Enter any number :: ” ;…

5. Write a program to reverse any given integer number.

#include<iostream> using namespace std; int main() { int num, reverse = 0, remainder; cout<<“Enter any number :: ” ; cin>>num; while(num>0) { remainder = num%10; reverse = reverse*10 + remainder; num /=10; } cout<<“Ans = ” <<reverse; }