Monday, November 17, 2014

Elements Of Rectangular Pyramid



C program to Use structure within union & display the contents of structure elements

#include
#include
void main()
{
struct student
{
char name[30];
char sex;
int rollno;
float percentage;
};
union details
{
struct student st;
}
union details
{
struct student st;
};
Union details set;
printf("Enter details:");
printf("\n Enter name:");
scanf("%s",set.st.name);
printf("\n Enter roll no:");
scanf("%d",&set.st.rollno);
flushall();
printf("\n Enter sex:");
scanf("%c",&set.st.sex);
printf("\n Enter percentage:");
scanf("%f",&set.st.percentage);
printf("\n The student details are:\n");
printf("\n Name : %s",set.st.name);
printf("\n Rollno :%d",set.st.rollno);
printf("\n Sex:%c",set.st.sex);
printf("\n Percentage %f",set.st.percentage);
getch();
}

Output

Enter details:
Enter name : Pritesh
Enter rollno: 10
Enter sex : m
Enter percentage: 89
The student details are:
Name : Pritesh
Rollno: 10
Sex : m
Percentage : 89.000000

C Program to print iterative rectangular pyramid

#include
#include 
int main()
{
    int number, row, col;
    int displayNum;
    printf("\nEnter Number of Rows to be display : \n");
    scanf("%d", &number);
     for (row = 1; row <= number; row++)
       {
        displayNum = 1;
        for (col = 1; col <= number; col++)
          {
            printf("%d\t", displayNum++);
        }
        printf("\n");
    }

    return 0;
}

Output

Enter Number of Rows to be display : 5
1    2    3    4    5    
1    2    3    4    5    
1    2    3    4    5    
1    2    3    4    5    
1    2    3    4    5  

C Program to print subtracting rectangular pyramid


#include
#include
int main()
  {
    int number, row, col;
    int displayNum;
    printf("\nEnter Number of Rows to be display : \n");
    scanf("%d", &number);
     for (row = 1; row <= number; row++)
     {
        displayNum = number;
        for (col = 1; col <= number; col++)
          {
            printf("%d\t", displayNum--);
        }
        printf("\n");
    }
     return 0;
}
Output
Enter Number of Rows to be display : 5
5    4    3    2    1    
5    4    3    2    1    
5    4    3    2    1    
5    4    3    2    1    
5    4    3    2    1

C Program to print zero border rectangular pyramid

#include
#include 
int main()
 {
    int number, row, col;
    printf("\nEnter Number of Rows to be display : \n");
    scanf("%d", &number);
    for (row = 1; row <= number; row++)
       {
        for (col = 1; col <= number; col++)
          {
            if (row == 1 || row == number)
            {
                printf("0\t");
            }
           else if (col == 1 || col == number)
               {
                printf("0\t");
            }
            else
             {
                printf("1\t");
            }
        }
        printf("\n");
    }

    return 0;
}

Output


Enter Number of Rows to be display : 6
0    0    0    0    0    0    
0    1    1    1    1    0    
0    1    1    1    1    0    
0    1    1    1    1    0    
0    1    1    1    1    0    
0    0    0    0    0    0

No comments:

Post a Comment