EcE b RoCkS
Monday, 10 March 2014
5.Armstrong number
/* Source Code to display Armstrong number between two intervals entered by user. */
#include <stdio.h>
main()
{
int n1, n2, i, temp, num, rem;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &n1, &n2);
printf("Armstrong numbers between %d an %d are: ", n1, n2);
for(i=n1+1; i<n2; ++i)
{
temp=i;
num=0;
while(temp!=0)
{
rem=(temp%10);
num+=rem*rem*rem;
temp/=10;
}
if(i==num)
{
printf("%d ",i);
}
}
}
Output
Enter two numbers(intervals): 100 400 Armstrong numbers between 100 and 400 are: 153 370 371
4.Prime numbers.
/* C program to display all prime numbers between Two interval entered by user. */
#include <stdio.h>
main()
{
int n1, n2, i, j, flag;
printf("Enter two numbers(intevals): ");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for(i=n1+1; i<n2; ++i)
{
flag=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d ",i);
}
}
Output
Enter two numbers(intervals): 20 50 Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47
3.fibonacci series:
/* Displaying Fibonacci series up to certain number entered by user. */ #include <stdio.h> main() { int t1=0, t2=1, display=0, num; printf("Enter an integer: "); scanf("%d",&num); printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */ display=t1+t2; for(display=t1+t2;display<num; display=t1+t2;){ printf("%d+",display); t1=t2; t2=display; } }
Output
Enter an integer: 200 Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+55+89+144+
1.Factorial of a number:
/* C program to display factorial of an integer if user enters
/* C program to display factorial of an integer if user enters
non-negative integer. */
#include <stdio.h>
main()
{
int n, count;
float factorial=1;
printf("Enter an integer: ");
scanf("%d",&n);
if ( n< 0)
printf("Error!!! Factorial of negative number doesn't exist.");
else
{
for(count=1;count<=n;count++) /* for loop terminates if count>n */
{
factorial*=count; /* factorial=factorial*count */
}
printf("Factorial = %f",factorial);
}
}
Output 1
Enter an integer: -5 Error!!! Factorial of negative number doesn't exist.
Output 2
Enter an integer: 10 Factorial = 3628800
2.Fibonacci series
/* Displaying Fibonacci sequence up to nth term where n is entered by user. */#include <stdio.h> int main() { int count, n, t1=0, t2=1, display=0; printf("Enter number of terms: "); scanf("%d",&n); printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */ count=2; /* count=2 because first two terms are already displayed. */while (count<n) { display=t1+t2; t1=t2; t2=display; ++count; printf("%d+",display); } return 0; }
Output
Enter number of terms: 10 Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+
Subscribe to:
Posts (Atom)