Showing posts with label ignou solved assignment. Show all posts
Showing posts with label ignou solved assignment. Show all posts

10.02.2011

Lucas number Program

Lucas numbers: It is similar to Fibonacci numbers, each Lucas number is defined to be the sum of its two immediate previous terms Where as the first two number are 2 and 1.
The Lucas numbers are as following :
2 1 3 4 7 11 18 29 47 76

/*c program to accept number from user and print less than Lucas numbers*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z,num;
 printf("Enter the limit of Lucas number : ");
 scanf("%d",&num);
 x=2;
 y=1;
 while(num>=x)
 {
   printf(" %d",x);
   z=x+y;
   x=y;
   y=z;
 }
 getch();
 return 0;
}


/***************Output*************** 

Enter the limit of Lucas number : 125
 2 1 3 7 11 18 29 47 76 123


************************************/

Related Programs:

  1. Amicable number C program
  2. Perfect number C program
  3. Armstrong number C program
  4. Print Armstrong number range C program
  5. Fibonacci series C program
  6. Generate Fibonacci series using recursion
  7. Search number is Fibonacci or not

9.30.2011

More if-else

Notice the following if statement :
if(expression)
  statement;
Here the expression can any valid expression including a relational expression. We can even use arithmetic expression in the if statement. For example all the following if statement are valid :
if(10 + 5 % 7)
  statement;
if(n=20)
  statement;
if(-15)
  statement;
if(-2.0)
  statement;

In C every expression is evaluated in terms of zero and non-zero values.
Note: that in C a non-zero value is considered to be true, whereas a 0 is considered to be false.
The default scope of the if statement is only the next statement. So, to execute more than one statement they must be written in a pair of braces

9.29.2011

if-else program

Q. Write a C program to read quantity and price for each item. Calculate the total expenses, and if the quantity is more than 1000 than giving discount is 20%.

Ans.
/*Calculate the total expenses*/

#include<stdio.h>
#include<conio.h>
void main()
{
 int quant,dis=0;
 float tot,rate;
 clrscr();
 printf("Enter quantity : ");
 scanf("%d",&quant);
 pirntf("\nEnter rate : ");
 scanf("%f",&rate);
 if(quant>1000)
  dis=20;
 tot=(quant*rate)-(quent*rate*dis/100);
 pirntf("\nTotal expenses = %f",tot);
 getch();
}

Output :
Enter quantity :750
Enter rate : 35.54
Total expenses = 25905.000000

Enter quantity : 1450
Enter rate : 9.50
Total expenses = 12397.500000

Intro if-else

In C language there are three major decision making instruction as if statement, the if-else statement, and the switch statement. And one also minor decision making instruction is conditional operator.

The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed, and If the condition is not true, then statement is not executed.

Let's demonstrates the use of if-else with c program:

/*demonstrates of if-else statement*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int n=10;
 if(n>=15)
  printf("n is greater!!");
 else
  printf("n is less than!!");
 getch();
}
Output :
n is less than!!

On execution of this program, when compiler check condition 10>=15, the condition is false so its execute else part statement i.e. n is less than!!.

Now try to write below program for more familiar with if-else:
Q. Write a C program to read quantity and price for each item. Calculate the total expenses, and if the quantity is more than 1000 than giving discount is 20%.

Ans: To see solved program click here