#include<stdio.h>
#include<math.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,sum,i,t,a;
printf("\n\n\nThe Armstrong numbers in between 1 to 500 are : \n\n\n");
for(i = 1; i <= 500; i++)
{
t = i; // as we need to retain the original number
sum = 0;
while(t != 0)
{
a = t%10;
sum += a*a*a;
t = t/10;
}
if(sum == i)
printf("\n\t\t\t%d", i);
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Showing posts with label array. Show all posts
Showing posts with label array. Show all posts
Program to find Armstrong Number between 1 to 500
Checking for Odd and Even Numbers using Bitwise Operator
#include<stdio.h> int main() { printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); int x; for(x = 0; x <= 10; x++) { if(x&1) // if number is odd printf("\t\t\t%d is odd\n",x); else if(!(x&1)) // ! is used inside if to reverse the boolean value printf("\t\t\t%d is even\n",x); } printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n"); return 0; }
Arrays
Arrays can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
| Numbers(1) | Numbers(2) | Numbers(3) | Numbers(4) | … |
Arrays can be one- dimensional (like vectors), two-dimensional (like matrices) and Fortran allows you to create up to 7-dimensional arrays.
Declaring Arrays
Arrays are declared with the dimension attribute.
For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,
real, dimension(5) :: numbers
The individual elements of arrays are referenced by specifying their subscripts. The first element of an array has a subscript of one. The array numbers contains five real variables –numbers(1), numbers(2), numbers(3), numbers(4), and numbers(5).
To create a 5 x 5 two-dimensional array of integers named matrix, you write −
integer, dimension (5,5) :: matrix
You can also declare an array with some explicit lower bound, for example −
real, dimension(2:6) :: numbers
integer, dimension (-3:2,0:4) :: matrix
Assigning Values
You can either assign values to individual members, like,
numbers(1) = 2.0
or, you can use a loop,
do i =1,5
numbers(i) = i * 2.0
end do
One-dimensional array elements can be directly assigned values using a short hand symbol, called array constructor, like,
numbers = (/1.5, 3.2,4.5,0.9,7.2 /)
please note that there are no spaces allowed between the brackets ‘( ‘and the back slash ‘/’
Subscribe to:
Posts (Atom)
OLDER POST
Head Constable (Ministerial)
Head Constable (Ministerial) Apply Now Notification