Write a C++ program where you should take the first element of an array named \"
ID: 3825351 • Letter: W
Question
Write a C++ program where you should take the first element of an array named "fantastic_numbers" and implement a function called check() which takes the array fantastic_numbers. The method check() checks whether all elements can be divided or not. If yes, print return TRUE from the function or return FALSE. Write a program in C++ to check whether a user given integer number is Armstrong number or not. Re-write the Armstrong program using function and class. Define a class called MyClass which has a method investigate_number() which does the Armstrong check. Write a C++ program to calculate roots of a quadratic equation. Use functions
Explanation / Answer
Program to check whether number is divisible:
#include<iostream.h>
#include<conio.h>
bool check(int nos[])
{
int flag=0;
for(int i=0; i<3; i++)
{
if(nos[i]%2!=0)
flag=1;
}
if(flag==1)
return true;
else
return false;
}
void main()
{
int fantastic_number[3];
clrscr();
cout<<"enter 3 fantastic numbers:";
for(int i=0; i<3; i++)
cin>>fantastic_number[i];
bool k =check(fantastic_number);
if(k==true)
cout<<"Number is not divisible" <<k;
else
cout<<"number is divisible";
}
Program to check number is armstrong or not:
#include<iostream.h>
#include<conio.h>
void main()
{
int arm=0,a,b,c,d,no;
clrscr();
cout<<"Enter any num: ";
cin>>no;
d=no;
while(no>0)
{
a=no%10;
no=no/10;
arm=arm+a*a*a;
}
if(arm==d)
{
cout<<"no is armstrong";
}
else
{
cout<<"not a armstrong";
}
}
Program to check number for artstrong using class and function:
#include<iostream.h>
#include<conio.h>
class MyClass
{
int num,sum,t,r;
public:
void investigate_number(int num)
{
t=num;
sum=0;
while( t>0 )
{
r=t%10;
sum=sum + r*r*r;
t=t/10;
}
if( num==sum )
cout<<"the given number is an amstrong number";
else
cout<<"the given number is not an amstrong number";
}
};
void main()
{
MyClass arms;
int num;
clrscr();
cout<<"Enter the number:";
cin>>num;
arms.investigate_number(num);
}
Write a C++ program to calculate roots of a quadratic equation using function:
#include<iostream>
#include<math.h>
void check(int a, int b, int c)
{
d=b*b-4*a*c;
if(d==0)
{
root1=(-b)/(2*a);
root2=root1;
cout<<"Given Roots are real & equal";
}
else if(d>0)
{
root1=-(b+sqrt(d))/(2*a);
root2=-(b-sqrt(d))/(2*a);
cout<<"Given Roots are real & distinct";
}
else
{
root1=(-b)/(2*a);
root2=sqrt(-d)/(2*a);
cout<<"Given Roots are imaginary";
}
cout<<" Value of Root 1= "<<root1;
cout<<" Value of Root 2= "<<root2;
}
void main()
{
float a,b,c,d,root1,root2;
cout<<"Enter value of a, b and c : ";
cin>>a>>b>>c;
check(a,b,c);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.