Wednesday, 25 October 2017

Data Structure in C


Data structures are used to store data in a computer in an organized form. In C language Different types of data structures are; Array, Stack, Queue, Linked List, Tree.
  • Array: Array is a collection of similar data type, you can insert and deleted element form array without following any order.
  • Stack: Stack work on the basis of Last-In-First-Out (LIFO). Last entered element removed first.
  • Queue: Queue work on the basis of First-In-First-Out (FIFO). First entered element removed first.
  • Linked List:  A Linked list is the collection of the node, Here you can insert and delete data in any order.
  • Tree: Stores data in a non-linear form with one root node and sub-nodes.

Algorithm

An algorithm is a finite set instruction, which is written for solve any problem. An algorithm is not the complete code or program, it is just like the English language.


Continue Reading...

Sunday, 29 November 2015

To design simple Calculator in C


Aim:
To write a simple program to develop a simple calculator that accepts two floating point numbers from the keyboard. Display a menu to the user and get the user’s choice. Perform the operation and display the result using switch statement.

Algorithm:
  1. Read the two floating point numbers A, B.
  2. Display the menu.
1 - Add
    2 - Subtract
3 - Multiply
4 - Divide
  1. Get the user’s choice.
  2. If choice =1 then Result=A+B. Goto step 8.
  3. If choice =2 then Result=A-B. Goto step 8.
  4. If choice =3 then Result=A*B. Goto step 8.
  5. If choice =4 then Result=A/B. Goto step 8.
  6. Display the result.
  7. Stop.

Program:
/* To design simple calculator */
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,res;
int ch;
clrscr();
printf("Enter the value of A:");
scanf("%f",&a);
printf("Enter the value of B(B<>0):");
scanf("%f",&b);
printf("\n\nMathematical Operations");
printf("\n************************");
printf("\n\t1->Add");
printf("\n\t2->Subtract");
printf("\n\t3->Multiply");
printf("\n\t4->Divide");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:res=a+b;
break;
case 2:res=a-b;
break;
case 3:res=a*b;
break;
case 4:res=a/b;
break;
}
printf("Answer =%.2f",res);
getch();
}
Output 1:
Enter the value of A:5
Enter the value of B(B<>0):7
Mathematical Operations
************************
       1->Add
       2->Subtract
       3->Multiply
       4->Divide

Enter your choice:1
Answer =12.00

Output 2:
Enter the value of A:7
Enter the value of B(B<>0):56
Mathematical Operations
************************
       1->Add
       2->Subtract
       3->Multiply
       4->Divide
Enter your choice:2
Answer =-49.00
Output 3:
Enter the value of A:5.2
Enter the value of B(B<>0):3
Mathematical Operations
************************
       1->Add
       2->Subtract
       3->Multiply
       4->Divide
Enter your choice:3

Answer =15.60

Continue Reading...

To check for perfect Number in C


Aim:
To write a ‘C’ program to find whether the given number is perfect number or not.
A number is a perfect number if the sum of the factors of the number other than itself is equal to that number.
Example: 28=1+2+4+7+14 is a perfect number.
Algorithm:
  1. Read the number n.
  2. If the number is less than 0 goto step  7
  3. Initialize sum=0, i=1
  4. if n % i=0 then sum=sum+i
  5. Increment i. If i<n goto step 4
  6. if sum=n
    Print the number is a perfect number
    else
Print the number is not a perfect number
  1. Stop.
Program:
/* To find whether the given number is perfect number */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,i;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
if(n>0)
{
for(i=1;i<n;i++)
if(n%i==0) sum=sum+i;
if(sum==n)
printf("\n\t%d is a perfect number",n);
else
printf("\n\t%d is not a perfect number",n);
}
else
printf("\n\tThe input number should be greater than zero");
getch();
}
Output -1:
Enter a number:28
       28 is a perfect number
Output-2:
Enter a number:56
       56 is not a perfect number
Output-3:
Enter a number:-28

       The input number should be greater than zero

Continue Reading...

Thursday, 22 October 2015

To Display Student's Grade

Aim:
To write a ‘C’ program to read the mark of the student and display grade based on the following norms:
>=75 Grade A , >=50 and <75 Grade B , >=25 and <50 Grade C
<25 Grade F
Algorithm:
  1. Read the average mark of the student.
  2. If the mark is >=75 print “GRADE A”.
  3. If the mark is between 50 to 75 print “GRADE B”.
  4. If the mark is between 25 to 50 print “GRADE C”.
  5. If the mark is less than 25 print “GRADE F”.
  6. Stop.
Program:
/* To display the grade of the student */
#include<stdio.h>
#include<conio.h>
void main()
{
float avg;
clrscr();
printf("Enter the average mark of the student:");
scanf("%f",&avg);
if(avg>=75)
printf("\n\tThe student's grade - A");
else if(avg>=50)
printf("\n\tThe student's grade - B");
else if(avg>=25)
printf("\n\tThe student's grade - C");
else
printf("\n\tThe student's grade -F");
getch();
}
Output 1:
Enter the average mark of the student:78
       The student's grade - A
Output 2:
Enter the average mark of the student:50
       The student's grade – B
Output 3:
Enter the average mark of the student:30
       The student's grade - C
Output 4:
Enter the average mark of the student:24

       The student's grade -F
Continue Reading...

Blogroll

About

Designed By IAMNGP | Moon