BCP Solution
BCP Solution
N. G. PATEL POLYTECHNIC
GTU SOLUTIONS
COMPUTER ENGINEERING DEAPARTMENT
Subject with Code 4310702 – Basic Computer Programming
return 0;
}
Example:
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b;
a=10; b=20;
printf(“Relational Operators output n“);
printf(“a < b is : %d \n“,a<b);
printf(“a <= b is : %d \n“,a<=b);
printf(“a == b is : %d \n“,a==b);
/* Calculate percentage */
per = (phy + chem + bio + math + comp) / 5.0;
printf("Percentage = %.2f\n", per);
22. Write a program to find the largest no from three given no. [03]
#include <stdio.h>
#include<conio.h>
void main() {
int n1, n2, n3;
printf("Enter three numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);
getch();
}
23. Explain ternary operator. [03]
Programmers use the ternary operator for decision making in place of longer if and
else conditional statements.
The ternary operator take three arguments:
1. The first is a comparison argument
2. The second is the result upon a true comparison
3. The third is the result upon a false comparison
It helps to think of the ternary operator as a shorthand way or writing an if-else
statement. Here’s a simple decision-making example using if and else:
int a = 10, b = 20, c;
if (a < b) {
c = a;
}
else {
c = b;
}
printf("%d", c);
This example takes more than 10 lines, but that isn’t necessary. You can write the
above program in just 3 lines of code using a ternary operator.
Advantages of Function
The advantages of using functions are:
Avoid repetition of codes.
Increases program readability.
Divide a complex problem into simpler ones.
Reduces chances of error.
Modifying a program becomes easier by using function.
The sprintf() function reads the one or multiple values specified with their
sprintf()
matching format specifiers and store these values in a char[] array.
void main()
{
char ar[20] = "User M 19 1.85";
char str[10];
char ch;
int i;
float f;
getch();
}
printf("\n");
printf("The value in the target string is : %s ", target);
getch();
}
Output
Where, type-of-array: It is the type of elements that an array store. If array stores
character elements, then type of array is ‘char’. If array stores integer elements, then
type of array is ‘int’. Besides these native types, if type of elements in array is structure
objects, then type of array becomes the structure.
name-of-array: This is the name that is given to array. It can be any string but it is
usually suggested that some can of standard should be followed while naming arrays.
At least the name should be in context with what is being stored in the array.
[number of elements]: This value in subscripts [] indicates the number of elements
the array stores.
// valid
char name[13] = "StudyTonight";
char name[10] = {'c','o','d','e','\0'};
// Illegal
char ch[3] = "hello";
char str[4];
str = "hello";
2. Function Definition:
The syntax of function definition is :
return_type function_name(argument list)
{
body of the function;
}
3. Function call:
The function call is declared inside the main function and the syntax of function call
is: function_name(argument list);
39. Write a program to find out smallest number with its position from given array.
(04)
#include <stdio.h>
#include<conio.h>
void main() {
int e, i, sval, position;
Output:
Input the length of the array: 5
Input the array elements:
25
35
20
14
45
Smallest Value: 14
Position of the element: 3
40. Write all categories of user defined function and explain anyone with example.
(04)
43. Write a program to perform addition of Two 3X3 matrix. Display the result on your
screen.(04)
49. Write a Program which will copy one array into another using pointer. (03)
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int source_arr[MAX_SIZE], dest_arr[MAX_SIZE];
int size, i;
int *end_ptr;
return 0;
}
void printArray(int *arr, int size)
{
int i;
For example,
int *p[5];
It represents an array of pointers that can hold 5 integer element addresses.
‘&’ is used for initialisation.
For Example,
Accessing
Indirection operator (*) is used for accessing.
For Example,
for (i=0, i<3; i++)
printf ("%d", *p[i]);
Example program
#include<stdio.h>
main ( ){
// Pointer to an integer
int *ptr1, *ptr2;
// Pointer stores
// the address of N
ptr1 = &N;
ptr2 = &N;
return 0;
}
55. Write a Program which will create product structure with related member variable and
get one product information from user, store it in structure and display on your computer
screen. (04)
#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s;
int main() {
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
Output
Enter information:
Enter name: Jack
Enter roll number: 23
Enter marks: 34.5
Displaying Information:
Name: Jack
Roll number: 23
Marks: 34.5