Saturday, 2 April 2016

Q.Write a program to display the sum of the sequence given below.

1 + 1/2 + 1/4 + 1/8 + 1/16 + ... + 1/1024


#include<stdio.h>
#include<conio.h>
void main()
   int  x,n; 
   float s=1,i=0.5;
   printf("Enter the number");
   scanf("%d",&n);
   for  (x=1; x<=n ; x=x+1) 
   { 
  
      s=s+i;
      i=i*0.5; 
   }
   printf("Sum:%.3f",s);
   getch();
}

Q.Create a four-function calculator for fractions. (See Exercise 9 in Chapter 2, and Exercise 4 in this chapter.) Here are the formulas for the four arithmetic operations applied to fractions:
                                                              i.      Addition: a/b + c/d = (a*d + b*c) / (b*d)
                                                            ii.      Subtraction: a/b - c/d = (a*d - b*c) / (b*d)
                                                          iii.      Multiplication: a/b * c/d = (a*c) / (b*d)
                                                          iv.      Division: a/b / c/d = (a*d) / (b*c)

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

float n1,d1,n2,d2,R,x=1;

char ch;
void main()
{

printf("****MENU****\n");
printf("+ for Addition\n");
printf("- for Subtraction\n");
printf("* for Multi[lication\n");
printf("/ for Division\n");
while(x!=0)
{

printf("\nFor first Fraction\n");
printf("Enter First number:");
scanf("%f",&n1);
printf("Enter second Number:");
scanf("%f",&d1);
printf("\nEnter the operator:");
ch=getche();
printf("\n\nFor Second Fraction\n");
printf("Enter First number:");
scanf("%f",&n2);
printf("Enter second Number:");
scanf("%f",&d2);
switch(ch)
{
  case'+':
{
 
printf("\n%.1f",(n1/d1)+(n2/d2));
break;
}
  case'-':
{
 printf("\n%.1f",(n1/d1)-(n2/d2));
break;
}
  case'*':
printf("\n%.1f",(n1/d1)*(n2/d2));
break;
}
  case'/':
{
  printf("\n%.1f",(n1/d1)/(n2/d2));
break;
}
  default:
 printf("\nINVALID OPERATOR");
}
printf("\nEnter any number>0 to continue and '0' to EXIT:");
scanf("%d",&x);
}
  getch();
}
Q.Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6, output the individual digits of 8030 as 8 0 3 0, output the individual digits of 2345526 as 2 3 4 5 5 2 6, output the individual digits of 4000 as 4 0 0 0, and output the individual digits of -2345 as 2 3 4 5.

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

int x,rev,s,rem,t;
void main()
{
    printf("Enter the number: ");
    scanf("%d",&x);
    s=0;
    t=x;
    rev=0;
   
    while(t!=0)
    {
        rem=t%10;
        s=s+rem;
        rev=rev*10+rem;
        t/=10;
    }
    while(rev!=0)
    {
       rem=rev%10;
       printf("%d ",rem);
       
       rev/=10;
    }
    printf("\nSum of digits= %d",s);
    getch();
    

}
Q.Write an algorithm that reads numbers repeatedly until 0 (zero) is entered, find and display the smallest and largest number. Use while loop statement.

#include<stdio.h>
#include<conio.h>
int x,lnum,snum;
void main()
{
printf("Enter the number");
    scanf("%d",&x);
    lnum=x;
    snum=x;
    while(x!=0)
    {
        if(x>lnum)
            lnum=x;
        else if(x<snum)
            snum=x;
    printf("Enter the number");
    scanf("%d",&x);

    }
printf("\nLarger %d",lnum);
    printf("\nSmaller %d",snum);
    getch();

}
Q.Write an algorithm that reads two numbers find and display their GCD (Greatest Common Divisor).

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


int x1,x2,GCD,rem,t1,t2,t;
void main()
{
printf("Enter 1st Number:");
scanf("%d",&x1);
printf("Enter 2nd Number:");
scanf("%d",&x2);
t1=x1;
t2=x2;

while(x2!=0)
{
rem=x1%x2;
GCD=x2;
x1=x2;
x2=rem;
}
if(GCD<0)
GCD=GCD*-1;
printf("GCD of %d and %d =%d",t1,t2,GCD);
getch();
}
Q.A palindrome is a number or a text phrase that reads the same backwards as forwards.  For example, each of the following integers is a palindrome: 121, 55555, 4554 and 113311.  Write an algorithm that reads in a five-digit integer and determines whether it is a palindrome.  

#include<stdio.h>
#include<conio.h>
int num,n,rev;

void main()
{
printf("Enter a five-digit integer:");
scanf("%d",&num);
if(( num > 9999) && (num <= 99999))
{
n=num;
while(n==0)
{
n%=10;
rev=rev*10+n;
n/=10;
}
if(n==rev)
printf("\nThe is a Palindrome");
else
printf("\nThe is not a Palindrome");
}
else
printf("\nThe number is not a  five-digit");
getch();
}


Q. Find the two largest values among the 10 numbers. 
Note: You must input each number only once.

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


void main()
{
int n,fl,sl,x;
fl=0;
sl=0;
for (x=1 ; x<=10 ; x++)
{
printf("Enter a number:");
scanf("%d",&n);
if((n>fl)&&(n>sl))
{
sl=fl;
fl=n;
}
else if(n>sl)
sl=n;
}
printf("1st largest %d\t",fl);
printf("2nd Largest %d",sl);
getch();
}
Q.Write an algorithm that reads three nonzero double values and determines and prints whether they could represent the sides of a triangle.

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


void main()
{
double x,y,z;
printf("Enter the value of x:");
scanf("%lf",&x);
printf("Enter the value of y:");
scanf("%lf",&y);
printf("Enter the value of z:");
scanf("%lf",&z);
if((x+y>z)&&(y+z>x)&&(x+z>y))
printf("Represents the sides of a Triangle");
else
printf("Don't Represents the sides of a Triangle");
getch();



Q. Write an algorithm that uses a for loop statement to sum a sequence of integers.  Also find and display the smallest and largest number. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value per input statement.  A typical input sequence might be           5          100     200     300     400            500

Note:-  The 5 indicates that the subsequent 5 values

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

void main()
int v,L,S,sum=0,j,n;
{
printf("Enter any number:");
scanf("%d",&v);
printf("Enter the number");
scanf("%d",&n);
L=n;
S=n;
for(j=2; j<=v; j=j+1)
{


scanf("%d",&n);
if(n>L)
L=n;
else if(n<S)
S=n;
sum=sum+n;

}
printf("Sum=%d",sum);
printf("\tLarger=%d\t",L);
printf("Samller=%d",S);
getch();
}

 Q. Write an algorithm to calculate factorial of a +ve number within range 1-10.

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

int n,x,f=1;
void main()
{
printf("Enter the number:");
scanf("%d",&n);
if((n>0)&&(n<=10)) 
{
for(x=1 ; x<=n; x++)
f=f*x; 
printf("Factorial=%d",f);
}
else 
printf("Number is not in a given Range");
getch();
}