0% found this document useful (0 votes)
74 views23 pages

Foc and C Assingment

The document contains sample code and explanations for various programming concepts in C like data structures, functions, file handling etc. It provides answers to questions on topics such as finding the largest number among three given numbers, checking if a number is even or odd, Fibonacci series, addition of matrices and searching an element in an array. The last few questions demonstrate concepts like structures, file handling and headings in HTML.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views23 pages

Foc and C Assingment

The document contains sample code and explanations for various programming concepts in C like data structures, functions, file handling etc. It provides answers to questions on topics such as finding the largest number among three given numbers, checking if a number is even or odd, Fibonacci series, addition of matrices and searching an element in an array. The last few questions demonstrate concepts like structures, file handling and headings in HTML.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Q1 Write a flowchart to find the large

largest number among three given numbers.


Answer.

.
Q2. Write a flowchart to check whether the number is even or odd.
Answer.

start

Enter a number

If
number%2
==0

Print even Print odd

stop
Q3. Demonstrate the following DOS commands: to view the contents of a directory,
Change from one directory to another, Create and delete directories, Change from
one drive to another, Copy files, Rename files, Delete files, Format a floppy disk.
Answer.

Change from one to another directory :-


:

Syntax:- cd

Example :-

2 . creating and deleting directory


Q5. WAP to print prime numbers up to 1 to n.
Answer.

/*code*/

#include<stdio.h>
void main(){
int i, num, n, count;
printf("Enter the range:
");
scanf("%d", &n);
printf("The prime numbers in between the range 1 to %d:",n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d \n",num);
}
}

Output: Enter the range:6

The prime numbers in between the range 1 to 6:

Q6. WAP to calculate the factorial of a given number.


Answer.

#include<stdio.h>
int main(){

int x,fact=1,n;

printf("Enter a number to find factorial: ");

scanf("%d",&n);

for(x=1;x<=n;x++)

fact=fact*x;

printf("Factorial of %d is: %d",n,fact);

return 0;

Output: Enter a number to find factorial: 4

Factorial of 4 is: 24
Q7 WAP to find the largest number among three given numbers.

Answer:

#include <stdio.h>

int main() {

int n1, n2, n3;

printf("Enter three different numbers: ");

scanf("%lf %lf %lf", &n1, &n2, &n3);

// if n1 is greater than both n2 and n3, n1 is the largest

if (n1 >= n2 && n1 >= n3)

printf("%.2f is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the largest

if (n2 >= n1 && n2 >= n3)

printf("%.2f is the largest number.", n2);

// if n3 is greater than both n1 and n2, n3 is the largest

if (n3 >= n1 && n3 >= n2)

printf("%.2f is the largest number.", n3);


return 0;

Ouput:

Enter three different numbers: 3

5.00 is the largest number.

Q8: WAP to search a number in an array.

Answer.
/**
* C program to search element in array
*/

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;

/* Input size of array */


printf("Enter size of array: ");
scanf("%d", &size);

/* Input elements of array */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nEnter element to search: ");


scanf("%d", &toSearch);
/* Assume that element does not exists in array */
found = 0;

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


{
/*
* If element is found in array then raise found flag
* and terminate from loop.
*/
if(arr[i] == toSearch)
{
found = 1;
break;
}
}

/*
* If element is not found in array
*/
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array", toSearch);
}

return 0;

Q9.wap to perform the addition of two matrices.

Answer.

#include <stdio.h>

int main() {

int r, c, a[100][100], b[100][100], sum[100][100], i, j;

printf("Enter the number of rows (between 1 and 100): ");

scanf("%d", &r);

printf("Enter the number of columns (between 1 and 100): ");

scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");

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

for (j = 0; j < c; ++j) {

printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &a[i][j]);

printf("Enter elements of 2nd matrix:\n");

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

for (j = 0; j < c; ++j) {

printf("Enter element b%d%d: ", i + 1, j + 1);

scanf("%d", &b[i][j]);

// adding two matrices

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

for (j = 0; j < c; ++j) {

sum[i][j] = a[i][j] + b[i][j];

// printing the result

printf("\nSum of two matrices: \n");

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

for (j = 0; j < c; ++j) {

printf("%d ", sum[i][j]);


if (j == c - 1) {

printf("\n\n");

return 0;

Output:

Enter the number of rows (between 1 and 100): 2

Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:

Enter element a11: 2

Enter element a12: 3

Enter element a13: 4

Enter element a21: 5

Enter element a22: 2

Enter element a23: 3

Enter elements of 2nd matrix:

Enter element b11: -4

Enter element b12: 5

Enter element b13: 3

Enter element b21: 5

Enter element b22: 6

Enter element b23: 3


Sum of two matrices:

-2 8 7

10 8 6

Q10:wap to generate Fibonacci series using functions.

Answer.

#include<stdio.h>

void fibonacciSeries(int range)

int a=0, b=1, c;

while (a<=range)

printf("%d\t", a);

c = a+b;

a = b;

b = c;

int main()

int range;

printf("Enter range: ");


scanf("%d", &range);

printf("The fibonacci series is: \n");

fibonacciSeries(range);

return 0;

Output: Enter range: 50

The fibonacci series is:

0 1 1 2 3 5 8 13 21 34

Q11:wap a swap two given numbers by using function as call by


reference.

Answer.

#include<stdio.h>

#include<conio.h>

void swap(int *,int *); // Declaration of function

void main( )

int a = 10, b = 20 ;

printf(" Before swapping");

printf( " a = %d b = %d", a, b );

swap(&a,&b); // call by reference

printf("n After swapping");

printf( "n a = %d b = %d", a, b );


getch();

void swap( int *x, int *y )

int t;

t = *x;

*x = *y;

*y = t;

Output :

Before sawping

A =10 B =20

After sawping

A = 20 B =10

Q12: wap to demonstrate file handling function to open and


close a file.

Answer:
Q13: wap to demonstrate the use of structure in c.

Answer.

Define Structures

Before you can create structure variables, you need to define its data type. To define a struct,
the struct keyword is used.

Syntax of struct

struct structureName {

dataType member1;

dataType member2;

...

};

For example,

struct Person {

char name[50];

int citNo;

float salary;

};

Here, a derived type struct Person is defined. Now, you can create variables of this type.

Create struct Variables

When a struct type is declared, no storage or memory is allocated. To allocate


memory of a given structure type and work with it, we need to create variables.

Here's how we create structure variables:


struct Person {

// code

};

int main() {

struct Person person1, person2, p[20];

return 0;

Another way of creating a struct variable is:

struct Person {

// code

} person1, person2, p[20];

In both cases,

person1 and person2 are struct Person variables

p[] is a struct Person array of size 20.

Access Members of a Structure

There are two types of operators used for accessing members of a structure.

. - Member operator

-> - Structure pointer operator (will be discussed in the next tutorial)

Suppose, you want to access the salary of person2. Here's how you can do it.
person2.salary

Example 1: C structs

#include <stdio.h>

#include <string.h>

// create struct with person1 variable

struct Person {

char name[50];

int citNo;

float salary;

} person1;

int main() {

// assign value to name of person1

strcpy(person1.name, "George Orwell");

// assign values to other person1 variables

person1.citNo = 1984;

person1. salary = 2500;

// print struct variables

printf("Name: %s\n", person1.name);

printf("Citizenship No.: %d\n", person1.citNo);


printf("Salary: %.2f", person1.salary);

return 0;

Output:

Name: George Orwell

Citizenship No.: 1984

Salary: 2500.00

In this program, we have created a struct named Person. We have also created a
variable of Person named person1.

In main(), we have assigned values to the variables defined in Person for the
person1 object.

strcpy(person1.name, "George Orwell");

person1.citNo = 1984;

person1. salary = 2500;

Notice that we have used strcpy() function to assign the value to person1.name.

This is because name is a char array (C-string) and we cannot use the assignment
operator = with it after we have declared the string.

Finally, we printed the data of person1.


Q14: wap to demonstrate headings and paragraphs in html.

Answer.

What is an HTML Heading?

An HTML heading is a title or subheading that we want to display on a webpage. In


HTML, there are six headings defined using <h1> to <h6> tags. h1 is the highest
level or the main heading, while h6 is the least important.

The text inside the heading tags <h1>TEXT</h1> shows on the browser. The size of
the text depends on the heading tag.

HTML Headings Syntax:

<h1>TEXT</h1>

HTML Heading Tags

The following are the six HTML tags for different heading sizes.

<h1>Heading 1</h1> - (Most Important)

<h2>Heading 2</h2>

<h3>Heading 3</h3>

<h4>Heading 4</h4>

<h5>Heading 5</h5>

<h6>Heading 6</h6> - (Least Important)

Here is a simple example in HTML to display the H1 through H6 headings on a web


page:

<!DOCTYPE html>

<html>
<head>

<title>Heading in HTML</title>

</head>

<body>

<h1>This
s is Heading 1</h1>

<h2>This is Heading 2</h2>

<h3>This is Heading 3</h3>

<h4>This is Heading 4</h4>

<h5>This is Heading 5</h5>

<h6>This is Heading 6</h6>

</body>

</html>

Output:

What is an HTML Paragraph?

Paragraphs tags or <p> tags in HTML help us create paragraphs on a web page. On web browsers, paragraphs
display as blocks of text separated from adjacent blocks by blank lines, white spaces, or first-line
first indentation.

You can use a <p> tagg followed by the content you want to display in your paragraph and a </p>. Whenever
the web browser comes across a <p> tag, it starts its contents on a new line.

HTML Paragraph Syntax:


<p>Paragraph Content</p>

Example:
<!DOCTYPE html>

<html>

<body>

<h1>The p element</h1>

<p>This is a paragraph.</p>

<p>This is a paragraph.</p>

<p>This is a paragraph.</p>

</body>

</html>

Output:
The p element

This is a paragraph.

This is a paragraph.

This is a paragraph.

Q15: wap to create all lists (ordered and unordered ) in html. Also add the
functionality of hyperlinks.
Answer:
Q16: wap to inserts images and tables in html.
Answer:

<!DOCTYPE html>

<html>

<body>

<h2>HTML Image</h2>

<img src="pic_trulli.jpg" alt="Trulli" width="500" height="333">

<h1>The table element</h1>

<table>

<tr>

<th>Month</th>

<th>Savings</th>

</tr>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</table>

</body>

</html>

Output:
Q17:: wap to create forms using th
the various form elements in html.
Answer.
Q4. Demonstrate the steps of mail merge in MS Word.

You might also like