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

Introduction To Programming

The document contains solutions to two programming questions. The first question asks the programmer to write a program to print all Armstrong numbers between 1 and 500. The solution provided includes code to check if the sum of the cubed digits of a number equals the number itself, indicating it is an Armstrong number. The second question asks to write a program to compute the sine of a given value x using its power series expansion up to the nth term. The provided solution includes code to calculate the factorial terms and alternating signs in the power series to compute sin(x).

Uploaded by

Cocka Tiels
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Introduction To Programming

The document contains solutions to two programming questions. The first question asks the programmer to write a program to print all Armstrong numbers between 1 and 500. The solution provided includes code to check if the sum of the cubed digits of a number equals the number itself, indicating it is an Armstrong number. The second question asks to write a program to compute the sine of a given value x using its power series expansion up to the nth term. The provided solution includes code to calculate the factorial terms and alternating signs in the power series to compute sin(x).

Uploaded by

Cocka Tiels
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

INTRODUCTION TO PROGRAMMING

TE51 A
Question 1.
Write a program to print out all Armstrong numbers between 1 and 500. If sum of
cubes of each
digit of the number is equal to the number itself, then the number is called an
Armstrong number.For example 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
SOLUTION
**************************************
** ** ************************* ** ** ***
* Programming Assignment :2 *
* Introduction to Programming
* Programmer: CAPT ADEEL
* Due Date: WED 3 DEC 2014. *
***** ** ** ** ******* ** ** ** ** ** ******
#include <iostream.h>
#include <conio.h>
void main()
{
int number, sum = 0, temp, remainder;
cout<<"Enter an integer\n";
cin>> number;
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
cout<<"Entered number is an armstrong number.\n";
else
cout<<"Entered number is not an armstrong number.\n";
getch();
}
Question 2.
Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the computation
should use all terms in the series up throughthe term involving x n
sin x = x - x3/3! + x5/5! - x7/7! + x9/9! ........
SOLUTION

**************************************
** ** ************************* ** ** ***
* Programming Assignment :2 *
* Introduction to Programming
* Programmer: CAPT ADEEL
* Due Date: WED 3 DEC 2014. *
***** ** ** ** ******* ** ** ** ** ** ******
#include <iostream.h>
#include <conio.h>
void main()
{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
while(i<=n)
{
fact=1;
while(j<=i)
{
p=p*x;
fact=fact*j;
j++;
}
sign=-1*sign;
sum+=sign*p/fact;
i+=2;
}
cout<<"sin "<<x<<"="<<sum;
getch();
}

You might also like