Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts

Friday, 7 June 2013

C program to Check Prime Numbers | Checking Prime Numbers

Aim:
                To write a C program to check the prime number.

Algorithm
  1. Start the program
  2. Read the number ‘num’ from the user.
  3. For all i>num/2 divide the number by i.
  4. If the number is divisible by any other numbers then that number is not a prime number.
  5. If the number is not divisible by any other numbers then that number is a prime number.
  6. Display the result
  7. Stop the Program
 C Program – checking Prime Number :

#include<stdio.h>
#include<conio.h>
Void main()
{
int num,I;
clrscr();
printf(“\nEnter a number”);
scanf(“%d”,&num);
for(i=2;i<=num/2;i++)
{
If(num % i==0)
Break;
}
If (i>num/2)
Printf(“the given number %d is prime”,num);
else
printf(“The given number %d is not prime”,num);
getch();
}

Output - checking Prime Number

Enter a number 56
The given number 56 is not prime
Enter a number 3
The given number 3 is prime
 
 --------------------------------------------------------------------------------------------------------------
 
C program to Check Prime Numbers-Checking Prime Numbers-C program to find prime numbers -Write a program to find prime numbers- C programs- Write a c program to find the given number is prime or not

Sunday, 2 June 2013

C program to find the average of three numbers

Aim:

To write a c program to find the average of three numbers

Algorithm:

1.Start the program
2.Get three numbers from the users
3.Add three numbers and divide it by 3
4.Print the average value
5.Stop the program

C Program - Average of three numbers

#include<stdio.h>

#include<conio.h>
void main()
{
int n1,n2,n3;
float average;
clrscr();
printf("Enter the three numbers");
Scanf("%d %d %d",&n1,&n2,&n3);
average =(n1+n2+n3)/3.0;
printf("average of above three numbers is %.4f",average);
getch();
}

Output for  Average of three numbers C program

Enter three numbers
56
78
93
Average of above three numbers is 75.6667

Result
Thus the C program to find the average of three numbers is written and executed successfully.

C program to find sum of two numbers - Adding two numbers

Algorithm:

1.Start the program
2.Read two numbers from users
3.Add two numbers using addition operator
4.Print the sum value
5.Stop the program

C program - Sum of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3;
clrscr();
printf("Enter first number \n");
scanf("%d",&num1);
printf("Enter second number \n");
scanf("%d",&num2);
num3 = num1+num2;
printf("sum is %d \n",num3);
getch();
}

Output for  Sum of two numbers c program 

Enter first number
10
Enter second number
20
sum is
30

Result:

Thus the c program to add two numbers is written and executed successfully.