Find armstrong number
#include < stdio.h >
int main()
{
int number, sum = 0, temp, remainder;
printf("Enter an integer\n");
scanf("%d",&number);
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
printf("Entered number is an armstrong number.\n");
else
printf("Entered number is not an armstrong number.\n");
return 0;
}
Generate armstrong number
#include < stdio.h >
int main()
{
int r;
long number = 0, c, sum = 0, temp;
printf("Enter an integer upto which you want to find armstrong numbers\n");
scanf("%ld",&number);
printf("Following armstrong numbers are found from 1 to %ld\n",number);
for( c = 1 ; c < = number ; c++ )
{
temp = c;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
if ( c == sum )
printf("%ld\n", c);
sum = 0;
}
return 0;
}
Temperature conversion
#include < stdio.h >
void main()
{
int from , to;
float value;
printf("Temperature Conversion\n");
printf("Enter no of Unit to covert from : \n 1.celcius\n 2. Farenheit\n 3. Kelvin");
scanf("%d",&from);
printf("Enter no of Unit to covert to : \n 1.celcius\n 2. Farenheit\n 3. Kelvin");
scanf("%d",&to);
printf("Enter The value to convert: ");
scanf("%f",&value);
switch(from) {
case 1:
value= value + 273.15;
break;
case 2:
value= (value+459.67)*5/9;
break;
case 3: break;
default: break;
}
switch(to)
{
case 1:
value= value-273.15; break;
case 2:
value= value*9/5-459.67; break;
case 3: break;
default: break;
}
printf("Converted Value : %f", value);
}
Factorial using function
#include < stdio.h >
long factorial(int);
int main()
{
int number;
long fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &number);
printf("%d! = %ld\n", number, factorial(number));
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return result;
Factorial using recursion
#include < stdio.h >
long factorial(int);
int main()
{
int n;
long f;
printf("Enter an integer to find factorial\n");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
/*recursive call to factorial function*/
}
Fibonacci series using loop
#include < stdio.h >
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c < = 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
return 0;
}
Fibonacci series using recursion
#include < stdio.h >
int Fibonacci(int);
main()
{
int n, i = 0, c;
printf("Enter the number of terms ");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n", n);
for ( c = 1 ; c < = n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Sine Series
#include < stdio.h >
#include < conio.h >
#include < math.h >
void main()
{
int i, n ;
float x, val, sum, t ;
printf("Enter the value for x : ") ;
scanf("%f", &x) ;
printf("\nEnter the value for n : ") ;
scanf("%d", &n) ;
val = x ;
x = x * 3.14159 / 180 ;
t = x ;
sum = x ;
for(i = 1 ; i < n + 1 ; i++)
{
t = (t * pow((double) (-1), (double) (2 * i - 1)) * x * x) / (2 * i * (2 * i + 1)) ;
sum = sum + t ;
}
printf("\nSine value of %f is : %8.4f", val, sum) ;
}
Cosine Series
#include < stdio.h >
#include < conio.h >
#include < math.h >
void main()
{
int i, n ;
float x, val, sum = 1, t = 1 ;
printf("Enter the value for x : ") ;
scanf("%f", &x) ;
printf("\nEnter the value for n : ") ;
scanf("%d", &n) ;
val = x ;
x = x * 3.14159 / 180 ;
for(i = 1 ; i < n + 1 ; i++)
{
t = t * pow((double) (-1), (double) (2 * i - 1)) * x * x / (2 * i * (2 * i - 1)) ;
sum = sum + t ;
}
printf("\nCosine value of %f is : %8.4f", val, sum) ;
}
These are the Concept of C programming codes We'll be glad if you share your valuable information with us regarding this topic, then it will be a golden opportunity for all of us to improve ourselves and know more. Comment below!!
Comments
Post a Comment