c++ program to illustrate the friend function,here we are finding mean of two numbers.

//wap in c++ to illustrate the friend function,here we are finding mean of two numbers. #include<iostream>using namespace std;class sample{ int num1,num2; public : void setval(); /*here we are passing sample as argument as we are going to pass objects as argument this is the syntax of Passing Objects as Function Arguments*/ friend float mean(sample);};void sample::setval(){ … Read more

facts about c++

Constructor constructor is a special type of function or method in which there is No return type(Even void) and constructor name will be same as the name of the class. There are two types of constructor1.default constructor2.parameterised constructor Passing Objects as Function Arguments in C++class Test{public : /*here we are passing Test the name of … Read more

c++ to display the values we are passing from parameterised constructor.

//To display the values of parameterised constructor.#include<iostream>using namespace std; class add{ int num1,num2,sum; public : add(int x,int y); void display();};add::add(int x,int y) { num1=x; num2=y; sum=x+y; }void add::display() { cout<<“The nummbers are “<<num1<<“t”<<num2<<“n”; }int main()  {  add b=add(10,20);  b.display();  } OUT-The numbers are 10 20

c++ to calculate the sum of 2 interger no. by using constructor concept

//wap to calculate the sum of 2 interger no. by using constructor concept#include<iostream>using namespace std;class sum{ int a,b,s; public: sum(int ,int ); void display();};sum::sum(int x ,int y){ a=x; b=y; s=a+b;}void sum::display(){ cout<<“The sum is : “<<s<<“n”;} int main(){ sum ad=sum(10,20); ad.display(); return (0);} OUT- The sum is : 30 //Add two number using constructor without … Read more

c programming for bubble sort

#include<stdio.h> void selection_sort(int arr[],int num); int main() { int arr[100],num,i; printf(“Enter the size of arrayn”); scanf(“%d”,&num); printf(“nEnter the elements in array one by onen”); for(i=0;i<num;i++) { scanf(“%d”,&arr[i]); } selection_sort(arr,num); printf(“The sorted array isn”); for(i=0;i<num;i++) { printf(“%d”,arr[i]); } } void selection_sort(int arr[],int num) { int i,j,temp,min; for(i=0;i<=num-1;i++) { min=i; for(j=i+1;j<=num-1;j++) /*here in j=i+1 we are adding … Read more

c programming for bubble sort

#include<stdio.h>void bubble_sort(int [],int num);int main(){int arr[100],num,i;printf(“n Enter the size of the arrayn”);scanf(“%d”,&num);printf(“n Enter the elements in the array one by onen”);for(i=0;i<num;i++){scanf(“%d”,&arr[i]);}bubble_sort(arr,num);//outputfor(i=0;i<num;i++){ printf(“%d”,arr[i]);}} void bubble_sort(int arr[],int num){int i,j,temp;for(i=1;i<=num-1;i++)//i is for passes, if the size of the array is 4 we need to go for n-1 indices only{for(j=0;j<=num-i-1;j++)// here in j<=num-i-1 we are subtracting i as in … Read more

c program to find prime factors of a number

#include<stdio.h>int prime_number(int num);int main(){ int num; printf(“n Enter a number for prime factor”); scanf(“%d”,&num); prime_number(num);}int prime_number(int num){ int i=2; while(num!=1) { if(num%i==0) { while(num%i==0) { printf(“%dt”,i); num=num/i; } } i++;}} OUT:  Enter a number for prime factor420 2 2 3 5 7 To understand go here https://www.youtube.com/watch?v=Xg89vIL9aog

C program for prime or not

#include<stdio.h>int main(){ int num,flag=0; printf(“Enter a numbert”); scanf(“%d”,&num); //if the number is divisible by half of its number that is if n=6 then it is divided by 3 like n/3 and n is divided by 1,2,3 and if remainder is zero number is not prime for(int i=2;i<=num/2;i++) { if(num%i==0){ flag=1; break;}} if (flag==1) printf(“Not Primen”);elseprintf(“n … Read more

c program for Runga-kutta method of 4th order

#include<stdio.h>#include<math.h>float f(float x,float y){float f; f=((1/(x+y)));return (f);} int main() {float x0,y0,h,xn,k,k1,k2,k3,k4;printf(“n Enter the initial value that is x0 and y0n”);scanf(“%f%f”,&x0,&y0); printf(“n Enter the value of hn”);scanf(“%f”,&h); printf(“n Enter the value of x for which we need to find the value of yn”);scanf(“%f”,&xn);do{k1=h*f(x0,y0);k2=h*f(x0+(h/2),y0+(k1/2));k3=h*f(x0+(h/2),y0+(k2/2));k4=h*f(x0+h,y0+k3);k=(k1+2*k2+2*k3+k4)/6;y0=y0+k;x0=x0+h;printf(“n%f %f”,x0,y0);}while(x0<xn);printf(“n value of y=%f”,y0);} output:  Enter the initial value that is x0 and … Read more

Pin It on Pinterest

Open chat