C Solutions
C Solutions
Ashad 2070
}while(sum<=15);
root= pow(i,1/2);
printf("\n%3f",root);
return 0;
}
Output:
-1
1.000000
3. a.) write a program to read the number until -1 is encountered. Also count the number of even number and
odd numbers entered by the user.
#include<stdio.h>
void main(){
int n,e=0,o=0;
printf("enter the numbers");
do{
scanf("%d",&n);
if(n%2==0)
{
e+=1;
}
else{
o+=1;
}
}while(n!=-1);
printf("\n the %d odd and %d even",o,e);
}
Output
enter the numbers1 2 3 4 5 -1
Break Continue
Break is used to break loop or iteration. Continue continues the loop or iteration.
Used with switch case loop. Not used with switch case.
Keyword used is "break". Keyword used is "continue".
Breaks loop and allows coming out from it. Allows iterating in the loop.
Control is transferred outside the loop. Control remains in the same loop.
E.g. E.g.
4.a.) Explain how the function are defined in c? Differentiate call by value and call by reference.
In C programming, functions are declared with a signature that includes the function name, return type,
and parameter types. The function definition contains the implementation of the code. Functions promote
code modularity and reusability, allowing you to call them from other parts of the program.
Syntax: return-type function_name(type1 argument1, type2 argument2, type3 argument3,…..........typen argument
n);
4. b.) Write a program using a function that returns the largest number from an array of numbers that is passed to
the
Function.
#include<stdio.h>
int large(int a[100],int n)
{
int i,temp=0;
for(i=0; i<n; i++)
{
if(a[i]>temp)
{
temp=a[i];
}
}
return temp;
}
void main()
{
int a[100],n,i=0;
printf("enter the number of numbers you want to compare");
scanf("%d",&n);
printf("enter all the numbers seperated by space");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
printf("the largest number is %d",large(a,n));
}
}
Output:
enter the number of numbers you want to compare2
enter all the numbers seperated by space1 2
the largest number is 2
5. a.) How are the one dimensional and two dimensional arrays created in c? Explain with example.
Elements of one dimensional array can be represented either as a single column or single row. One dimensional a
Is specified with a single subscript to locate a specific element.
Syntax: datatype variable_name[size of array]
Example: int a[10]; it is an 1D array consisting 10 memory location allocated for elements of integer datatype
Two dimensional array can be seen as an array of array. A pair of indices representing the position of the element
in the array can be used to access each element of the array.
Syntax: data type array_name[row_size][column_size];
Example: int a[10][10] ;
It is an 2D array of integer datatype consisting 10 number of rows and 10 columns.
5. b.) write a c program to read two matrices from the user, add them and display the result in matrix
form. #include<stdio.h>
void main()
{
int m,n;
scanf("%d%d",&n,&m);
int a[m][n],b[m][n],sum[m][n];
scanf("%d",&a[i][j]);
scanf("%d",&b[i][j]);
sum[i][j]=a[i][j]+b[i][j];
printf("%d\t",sum[i][j]);
}
printf("\n");
Output:
enter the number of rows and columns
22
eter the 11 element of a matrix1
enter the 12 element of a matrix1
enter the 21 element of a matrix1
enter the 22 element of a matrix1
enter the 11 element of b matrix
2
enter the 12 element of b matrix
2
enter the 21 element of b matrix
2
enter the 22 element of b matrix
2
the sum
is 3 3
3 3
6. What do you mean by nested structures? Give suitable example. Write a program to read the heights of two
Students and display the difference between their heights. Use feet and inches as members of structures to define
height.
Nested structure means the structure within the structures.
Example: struct employee
{
char address[20];
float salary;
struct person
{
}s;
} p;
#include <stdio.h>
struct height {
int feet;
float inch;
int main() {
scanf("%f", &d1.inch);
scanf("%d", &d2.feet);
scanf("%f", &d2.inch);
++result.feet;
return 0;
Output:
Enter height of first person
Enter feet: 11
Enter inch: 1
int main()
char *string1[100];
char *string2[100];
printf("string\n");
scanf("%s", string1);
copy(string1, string2);
return 0;
*destination=*source-32;
source++;
destination++;
*destination = '\0';
Output:
string
sosyika
original sosyika
copied SOSYIKA
8. Write a program to read the details of book authors and write it to a file, until user confirms to end. Then read
and display the nth record in the file, where n is read from the user. The data for authors must be represented by
structures that contains name, nationality and number of books published.
#include<stdio.h>
void main()
struct author{
char name[20];
char nat[25];
int book;
int number;
}book;
FILE *f1 ;
char cont;
f1=fopen("books.txt","w+");
int i=0;
do {
scanf("%s",&book.name);
printf("enter nationality:\n");
scanf("%s",&book.nat);
scanf("%d",&book.number);
scanf(" %c",&cont);
i++;
}while(cont=='y' || cont=='Y' );
fclose(f1);
f1=fopen("books.txt","r");
int bok=i;
int k=1;
scanf("%d",&bok);
if(k==bok){
break;
Output:
enter the record for book no 1
enter the name
john
enter nationality:
british
enter the publishid book no:5
do you want to add more:
y
enter the record for book no 2
enter the name
hoover
enter nationality:
american
enter the publishid book no:4
do you want to add more:
n
enter the record no you want to search
1
1. Name=john
nationality=british number
of book published=5
Submitted by:
Sagar Poudel(080bce137)
Samyam Shrestha(080bce144)