Monday, 1 September 2014
Generate first n Prime number
Generate first n Prime number
Q. Write a C program to generate first n prime number C program.
Ans.
/*c program for generate first n prime number*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,num,t,div,count;
printf("How many prime number you want to print: ");
scanf("%d", &n);
printf("\n%d\t",2); /*2 is first prime number*/
count=1;
num=3;
while(count<n)
{
t=sqrt(num);
div=2;
while(div<=t)
{
if(num%div==0)
break;
div++;
}
if(div>t)
{
printf("%d\t",num);
count++;
}
num=num+2;
}
getch();
return 0;
}
Ans.
/*c program for generate first n prime number*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,num,t,div,count;
printf("How many prime number you want to print: ");
scanf("%d", &n);
printf("\n%d\t",2); /*2 is first prime number*/
count=1;
num=3;
while(count<n)
{
t=sqrt(num);
div=2;
while(div<=t)
{
if(num%div==0)
break;
div++;
}
if(div>t)
{
printf("%d\t",num);
count++;
}
num=num+2;
}
getch();
return 0;
}
search prime number
search prime number
Q. Write a C program to find whether a number is prime or not.
Definition of prime number:A prime number is one,which is divisible only by one or itself. And its must greater than 1.
And if number is not prime, it is called composite number and vice versa.
Ans.
#include<stdio.h>
#include<stdio.h>
int main()
{
int x,num;
printf("Enter number : ");
scanf("%d",&num);
x=2;
while(x<=num-1)
{
if(num%x==0)
{
printf("Number is not prime!!");
break;
}
x++;
}
if(x==num)
printf("Number is prime!!");
getch();
getch();
return 0;
}
Flowchart for finding Armstrong number C program
Perfect number
Q. Write a C program to check whether a given number is a perfect number or not.
Ans.
Definition of Perfect number: A positive integer n is called a perfect number if it is equal to the sum of all of its positive divisors, excluding n itself.
For example, 6 is perfect integer number, because 1, 2 and 3 are its proper positive divisors and 1+2+3=6.
The next perfect number is 28 because 1+2+4+7+14=28.
The next perfect number is 496 because
1+2+4+8+16+31+62+124+248=496.
/*program to check whether a number is perfect or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,num,sum=0;
printf("Enter a number : ");
scanf("%d",&num);
n=num;
for(i=1; i<n; i++)
{
if(num%i==0)
sum=sum+i;
}
if(num==sum)
printf("Given number is Perfect Number ");
else
printf("Given number is Not Perfect Number ");
getch();
return 0;
getch();
return 0;
}
Output:-
Enter a number : 7543
Given number is Not Perfect Number
Enter a number : 8128
Given number is Perfect Number
Amicable numbers
Amicable numbers
Q. writes a C program to check whether given two numbers are amicable numbers or not.
Ans.
Definition of Amicable numbers: Amicable numbers are two numbers so related that the sum of the proper divisors of the one is equal to the other, unity being considered as a proper divisor but not the number itself.
For example, lets a pair or two number pair is (220,284),
For the proper divisor of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110, of which the sum is 284.
And the proper divisor of 284 are 1, 2, 4, 71, 142 of which the sum is 220.
/*program to check whether given numbers are amicable or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n1,n2,s1=0,s2=0,i;
printf("Enter first number : ");
scanf("%d",&n1);
printf("Enter second number : ");
scanf("%d",&n2);
for(i=1; i<n1 ; i++)
{
if(n1%i==0)
s1=s1+i;
}
for(i=1; i<n2 ; i++)
{
if(n2%i==0)
s2=s2+i;
}
if(n1==s2 && n2==s1)
printf("Given numbers are Amicable Numbers");
else
printf("Given numbers are Not Amicable Numbers");
getch();
getch();
return 0;
}
Output:-
Enter first number : 452
Enter second number : 264
Given numbers are Not Amicable Numbers
Enter first number : 220
Enter second number : 284
Given numbers are Amicable Numbers
print Armstrong number range
print Armstrong number range
Q. Write a C program to accept a number from user and print all the Armstrong number that are less then or equal to user number.
Ans.
/*C program to printing armstrong number between a specific range*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,d1,d2,d3,tmp,st;
printf("Enter ending number: ");
scanf("%d", &num);
for(st=1; st<=num; st++)
{
d1=st-((st/10)*10);
d2=(st/10)-((st/100)*10);
d3=(st/100)-((st/1000)*10);
tmp=(d1*d1*d1)+(d2*d2*d2)+(d3*d3*d3);
if(tmp==st)
printf("\nArmstrong number is: %d",tmp);
}
getch();
return 0;
}
/************Output************/
or
Q. write a C program to print Armstrong number from 1 to 1000.Ans.
/*C program to printing armstrong number between a specific range*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,d1,d2,d3,tmp,st;
printf("Enter ending number: ");
scanf("%d", &num);
for(st=1; st<=num; st++)
{
d1=st-((st/10)*10);
d2=(st/10)-((st/100)*10);
d3=(st/100)-((st/1000)*10);
tmp=(d1*d1*d1)+(d2*d2*d2)+(d3*d3*d3);
if(tmp==st)
printf("\nArmstrong number is: %d",tmp);
}
getch();
return 0;
}
/************Output************/
Sum of square program, Algorithm and Flowchart
Sum of square program, Algorithm and Flowchart
Q. Write a C program to accept a number from user and print the sum of square. Also write down the algorithm and draw flowchart.
Ans.
/*c program for accept number and calculate sum of square*/
#include<stdio.h>
int main()
{
int num,sum=0,i;
printf("Enter any number : ");
scanf("%d", &num);
for(i=1; i<=num; i++)
sum = sum + (i*i);
printf("Sum of square of %d = %d",num,sum);
return 0;
}
The output of above program would be:
Algorithm for accept a number from user and calculate sum of square:
[Sum of square procedure:accept number num from user, and set sum=0 and calculate sum of square.]
Step 1. Start
Step 2. Read number num
Step 3. [Initialize]
sum=0, i=1
Step 4. Repeat step 4 through 6 until i<=num
Step 5. sum=sum+(i*i)
Step 6. i=i+1
Step 7. print the sum of square
Step 8. stop
[end of loop step 4]
[end of sum of square procedure]
Flowchart for sum of square C program as following:
Ans.
/*c program for accept number and calculate sum of square*/
#include<stdio.h>
int main()
{
int num,sum=0,i;
printf("Enter any number : ");
scanf("%d", &num);
for(i=1; i<=num; i++)
sum = sum + (i*i);
printf("Sum of square of %d = %d",num,sum);
return 0;
}
The output of above program would be:
Figure: Screen shot for calculate sum of square of number C program |
Algorithm for accept a number from user and calculate sum of square:
[Sum of square procedure:accept number num from user, and set sum=0 and calculate sum of square.]
Step 1. Start
Step 2. Read number num
Step 3. [Initialize]
sum=0, i=1
Step 4. Repeat step 4 through 6 until i<=num
Step 5. sum=sum+(i*i)
Step 6. i=i+1
Step 7. print the sum of square
Step 8. stop
[end of loop step 4]
[end of sum of square procedure]
Flowchart for sum of square C program as following:
Figure: Flowchart for calculate sum of square C progra |
Algorithm for number pyramid
Algorithm for number pyramid
Q. write a C program for following number pyramid and also write down algorithm:
1234554321
1234__4321
123____321
12______21
1________1
Ans.
C program for above number pyramid structure:
click here
Algorithm for above number pyramid as:
[procedure of number pyramid]
step1: Start
step2: Read number num
step3: [initialize]
r=1
step4: Repeat step 4 to 21 until num>=1
step5: [initialize]
c=1
step6: Repeat step 6 to 8 until c<=num
step7: print value of c
step8: c=c+1
[end of step6 loop]
step9: [initialize]
sp=r
step10: Repeat step 10 to 12 until sp>1
step11: print _
step12: sp=sp-1
[end of step10 loop]
step13: [initialize]
sp=r
step14: Repeat step 14 to 16 until sp>1
step15: print _
step16: sp=sp-1
[end of step14 loop]
step17: [initialize]
c=1
step18: Repeat step 18 to 20 until c>=1
step19: print value of c
step20: c=c-1
[end of step18 loop]
step21: Go to next line
[end of step4 loop]
step22: Stop
[end procedure of number pyramid]
1234554321
1234__4321
123____321
12______21
1________1
Ans.
C program for above number pyramid structure:
click here
Algorithm for above number pyramid as:
[procedure of number pyramid]
step1: Start
step2: Read number num
step3: [initialize]
r=1
step4: Repeat step 4 to 21 until num>=1
step5: [initialize]
c=1
step6: Repeat step 6 to 8 until c<=num
step7: print value of c
step8: c=c+1
[end of step6 loop]
step9: [initialize]
sp=r
step10: Repeat step 10 to 12 until sp>1
step11: print _
step12: sp=sp-1
[end of step10 loop]
step13: [initialize]
sp=r
step14: Repeat step 14 to 16 until sp>1
step15: print _
step16: sp=sp-1
[end of step14 loop]
step17: [initialize]
c=1
step18: Repeat step 18 to 20 until c>=1
step19: print value of c
step20: c=c-1
[end of step18 loop]
step21: Go to next line
[end of step4 loop]
step22: Stop
[end procedure of number pyramid]
Algorithm for star pyramid
Algorithm for star pyramid
Q. Write a C program to print the following star program. Also write down the algorithm.
*
**
***
****
*****
Ans.
C program for above star pyramid at:
click here
Algorithm for above star pyramid as follows:
[star pyramid procedure]
step1: Start
step2: Read number num
step3: [initialize]
r=1
step4: Repeat step 4 through 10 until num>=r
step5: [initialize]
c=1
step6: Repeat step 6 through 8 until c<=r
step7: print */#
step8: c=c+1
[end of loop step6]
step9: go to next line
step10: r=r+1
[end of loop step4]
step11: stop
[end of star pyramid procedure]
*
**
***
****
*****
Ans.
C program for above star pyramid at:
click here
Algorithm for above star pyramid as follows:
[star pyramid procedure]
step1: Start
step2: Read number num
step3: [initialize]
r=1
step4: Repeat step 4 through 10 until num>=r
step5: [initialize]
c=1
step6: Repeat step 6 through 8 until c<=r
step7: print */#
step8: c=c+1
[end of loop step6]
step9: go to next line
step10: r=r+1
[end of loop step4]
step11: stop
[end of star pyramid procedure]
if else statement and flowchart
if else statement and flowchart
Decision Control Statements and Flowchart
The if Statement
It is used to execute an instruction or sequence/block of instruction only if a condition is fulfilled.
Difference forms of implements if-statement are:
- Simple if statement
- if-else statement
- Nested if-else statement
- else if statement
Figure: Simple if statement syntax and flowchart |
Figure: if-else statement syntax and flowchart |
Nested if-else statement
In nested if...else statement, an entire if...elseconstruct is written within either the body of the if statement or the body of an else statement.
The syntax is as follows:
if(condition_1)
{
if(condition_2)
{
block statement_1;
}
else
{
block statement_2;
}
}
else
{
block statement_3;
}
block statement_4;
Figure: nested if-else statement flowchart |
Else if statement
It is used in multi-way decision on several conditions. This works by cascading comparisons. As soon as one of the conditions is true, the statement or block of statements following them is executed and no further comparison are performed.
The else...if syntax is as follows:
if(condition_1)
block statement_1;
else if(condition_2)
block statement_2;
else if(condition_n)
block statement_n;
else
default statement;
Figure: flowchart for else...if statement |
Algorithm,Rules for constructing an Algorithm
Algorithm
An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm can be expressed in human readable language like as English. Algorithm is language depended, well structured and detailed.
Rules for constructing an Algorithm
When you are going to create an algorithms, keep following point in mind as:
- Input: There should be zero or more values which are to be supplied.
- Output: At least one result is to be produced.
- Definiteness: Each step must be clear and unambiguous.
- Finiteness: If we trace the steps of an algorithm, then for all cases, the algorithm must terminate after a finite number of steps.
- Effectiveness: Each step must be sufficiently basic that a person using only paper and pencil can in principle carry it out. In addition, not only each step is definite, it must also be feasible.
- Comment Session: Comment is additional info of program for easily modification. In algorithm comment would be appear between two square bracket []. For example: [ this is a comment of an algorithm ]
Figure: Algorithm of demonstration of how to write "for loop" in algorithm factorial program |
Let's understand to algorithm by example:
Q. Write algorithm to calculate the sum and average of two numbers.
Ans.
[procedure for calculate sum and average of two numbers]
Step1 : Start
Step2 : Read two numbers n,m
Step3 : Calculate sum=n+m
Step4 : Calculate avg=sum/2
Step5 : Print sum,avg
Step5 : Stop[End of procedure for calculate sum and average of two numbers]
Q. Write an algorithm to convert a decimal number into binary.
Ans.
[procedure for convert decimal to binary number]
Step1 : Start
Step2 : Read number num
Step3 : Set x=1
Step4 : B(x)= num MOD 2
Step5 : num=num/2
Step6 : If num is equal to 0 then goto step8
Step7 : x=x+1
Step8 : goto step3
Step9 : Print B(x)
Step10 : x=x-1
Step11 : If x is greater than 0 then goto step8
step12 : Stop
[end of procedure for convert decimal to binary number]
Flowchart for finding Armstrong number
Flowchart for finding Armstrong number
Q. Write down the program for finding Armstrong number and also draw flowchart.
Ans.
for Armstrong number C program source code click below link:
Finding Armstrong number C program
Flowchart for finding Armstrong number C program:
Ans.
for Armstrong number C program source code click below link:
Finding Armstrong number C program
Flowchart for finding Armstrong number C program:
Figure: Flowchart for finding number is Armstrong or not C program |
Factorial C program,Algorithm,Flowchart
Factorial C program,Algorithm,Flowchart
Q. Write a C program to find the factorial value of a number. Also write the algorithm and draw flowchart.
Ans.
/*c program to find out factorial value of a number*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,fact=1;
printf("Enter any number : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
fact = fact * i;
printf("Factorial value of %d = %d",n,fact);
return 0;
}
The output of above program would be:
Algorithm for calculate factorial value of a number:
[algorithm to calculate the factorial of a number]
step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
[process finish of calculate the factorial value of a number]
Flowchart for calculate factorial value of a number:
Ans.
/*c program to find out factorial value of a number*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,fact=1;
printf("Enter any number : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
fact = fact * i;
printf("Factorial value of %d = %d",n,fact);
return 0;
}
The output of above program would be:
Screen shot for calculate factorial value of a number C program |
Algorithm for calculate factorial value of a number:
[algorithm to calculate the factorial of a number]
step 1. Start
step 2. Read the number n
step 3. [Initialize]
i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
[process finish of calculate the factorial value of a number]
Flowchart for calculate factorial value of a number:
Figure: Flowchart for calculate factorial value of a number C program |
Subscribe to:
Posts (Atom)