Sunday 30 June 2013

Write a program using user defined function to print all the Armstrong numbers between 1 and 500.

01.
#include<stdio.h>
main()
{
int N, temp, N1, N2, N3;
printf("All Armstrong numbers between 1 and 500:\n\n");
N = 001;
while (N <= 500)
{
N1 = N - ((N / 10) * 10);
N2 = (N / 10) - ((N / 100) * 10);
N3 = (N / 100) - ((N / 1000) * 10);
temp = (N1*N1*N1) + (N2*N2*N2) + (N3*N3*N3);
if (temp == N)
{
printf("\nAmstrong Number:%d", temp);
}
N++;
}
getch ();
}


02.


#include<stdio.h>
int main(){
    int num,r,sum,temp;

    for(num=1;num<=500;num++){
         temp=num;
         sum = 0;

         while(temp!=0){
             r=temp%10;
             temp=temp/10;
             sum=sum+(r*r*r);
         }
         if(sum==num)
             printf("%d ",num);
    }

    return 0;
}

Write a user defined function to compute xy.x and y should be integer but the function should return a double value (since y may be negative

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int x,y;
    double power();
    printf("Enter value of x : ");
    scanf("%d",&x);
    printf("Enter value of y : ");
    scanf("%d",&y);
    printf("%d to power %d is = %f\n",x,y,power(x,y));
    getch();
}
double power(x,y)
int x,y;
{
    double p;
    p=1.0;
    if(y>=0)
        while(y--)
            p*=x;
    else while(y++)
            p/=x;
    return(p);
}

Write a program using a recursive function to find the gcd (greatest common divisor) of two given numbers.

#include<stdio.h>
#include <conio.h>

int gcd ( int a, int b ) 
{
    int r;
    if ( ( r = a%b ) == 0 )
       return b;
    else
       return gcd ( b , r );
}
void main(void){
   int p,q;
   printf("\n Enter two numbers: ");
   scanf("%d %d",&p,&q);
   printf("\n The gcd is  %d ",gcd(p,q));
   getch( );
}

Write a program using a recursive function & LOOP to find the nth Fibonacci number.

01.

#include<stdio.h>
#include<conio.h>
int fibo(int n)
{
if(n==1||n==0)
return n;
else
return(fibo(n-1)+fibo(n-2));
}
int main()
{
int n,i;
puts("enter any number:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%d\t",fibo(i));
}
}



02.

#include<stdio.h>
#include<conio.h>
int Fib(int x, int range);

int main(){
int r;
printf("Enter the MAx range for Fibonacci Series:\n");
scanf("%d", &r);
Fib(r,r);
printf("\n");
}
Fib(int x, int range )
{
static int sum=0,pre=0,p=1;
if (sum>=range)
return 0;
else
{
if (pre==0 && p==1 && sum==0)
{sum=pre+p;
printf("%d %d %d", pre,p,sum);
}
else
{ pre=p;
p=sum;
sum=pre+p;
printf(" %d ", sum);
}
}
Fib(sum,range);
}






Using LOOP

#include<stdio.h>
#include<conio.h>

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);
   }

   getch();
   return 0;
   }

Friday 28 June 2013

Write a C program to find the value of nCr n and r should be taken from the keyboard.




#include<stdio.h>
int main(){
  int n,r,ncr;
  printf("Enter any two numbers->");
  scanf("%d %d",&n,&r);
  ncr=fact(n)/(fact(r)*fact(n-r));
  printf("The NCR factor of %d and %d is %d",n,r,ncr);
  return 0;
}
 int fact(int n){
  int i=1;
  while(n!=0){
      i=i*n;
      n--;
  }
  return i;
 }



Algorithm:

In the mathematics nCr has defined as
nCr = n! /((n-r)!r!)

Write a C program to find all the factors of a given integer.

#include<stdio.h>
#include<conio.h>
void main( )
{
int no,x;
printf("Enter the required number:");
scanf("%d",&no);
printf("\nThe factors are:");
for(x=1; x<=no; x++)
{
if(no%x==0)
printf("\n%d",x);
}
getch( );
}

Write a C program to find the value of nPr, n and r are taken from the keyboard




#include <stdio.h>
main()
{
    int n,r;
    float f;
    printf("Enter the values of N and R:\n");
    scanf("%d%d",&n,&r);
    f=fact(n)/fact(n-r);
    printf("nPr values is : %5.2f",f);
    getch();
}
fact(int n)
{
    int i,f=1;
    for (i=1; i<=n; i++)
        f=f*i;
    return(f);
}

Write a C program to print the following number pyramid:
                                                  1
                                                232
                                              34543
                                            4567654
                                          567898765

Answer:

#include<stdio.h>

main()
{
    int n, c, d, num = 1, space;
    printf("Enter any number : ");
    scanf("%d",&n);
    space = n-1;
    for ( d=1 ; d <= n ; d++ )
    {
        num = d;
        for ( c = 1 ; c <= space ; c++ )
            printf(" ");
        space--;
        for ( c = 1 ; c <= d ; c++ )
        {
            printf("%d", num);
            num++;
        }
        num--;
        num--;
        for ( c = 1 ; c < d ; c++)
        {
            printf("%d", num);
            num--;
        }
        printf("\n");
    }

    return 0;
}

Saturday 22 June 2013

An Electric power Distribution Company charges its domestic consumers as follows: Consumption units.

## An Electric power Distribution Company charges its domestic consumers as follows:
Consumption units                                                   Rate of charge
       0-200                                                                 2.5 taka per unit
      201-400                                                              100 taka plus 3 taka per unit excess of 200
     401-600                                                               250 taka plus 4 taka per unit excess of 400
     601 and above                                                     400 taka plus 5 taka per unit excess of 600
Write a C program which reads the customers amount of power consumed and prints the
amount to be paid by the customer.



Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n, p;
float amount;
printf("Enter the customer number: ");
scanf("%d",&n);
printf("Enter the power consumed: ");
scanf("%d",&p);
if(p>=0 && p<=200)
    amount=p*2.5;
else if(p>=201 && p<=400)
    amount = 100+((p-200) * 3);
else if(p>=401 && p<=600)
    amount = 250 + ((p-400) * 4);
else if(p>=601)
    amount = 400+((p-600) * 5);
printf("Amount to be paid by customer no. %d is Tk.:%5.2f.",n,amount);
getch();

return 0;

}

Write a C program which recognizes a letter whether it is a vowel or consonant. Use switch statement in your program.



Code:

#include<stdio.h>
void main()
{
 char ch;
 printf("Enter a single character:\n");
 scanf("%c",&ch);
 if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u')||(ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U'))
 {
  printf("The given Character '%c' is a Vowel\n ",ch);
 }
 else
 {
  printf("The given Character '%c' is a Consonant\n ",ch);
 }
getch();
}

Write a program in C which checks a number if it is a perfect square or not

 


Code:






#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
printf("\nEnter the number:");
scanf("%d",&a);
b=sqrt(a);
c=b*b;
if(c==a)
printf("\n%d is a perfect square.",a);
else
printf("\n%d is not a perfect square.",a);
getch();
}

Write a program in C which finds the largest number between any three numbers.


Code:

#include<stdio.h>
#include<conio.h>
int main()
{
float a,b,c;
     printf("Enter values:\n");
     scanf("%f %f %f",&a,&b,&c);
if(a>b)
{
if(a>c)
     printf("\nThe largest value is%f",a);
else
     printf("\nThelargest value is %f",c);
}
else
{
if(c>b)
    printf("\n The largest value is %f",c);
else
    printf("\n The largest value is%f",b);
}
getch();
}
 


Friday 21 June 2013

Write a program that asks user an arithmetic operator('+','-','*' or '/') and two operands and perform the corresponding calculation on the operands.






Code:

# include <stdio.h>
   int main()
{
 char operator;
     float num1,num2;
     printf("Enter operator +, - , * or / :\n");
     operator=getche();
     printf("\nEnter two operands:\n");
     scanf("%f%f",&num1,&num2);
     switch(operator)
{
case '+':
printf("num1+num2=%.2f",num1+num2);break;
case '-':
printf("num1-num2=%.2f",num1-num2);break;
case '*':
printf("num1*num2=%.2f",num1*num2);break;
case '/':
printf("num2/num1=%.2f",num1/num2);break;
default: printf("Error! operator is not correct");break;
}
return 0;
}