Armstrong number in logic explanation and
code dry run
#include
#include
int main()
{
int armstrong=0,num=0,result=0,check;
cout<<"enter number to find it is an armstrong number?";
cin>>num;
check=num;
for(int i=1;num!=0;i++)
{
armstrong=num%10;
num=num/10;
armstrong=armstrong*armstrong*armstrong;
result=result+armstrong;
}
if(result==check)
num=num/10;
armstrong=armstrong*armstrong*armstrong;
result=result+armstrong;
}
if(result==check)
{
cout<
}
else
cout<
else
{
cout<
}
return 0;
}
cout<
return 0;
C++ code to print right triangle shape
using for loops
#include
#includeint main() { for(int i=0;i<=5;i++)
{
for(int j=0;j<=i;j++)
{
cout<
}
cout<
}
return 0;
}
for(int j=0;j<=i;j++)
{
cout<
}
cout<
}
return 0;
}
Greatest Common Divisor (GCD) of two
numbers
#include
#includeint main()
{
int first_number;
cout<<"enter first number : ";cin>>first_number;
int second_number;
cout<<"enter second number: ";cin>>second_number;
int gcd;
for(int i=1;i<=first_number&&i<=second_number;i++)
int first_number;
cout<<"enter first number : ";cin>>first_number;
int second_number;
cout<<"enter second number: ";cin>>second_number;
int gcd;
for(int i=1;i<=first_number&&i<=second_number;i++)
{
if(first_number%i==0 && second_number%i == 0 )
if(first_number%i==0 && second_number%i == 0 )
{
gcd=i;
}
}
cout<<"greatest common divison (gcd):"<
return 0;
}
gcd=i;
}
}
cout<<"greatest common divison (gcd):"<
return 0;
}
Reverse a Number in C++ Program Code
#include
#include
int main()
{
int number, reverse = 0;
cout<<"input a number to reverse and press enter: ";
cin>> number;
int number, reverse = 0;
cout<<"input a number to reverse and press enter: ";
cin>> number;
for( ; number!= 0 ; )
{
reverse = reverse * 10;
reverse= reverse + number%10;
number = number/10;
}
cout<<"new reversed number is: "<
return 0;
}
{
reverse = reverse * 10;
reverse= reverse + number%10;
number = number/10;
}
cout<<"new reversed number is: "<
return 0;
}
Linear search in C++ Program Example Code
#include
#include
int main()
{
cout<<"enter the size of array: ";
int size;
cin>>size;
int array[size], key,i;
taking input in array
for(int j=0;j
cout<<"enter the size of array: ";
int size;
cin>>size;
int array[size], key,i;
taking input in array
for(int j=0;j
{
cout<<"enter "<
cin>>array[j];
}
your entered array is
for(int a=0;a
cout<<"enter "<
cin>>array[j];
}
your entered array is
for(int a=0;a
{
if(key==array[i])
if(key==array[i])
{
cout<<"key found at index number : "<
break;
}
}
if(i != size)
cout<<"key found at index number : "<
break;
}
}
if(i != size)
{
cout<<"key found at index : "<
}
else
cout<<"key found at index : "<
}
else
No comments:
Post a Comment