Monday, 1 September 2014

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 example6 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;
}

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();
 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.
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:
Output of sum of square of any number C program
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:
flowchart for accept number and calculate sum of square
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]

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]

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
simple if statement syntax and flowchart
Figure: Simple if statement syntax and flowchart

if-else 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;

nested if else flowchart
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;

else...if flowchart statement
Figure: flowchart for else...if statement