Friday, 3 March 2017

BCA 2 nd sem /L-021/Solved Assignment/C Language Programming/2016-2017 New

Q.1.Write a C program to generate Pascal's triangle.

A.1.To build the triangle, start with "1" at the top, then continue placing numbers below it in a triangular pattern. 

Each number is the numbers directly above it added together.
(Here I have highlighted that 1+3 = 4)

pascals triangle

Patterns Within the Triangle

pascals triangle 1s, counting, triangular

#include <stdio.h>
 
long factorial(int);
 
int main()
{
   int i, n, c;
 
   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);
 
   for (i = 0; i < n; i++)
   {
      for (c = 0; c <= (n - i - 2); c++)
         printf(" ");
 
      for (c = 0 ; c <= i; c++)
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
 
      printf("\n");
   }
 
   return 0;
}
 
long factorial(int n)
{
   int c;
   long result = 1;
 
   for (c = 1; c <= n; c++)
         result = result*c;
 
   return result;
}

Pascal triangle program

Q.2.Write a C Program to find the surface area and the volume of a sphere.
 (Surface Area = 4 π r2 and Volume = 4/ 3 π r3 )

A.2.

#include <stdio.h>
#include <math.h>
#include <conio.h>
 
#define PI 3.141
 
int main()
{
    float radius, surfaceArea;
    printf("Enter radius of Sphere\n");
    scanf("%f", &radius);
    /* Surface area of Sphere =
       4 X PI X Radius X Radius  */
    surfaceArea = 4*PI*radius*radius;
    printf("Total surface area of Sphere : %0.4f\n",surfaceArea);
     
    getch();
    return 0;
}

Program Output
Enter radius of Sphere
7
Total surface area of Sphere : 615.6360
Volume of Sphere : 1436.4840

OR

Second Way:- To Do this program :- 

#include <stdio.h>#include <math.h> int main(){     float radius;    float surface_area, volume;     printf("Enter radius of the sphere : \n");    scanf("%f", &radius);    surface_area =  4 * (22/7) * radius * radius;    volume = (4.0/3) * (22/7) * radius * radius * radius;    printf("Surface area of sphere is: %.3f", surface_area);    printf("\n Volume of sphere is : %.3f", volume);    return 0;}

Output:

Enter radius of the sphere :

40

Surface area of sphere is: 19200.000

Volume of sphere is : 256000.000


Q.3.Write a C program to find whether the given matrix is symmetric or not.

A.3.
 a symmetric matrix is a square matrix that is equal to its transpose. Formally, matrix A is symmetric if
Because equal matrices have equal dimensions, only square matrices can be symmetric.
The entries of a symmetric matrix are symmetric with respect to the main diagonal. So if the entries are written as A = (aij), then aij = aji, for all indices i and j.
The following 3 × 3 matrix is symmetric:

Coding


#include<conio.h>
#include<stdio.h>
void main()
{
 int a[10][10],i,j,m;
 clrscr();
 printf("Enter order of square matrix: ");
 scanf("%d",&m);
 for(i=1;i<=m;i++)
 {
  for(j=1;j<=m;j++)
  {
   printf("Enter value of a[%d][%d]: ",i,j);
   scanf("%d",&a[i][j]);
  }
 }
 for(i=1;i<=m;i++)
 {
  for(j=1;j<=m;j++)
  {
   if(a[i][j]!=a[j][i])
   {
    printf("\n\nMatrix is not symmetric");
    getch();
    exit(0); 
   }
  }
 }
 printf("\n\nMatrix is symmetric");
 getch();
 }


Q.4.Write a interactive C program to create records of 15 students, where each record has fields name, rollno, GPA and fees. Write a function calcfee ( ) to reduce the fees of those students who have obtained GPA greater than 9.0 by 25% of the original fees, 10% fee concession if GPA is between 8.0 and 8.9 and 5% concession if the GPA is between 7.5 and 7.9. Display all the records before and after updation.

A.4.

#include<stdio.h> #define SIZE 50

struct student

{

char name[30]; int rollno;

int gpa,fee;


void calcfee(struct student st[])

{

for (i = 0; i < n; i++)

{

if(st[i].gpa>=9.0)

st[i].fee=st[i].fee­(st[i].fee*0.25);

else if(s[i]t.gpa<=8.9 && st[i].gpa<8.0)

{

St[i].fee=st[i].fee­(st[i].fee*0.05); printf("Total Fee=%d",st[i].fee); printf("roll no=%d", &st[i].rollno);

}

}

}

void main()

{

int i, j, max, count, total, n, a[SIZE], ni;



struct student st; clrscr();

printf("Enter how many students: "); scanf("%d", &n);


for (i = 0; i < n; i++)

{

printf("\nEnter details name, rollno,gpa,fee"); scanf("%s", &st[i].name);

scanf("%d", &st[i].rollno); scanf("%d", &st[i].gpa); scanf("%d", &st[i].fee);

}

Printf(“\nPrint Before update\n”) for (i = 0; i < n; i++)

{

printf("roll no=%d", &st[i].rollno); printf (“GPA=%d", &st[i].gpa);

printf("GPA=%d", &st[i].gpa); printf("fee=%d", &st[i].fee); calcfee(st[i]);

}

Printf(update data:\n”) getch();


}

Q.5.Using pointers, write a function that receives a string and a character as argument. Delete all occurrences of this character in the string. The function should return corrected string with no holes/spaces.

A.5.
In this program user ask to delete character from String using pointer concept. User declares char type array variable. User using malloc() function for the allocate the memory to the variable. Then user asks to enter the string then shifted the ptr to str variable for address reference. Then user puts the while condition to verify condition. In condition while (*ptr! =NULL) then if (*ptr!=ch) is that all the conditions got true the *ptr2 will be *ptr2++ with increment operator. And in the end the given result will be on the screen at the end.
Problem Statement:
This is C program where coder has to use pointer function for the deletion of character from string.
  1. Declare the variables.
  2. Using loop statement and control statements.
  3. Display result in the screen.
Here is C source code for Deleting the Character from the string using pointer function. The output of this program shown below. 

#include<stdio.h>

#include<conio.h>

#include<string.h>


void del(char str[], char ch);
void main() { char str[10]; char ch;

printf("\nEnter the string : "); gets(str);

printf("\nEnter character which you want to delete : "); scanf("%ch", &ch);

del(str, ch); getch();

}

void del(char str[], char ch) { int i, j = 0;

int size; char ch1; char str1[10];

size = strlen(str);

for (i = 0; i < size; i++) { if (str[i] != ch) {

ch1 = str[i]; str1[j] = ch1; j++;

}

}

str1[j] = '\0';

printf("\n corrected string is : %s", str1);

}

Q.6.Define a structure that describes a hotel. It should have members that include the name, address, star(5 star, 3 star or 2 star), average room charge and number of rooms. Write a function to perform the following operations:
(i) To print out hotels of a given grade in order of charges.
(ii) To print out hotels with room charges less than a given value.

A.6.

#include <stdio.h> struct hotel
{

char name[20]; char add[20]; int grade;

int arc; int rooms; };

void output(); void out();

struct hotel inn[]={ {"PLAZA","G­99,DELHI",3,4500,50}, {"MYUR","E­45,NOIDA",4,5000,100}, {"RAJDOOT","H­44,DELHI",2,4000,50}, {"SAMRATH","B­75,BOMBAY",5,6000,200}, {"SURYA","A­77,NOIDA",1,3500,150}


void main()

{

int go; clrscr();

printf("Enter 1 for grade search\n"); printf("Enter 2 to search by charge:"); scanf("%d",&go);

switch(go)

{

case 1: output(); break;

case 2: out(); break;

default:printf("Wrong input"); break;

}

getch();

}

void output()

{

int gr,i;

printf("Enter Grade 1 to 5:"); scanf("%d",&gr); if(gr>=1||gr<=5)

{


for(i=0;i<=4;i++)
{

if(inn[i].grade==gr)

printf("Hotel Name: %s\nAddress:%s\nGrade:%d\nAverage Room charge:%d\n\ Number of Rooms:%d",inn[i].name,inn[i].add,inn[i].grade,inn[i].arc,inn[i].rooms);

}

}

else

printf("Wrong grade input!");

}

void out()

{

int ch,i=0;

printf("Enter the Room charges not greater than 6000:"); scanf("%d",&ch);

while(i<5)

{

if(inn[i].arc<ch)

printf("Hotel Name: %s\nAddress:%s\nGrade:%d\nAverage Room charge:%d\n\ Number of Rooms:%d\n",inn[i].name,inn[i].add,inn[i].grade,inn[i].arc,inn[i].rooms); i++;

}
}

Q.7.Write an interactive C program which copies one file to another.

A.7.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
char ch,fname1[20],fname2[20];
printf("\n enter sourse file name");
gets(fname1);
printf("\n enter sourse file name");
gets(fname2);
fp1=fopen(fname1,"r");
fp2=fopen(fname2,"w");
if(fp1==NULL||fp2==NULL)
{
printf("unable to open");
exit(0);
}
do
{
ch=fgetc(fp1);
fputc(ch,fp2);
}
while(ch!=EOF);
fcloseall();
getch();
}


Q.8.Write an interactive C program to reverse the first n characters in a file.

A.8.
#include <stdio.h> #include <conio.h> #include <string.h> #include <process.h>

void main(int argc, char *argv[])

{

char a[15]; char s[20]; char n;

int k; int j=0; int i; int len;

FILE *fp;

if(argc!=3)

{

puts("Improper number of arguments."); exit(0);

}

fp = fopen(argv[1],"r"); if(fp == NULL)

{

puts("File cannot be opened."); exit(0);

}

k=*argv[2]­48;

n = fread(a,1,k,fp); a[n]='\0'; len=strlen(a); for(i=len­1;i>=0;i­­)

{

s[j]=a[i];

printf("%c",s[j]);

j=j+1;

}


s[j+1]='\0';
getch();

}

No comments:

Post a Comment