Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

 

COMPUTER SCIENCE FIRST YEAR - PROGRAMMING IN C


1 a) write an algorithm and draw the flowchart to find largest of three numbers

Algorithm: Step 1:Start

Step 2:Read three numbers A,B & C

Step 3:If A>B,then go to step 6

Step 4:If B>C,then print B & go to step 8

Step 5:print C is greatest & go to step 8

Step 6:If A>C,then print A is greatest & go to step 8

Step 7:Print C is greatest

Step 8:end

FLOWCHART:

 
 

1 b) write a C program to find the biggest of three numbers

#include <stdio.h>
#include<conio.h>
void main()
{
int num1, num2, num3;
clrscr();
printf("Enter the values of num1, num2 and num3\n");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2)
{
if (num1 > num3)
printf("%d is the greatest among three \n",num1);
else
printf("%d is greatest among three \n",num3);
}
else
{
if (num2 > num3)
printf("%d is the greatest among three \n",num2);
else
printf("%d is the greatest among three \n",num3);
}
getch();
}

OUTPUT:

Enter the values of num1, num2 and num3

89
65
78

89 is greatest among three.

 
 

2) Write a C program to Calculate of Factorial to given number using Recursion


#include <stdio.h>
#include<conio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
getch();
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}

OUTPUT:

Enter a positive integer: 5

Factorial of 5 = 120

 
 

3) write a C program for Addition of two dimensional matrices.


#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n;
clrscr();
printf("How many rows and columns?");
scanf("%d%d",&m,&n);
printf("\nEnter first matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
printf("\nEnter second matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&b[i][j]);
printf("\nMatrix after addition:\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
return 0;
}

OUTPUT:
How many rows and columns? 2
Enter the first matrix:
56
23
Enter second matrix:
46
24
Matrix after addition:
9 12
47

 
 

4)   Write a C program to create a single dimensional array of numbers   and display the contents and       
arrange this single dimensional  array of numbers into descending order   

#include <stdio.h> 
#include<conio.h> 
#define MAX 100 
int main() 

int arr[MAX],n,i,j; 
int temp; 
clrscr(); 
printf("Enter total number of elements: "); 
scanf("%d",&n); 
printf("Enter array elements:\n"); 
for(i=0;i< n;i++) 

printf("Enter element %d: ",i+1); 
scanf("%d",&arr[i]); 

printf("The elements of array before sorting are :\n"); 
for(i=0;i<n;i++) 
printf("%d\n",arr[i]); 
for(i=0;i< n;i++) 

for(j=i+1;j< n;j++) 

if(arr[i]<arr[j]) 

temp =arr[i]; 
arr[i] =arr[j]; 
arr[j] =temp; 



printf("\nArray elements after sorting are:\n"); 
for(i=0;i< n;i++) 

printf("%d\n",arr[i]); 

getch(); 
return 0; 

 
 
 
 
 
 

 
 

OUTPUT: 
 
Enter total number of elements: 6 
Enter array elements: 
Enter element 1: 25 
Enter element 1: 35 
Enter element 1: 84 
Enter element 1: 12 
Enter element 1: 36 
Enter element 1: 98 
The elements of array before sorting are : 
25 
35 
84 
12 
36 
98 
Array elements after sorting are:  
98 
84 
36 
35 
25 
12 
 

 
 

5) write a C program for Multiplication of Two matrices

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("Enter rows and columns of first matrix:");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of second matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("\nEnter first matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
printf("\nEnter second matrix:\n");
for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&b[i][j]);
printf("\Multiplication of Matrices is :\n");
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
printf("%d ",c[i][j]);
}
printf("\n");
}
}
else
printf("\nSorry!!!! Matrix multiplication can't be done");
getch();
return 0;
}

 
 

OUTPUT:

Enter rows and columns of first matrix: 3 3


Enter rows and columns of second matrix: 3 3

Enter the first matrix:


123
456
789
Enter second matrix:
987
654
321

Multiplication of Matrices is

30 24 18
84 69 54
138 114 90

 
 

6 ) write a C program to Generate a multiplication Table of given number and given limit using
for loop

#include <stdio.h>
#include<conio.h>
int main()
{
int n, i,l;
clrscr();
printf("Enter a number and limit for multiplication table: ");
scanf("%d %d",&n, &l);
for(i=1; i<=l; i++)
{
printf("%d * %d = %d \n", n, i, n*i);
}
getch();
return 0;
}

OUTPUT

Enter a number and limit for multiplication table:


25 20
25 * 1 = 25
25 * 2 = 50
25 * 3 = 75
25 * 4 = 100
25 * 5 = 125
25 * 6 = 150
25 * 7 = 175
25 * 8 = 200
25 * 9 = 225
25 * 10 = 250
25 * 11 = 275
25 * 12 = 300
25 * 13 = 325
25 * 14 = 350
25 * 15 = 375
25 * 16 = 400
25 * 17 = 425
25 * 18 = 450
25 * 19 = 475
25 * 20 = 500

 
 

7) write a C program to generate Fibonacci series using Recursion for given limit.

#include<stdio.h>
#include<conio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
clrscr();
printf("Enter the limit for fibonacci series : ");
scanf("%d",&n);
printf("Fibonacci series :\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
getch();
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}

OUTPUT

Enter the limit for fibonacci series : 10


Fibonacci series :

0
1
1
2
3
5
8
13
21
34

 
 

8 a) write a c program to calculate simple and compound interest

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
float p,r,SI,CI;
int n;
printf("Enter the value of Principal p = ");
scanf("%f",&p);
printf("Enter the value of Rate r = ");
scanf("%f",&r);
printf("Enter the value of Period in year n = ");
scanf("%d",&n);
SI = ((p*r*n)/100);
printf("Simple Interest SI=%f \n",SI);
ci=p*pow(1+r/100,n);
printf("Compound Interest CI=%f \n",CI);
getch();
return 0;
}

OUTPUT:

Enter the value of Principal p = 200


Enter the value of Rate r = 4
Enter the value of Period in year n = 12
Simple Interest SI= 96.0
Compound Interest CI=320.206451

 
 

8 b) write a C program to convert temperature in Celsius to Fahrenheit */

#include<stdio.h>
#include<conio.h>
int main()
{
float celsius, fahrenheit;
clrscr();
printf("Please Enter temperature in Celsius: \n");
scanf("%f", &celsius);
fahrenheit = ((celsius * 9)/5) + 32;
printf("\n %.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);
getch();
return 0;
}

OUTPUT:

Please Enter temperature in Celsius: 98

98.00 Celsius = 208.40 Fahrenheit

 
 

9 a) write a C program to find whether a number is prime number or not

#include <stdio.h>
#include<conio.h>
int main()
{
int i, num, isPrime;
isPrime = 1;
clrscr();
printf("Enter any number to check prime: ");
scanf("%d", &num);
for(i=2; i<=num/2; i++)
{
if(num%i==0)
{
isPrime = 0;
break;
}
}
if(isPrime == 1)
{
printf("%d is prime number", num);
}
else
{
printf("%d is composite number", num);
}
getch();
return 0;
}

OUTPUT:

Enter any number to check prime: 41


41 is prime number

 
 

9 b) write a function and call function to perform add, subtract and multiply two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter Two Values :");
scanf("%d%d",&a,&b);
sum(a,b);
mult(a,b);
sub(a,b);
getch();
}
sum(int x,int y)
{
int z;
z=x+y;
printf("Addtion : %d\n",z);
return 0;
}
mult(int x,int y)
{
int z;
z=x*y;
printf("Multiply : %d\n",z);
return 0;
}
sub(int x,int y)
{
int z;
z=x-y;
printf("Subtraction : %d\n",z);
return 0;
}

OUTPUT:

Enter Two Values : 12 4


Addtion : 16
Multiply : 48
Subtraction : 8

 
 

10) write a C program to check given year is Leap year or not

#include<stdio.h>
#include<conio.h>
int main()
{
int year;
clrscr();
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if (year%400 == 0)
printf("%d is a leap year.\n", year);
else if (year%100 == 0)
printf("%d isn't a leap year.\n", year);
else if (year%4 == 0)
printf("%d is a leap year.\n", year);
else
printf("%d isn't a leap year.\n", year);
getch();
return 0;
}

OUTPUT:

Enter a year to check if it is a leap year


1220
1220 is a leap year.

 
 

11) write the structure of C Program

A) There are six main sections to a basic c program.


The six sections are,
 Documentation section
 Link section
 Definition section
 Global Declarations
 Main functions
 Subprograms
Documentation Section : The documentation section is the part of the program where the
programmer gives the details associated with the program.
Ex:- /* program on string handling functions */

Link  Section : This part of the code is used to declare all the header files that will be used in the
program. This leads to the compiler being told to link the header files to the system libraries.
Ex:- #include<stdio.h> 

Definition Section : In this section, we define different constants. The keyword define is used in this
part.
Ex: #define PI=3.14

Global Declaration Section : This part of the code is the part where the global variables are
declared. All the global variable used are declared in this part. The user-defined functions are also
declared in this part of the code.
Ex: float area(float r);
int a=7;

Main Function Section : Every C-programs needs to have the main function. Each main function
contains 2 parts.
1) A declaration part : The declaration part is the part where all the variables are declared.
2) Execution part. The execution part begins with the curly brackets and ends with the curly
close bracket. Both the declaration and execution part are inside the curly braces.
Ex : void main()
{
Local declarations( ex int I;)  Declaration part
Statement1;
Statement 2;
.
. Execution part
.
Statement n;

Sub Program Section : All the user-defined functions are defined in this section of the program.
Ex : int add(int a, int b)

return a+b; 

 
 
 
 

 
 

12) Write about SWICH statement with Syntax.


A) A switch statement is used to choose a statement among several alternatives. The switch
statement is useful when a variable is to be compared with different constants and in case is
equal to a constant a set of statements are to be executed.
Syntax:
Switch(expression)
{
Case Constant 1;
Statement;
Case constant 2;
Statement;
…………….
…………….
Default;
Statement;
}
 
Ex:   #include <stdio.h> 
   int main () { 
     char grade = 'B'; 
   switch(grade) { 
        case 'A' : 
           printf("Excellent!\n" ); 
           break; 
        case 'B' : 
        case 'C' : 
          printf("Well done\n" ); 
           break; 
        case 'D' : 
           printf("You passed\n" ); 
           break; 
        case 'F' : 
           printf("Better try again\n" ); 
           break; 
       default : 
           printf("Invalid grade\n" ); 
     } 
        printf("Your grade is  %c\n", grade ); 
      return 0; 

 
OUTPUT : 
Well done
Your grade is B
 
 

 
 

13) Write a C program to perform string handling functions

#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
clrscr();
strcpy(str3, str1);
printf("The copied string is : %s\n", str3 );
strcat( str1, str2);
printf("The concatenated string is: %s\n", str1 );
len = strlen(str1);
printf("Length of the string is : %d\n", len );
strrev(str3);
printf("The reversed string is : %s\n", str3 );
getch();
return 0;

OUTPUT: 

The copied string is : Hello

The concatenated string is: HelloWorld

Length of the string is : 10

The reversed string is : olleH

 
 

14) What are the File operations in C .

Ans) The file operations in C are:


1. fopen(): Creation of a new file with attributes as “a” or “a+” or “w” or “w+”

2. Opening an existing file (fopen)


Ex: FILE *filePointer;
filePointer = fopen(“fileName.txt”, “w”);

3. Reading from file (fscanf or fgetc)


Ex: fscanf(filePointer, "% d", str1);

4. Writing to a file (fprintf or fputs)


Ex: fprintf(filePointer, "%", "We”);

5. Moving to a specific location in a file (fseek, rewind)

6. Closing a file (fclose)


Ex: fclose(filePointer);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 
 

15)  What is structure and Union ? Write its syntax with example.

Ans) Structure: Structure is a collection of variables (can be of different data types) stored
under a single name.

Syntax: struct structurename


{
Datatype member1;
Datatype member2;
................
};

Example: struct Person


{
char name[50];
int citNo;
float salary;
};

Union: A union is a special data type available in C that allows to store different data types in
the same memory location. You can define a union with many members, but only one member
can contain a value at any given time.

Syntax: union name


{
Datatype member1;
Datatype member2;
................
};

Example : union Data


{
int i;
float f;
char str[20];
}; 
 
 
 

You might also like