Sunday, November 18, 2012

Star Pyramid

Q. Write a C program to accept the number of rows of pyramid from user and print the correspondence star triangle.
For example,

Enter number by user : 7

*
**
***
****
***
**
*

Ans.

/*c program for star pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 printf("Enter number of rows : ");
 scanf("%d", &num);
 for(r=1; r<=(num/2)+1; r++)
 {
  for(c=1; c<=r; c++)
    printf("*");
  printf("\n");
 }
 for(r=1; r<=(num/2); r++)
 {
  for(c=r; c<=num/2; c++)
    printf("*");
  printf("\n");
 }
 return 0;
}

The output of above program would be:

Output of star pyramid C program
Figure: Screen shot for star pyramid C program

Character Pyramid

Q. Write a C program to print the following character pyramid:

A
BA
ABA
BABA
ABABA

Ans.

/*c program for character pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 char ch='A',st='B';
 printf("Enter number of rows: ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=r; c>=1; c--)
  {
   if(c%2==0)
     printf("%c", st);
   else
     printf("%c", ch);
  }
  printf("\n");
 }
 return 0;
}

The output of above program would be:

Output of character pyramid C program
Figure: Screen shot for character pyramid C program


Saturday, November 10, 2012

User Define Function- Power

Q. Write a C program to create function power and calculate the power of any number.
For example:
Assume number = 5
Power = 3
Result = 5*5*5 = 125

Ans.

/*c program for creating user define function power and calculating power of number*/
#include<stdio.h>
int power(int , int );
int main()
{
 int num,pow,res;   //res = result
 printf("Enter any number : ");
 scanf("%d", &num);
 printf("Enter power of number : ");
 scanf("%d", &pow);
 res = power(num,pow);
 printf("%d's power %d = %d",num,pow,res);
 return 0;
}

int power(int n, int p)
{
 int r=1;
 for(; p>=1; p--)
    r = r*n;
 return r;
}

The output of above program would be:


Output of user define function "power" C program
Figure: Screen shot for calculating power of any number
using user define function power C program

User Define Function- Reverse Number

Q. Write a C program to create a function that would reverse the any number.
For example:
Assume enter number = 57429
Result = 92475

Ans.

/*c program for reverse number using user define function- rev */
#include<stdio.h>
int rev(int );
int main()
{
 int num,res;  // res = result
 printf("Enter any number : ");
 scanf("%d", &num);
 res = rev(num);
 printf("Reverse order number = %d",res);
 return 0;
}

int rev(int n)
{
 int r=0;
 for(; n>=1; n=n/10)
   r = r*10 + n%10;
 return r;
}

The output of above program would be:


Output of reverse number using user define function C program
Figure: Screen shot for reverse digit using
user define function C program


User Define Function- LeapYear

Q. Write a C program to find entered year is leap year or not using own created function say "leap".

Ans.

/*c program for find year is leap year or not using user define function leap*/
#include<stdio.h>
int leap(int );
int main()
{

 int year;
 printf("Enter any year : ");
 scanf("%d", &year);
 if(leap(year))
   printf("\n%d is leap year",year);
 else
   printf("\n%d is not leap year",year);
 return 0;
}

int leap(int y)
{
 if((y%400==0 && y%100==0)||(y%4==0))
    return 1;
 else
    return 0;
}

The output of above program would be:


Output of check year is leap year or not using user define function C program
Figure: Screenshot for user define function to
check year is leap year C program



Output of check year is leap year or not using user define function C program
Figure: Screenshot for user define function to 
check year is not leap year C program


Sunday, November 4, 2012

User Define Function- Factorial

Q. Write a function fact to calculate the factorial value of any number.

Ans.

/*c program for make function fact to calculate factorial value of any number*/
#include<stdio.h>
int fact(int );
int main()
{

 int num,f;
 printf("Enter any number : ");
 scanf("%d", &num);
 f = fact(num);
 printf("Factorial value of %d is %d",num,f);
 return 0;
}

int fact(int n)
{
 int z=1;
 for(; n>=1; n--)
    z = z * n;
 return(z);
}

The output of above program would be:


Output of calculating factorial value using user define function C program
Figure: Screenshot for find factorial value using
user define function C program

Number Pyramid

Q. Write a C program to print the following number pyramid :

1 2 3 4 5
 1 2 3 4
  1 2 3
   1 2
    1

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{
 int num,n,r,c,sp;
 printf("Enter any number : ");
 scanf("%d", &num);
 n=num;
 for(r=1; r<=num; r++,n--)
 {
  for(sp=1; sp<r; sp++)
     printf(" ");
  for(c=1; c<=n; c++)
     printf("%d ",c);
  printf("\n");
 }
 return 0;
}

The output of above program would be:

Output of number pyramid C program
Figure: Screenshot for number pyramid C program


Number Pyramid

Q. Write a C program to print the following number pyramid as:

1 2 3 4 5
 2 3 4 5
  3 4 5
   4 5
    5

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=1; sp<r; sp++)
     printf(" ");
  for(c=r; c<=num; c++)
     printf("%d ",c);
  printf("\n");
 }
 return 0;
}

The output of above program would be:

Output of number pyramid C program
Figure: Screenshot for number pyramid C program


Saturday, November 3, 2012

Floyd Triangle

Q. Write a C program to print the following number pyramid or Floyd triangle:

       1
      2 3
     4 5 6
    7 8 9 10

Ans.

/*c program to print the number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,sp,i=1;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=1; sp<=num-r; sp++)
    printf(" ");
  for(c=1; c<=r; c++,i++)
    printf("%d ",i);  
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of Floyd triangle C program
Figure: Screen shot for Floyd triangle C program

Friday, November 2, 2012

Flowchart for Fibonacci Series

Q. Draw the flowchart of Fibonacci series C program.

Ans.

Flowchart for Fibonacci series C program as:

Fibonacci series Flowchart
Figure: Flowchart for generate Fibonacci series



Related programs:

Number Character Pyramid

Q. Write a C program to print the following number character pyramid.

1
AB
123
ABCD
12345

Ans.

/*c program for number character pyramid*/
#include<stdio.h>
int main()
{
 int num=5,r,c;
 char ch;
 for(r=1; r<=num; r++)
 {
  if(r==2 || r==4)
  {
   ch='A';
   for(c=1; c<=r; c++,ch++)
     printf("%c",ch);
  }
  else
  {
   for(c=1; c<=r; c++)
     printf("%d",c);
  }
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number character pyramid C program
Figure: Screen shot for number character pyramid C program