Sie sind auf Seite 1von 23

Practical File

Of “C”
Programing
Department of computer science and application
Submitted To:- Submitted By:- Wayne D. Amewu

Roll No: B40719010 BSC I.T

Wayne BSc IT 1s t Sem


INDEX
Sr. No. Program Name Signature

1 Write a C Program to check if a given


number is Odd or even

2 Write a C Program to reverse a number and


check whether it is palindrome or not

3 Write a C Program to compute Sum of Digit


in a given Number

4 Write a C Program to find whether a given


number is Prime or Not

5 To write a C Program,
Using Switch to
Implement Simple
Calculator (ADD, MIN,
DIV, MUL)
6 To write a C program to illustrate Call by
Value and Call by Reference

7 To write a C Program to
Find Factorial of a
Number using
Recursion

8 To write a C program to check whether a


given string is palindrome or not

Wayne BSc IT 1s t Sem


9 To write a C program for to read two strings
and concatenate the
Strings

10 To write a C Program to implement the


following
Pointer Concept:-
a)Pointer to Pointer
b)Pointer to Structure.
Pointer to Function

11 Using Array, write a C Program to


Implement the transpose of a
Matrix

12 Using Array, write a C Program to


Implement the Multiplication of a Matrix

13 Using Structure in C, write a Program to


create the record of 10
students consisting of Name, Age, Address
& their marks In
Percentage

14 To write a C program to Create a file and


store the Information

15 To write a C program to illustrate reading of


Data from a File

Wayne BSc IT 1s t Sem


Program 1
Write a C Program to check if a given number is Odd or Even

#include<stdio.h>
main()
{
int num;
printf ("Enter an integer");
scanf ("%d",&num); if (num%2 == 0)
printf ("%d is an even number", num) ;
else
printf ("%d is an odd number", num); return 0;
}

Output 1

1
Wayne/10 Bsc IT 1st
Program 2
Write a C Program to reverse a number and check whether it is
palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int num, temp, remainder, reverse = 0;
printf("Enter an integer \n");
scanf("%d", &num);

temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n",reverse);
if (temp == reverse)
printf("Number is a palindrome \n");
else
printf("Number is not a palindrome \n"); getch(); }

Output:- 2

2
Wayne/10 Bsc IT 1st
Program 3
Write a C Program to compute Sum of Digit in a given Number

#include <stdio.h>
main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;

while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}

printf("Sum of digits of %d = %d\n", n, sum);


}

Output:- 3

3
Wayne/10 Bsc IT 1st
Program 4
Write a C Program to find whether a given number is Prime or Not.
#include <stdio.h>
main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

for(i = 2; i <= n/2; ++i)


{
if(n%i == 0)
{
flag = 1;
break;
}
}

if (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
}
Output:- 4

4
Wayne/10 Bsc IT 1st
Program 5
Write a C Program, Using Switch to Implement Simple Calculator
(ADD, MIN, DIV,MUL)
#include <stdio.h>
main()
{
char operator;
double first number, second number;

printf("Enter an operator (+, -, *,/): ");


scanf("%c", &operator);

printf("Enter two operands: ");


scanf("%lf %lf",&first number, &second number);

switch(operator)
{
case '+':
printf ("%.1lf + %.1lf = %.1lf",first number, second number, first number +
second number);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, second number, first number – second
number);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, second number, first number * second
number);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, second number, first number / second
number);
break;
default:
printf("Error! operator is not correct");
}
}

5
Wayne/10 Bsc IT 1st
Output:- 5

6
Wayne/10 Bsc IT 1st
Program 6
Write a C program to illustrate Call by Value and Call by Reference
#include<stdio.h>
void change
(int num)
{
printf("Before adding value inside function num=%d \n” , num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main()
{
int x=100;
printf("Before function call x=%d \n", x);
change(x);
printf("After function call x=%d \n", x);

}
Output:- 6

7
Wayne/10 Bsc IT 1st
Program 7
Write a C Program to Find Factorial of a Number using Recursion
#include<stdio.h>
int find_factorial(int);
int main()
{
int num, fact;
printf("\nEnter any integer number:");
scanf("%d",&num);

fact =find_factorial(num);

printf("\nfactorial of %d is: %d",num, fact);


return 0;
}
int find_factorial(int n)
{

if(n==0)
return(1);

return(n*find_factorial(n-1));
}
Output:- 7

8
Wayne/10 Bsc IT 1st
Program 8
Write a C program to check whether a given string is palindrome or
not
#include <stdio.h>
#include <string.h>

int main()
{
char string1[20];
int i, length;
int flag = 0;

printf("Enter a string:");
scanf("%s", string1);

length = strlen(string1);

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


{
if(string1[i] != string1[length-i-1])
{
flag = 1;
break;
}
}
if (flag)
{
printf("%s is not a palindrome", string1);
}
else
{
printf("%s is a palindrome", string1);
}
return 0;
}
Output:- 8

9
Wayne/10 Bsc IT 1st
Program 9
Write a C program to read two strings and concatenate the Strings
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000], b[1000];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a, b);
printf("String obtained on concatenation: %s\n", a);
return 0;
}
Output:- 9

10
Wayne/10 Bsc IT 1st
Program 10
Write a C Program to implement the following Pointer Concept:
a) Pointer to Pointer
#include <stdio.h>

int main () {

int var;
int *ptr;
int **pptr;

var = 8006;

/* take the address of var */


ptr = &var;

/* take the address of ptr using address of operator & */


pptr = &ptr;

/* take the value using pptr */


printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);

return 0;
}
Output:-

b) Pointer to Structure
#include <stdio.h>
struct my_structure
{
char name[20];
int number;
int rank;
};

11
Wayne/10 Bsc IT 1st
int main()
{
struct my_structure variable = {"Shivam", 06, 1};
struct my_structure *ptr;
ptr = &variable;

printf("NAME: %s\n", ptr->name);


printf("NUMBER: %d\n", ptr->number);
printf("RANK: %d", ptr->rank);

return 0;
}

Output:-

c) Pointer to Function
#include <stdio.h>

void fun
(int a)
{
printf("Value of a is %d\n", a);
}

int main()
{

void (*fun_ptr)
(int) = &fun;
(*fun_ptr) (06);

return 0;
}
Output:-

12
Wayne/10 Bsc IT 1st
Program 11
Using Array, write a C Program to Implement the transpose of a
Matrix
#include <stdio.h>
void main()
{ int array[10][10];
int i, j, m, n;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the coefiicients of the matrix\n");

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


{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("The given matrix is \n"); for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is \n");
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
printf(" %d", array[i][j]);
}
printf("\n");
}

13
Wayne/10 Bsc IT 1st
Output 11

14
Wayne/10 Bsc IT 1st
Program 12
Using Array, write a C Program to Implement the Multiplication of a Matrix
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");

for (c = 0; c < p; c++)


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);

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


for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum; sum = 0;
}
}
printf("Product of the matrices:\n"); for (c = 0; c < m; c++) { for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
} } return 0; }

15
Wayne/10 Bsc IT 1st
Output:-

16
Wayne/10 Bsc IT 1st
Program 13
Using Structure in C, write a Program to create the record of 10 students
consisting of (Name, Age, Address & their marks In Percentage)
#include <stdio.h>
struct student {
char name[50];
int roll;
int age;
char address;
float marks; }
s[2];
int main(){
int i;
printf("Enter information of students:\n");
for(i=0; i<2; ++i){
s[i].roll = i+1;
printf("\nFor roll number%d,\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter age: ");
scanf("%d",&s[i].age);
printf("\n");
printf("Enter address: ");
scanf("%s",&s[i].address);
printf("\n");
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying Information:\n\n");
for(i=0; i<2; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("age: %.d",s[i].age);
printf("address: %.s",s[i].address);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}

17
Wayne/10 Bsc IT 1st
return 0; }
Output:-

18
Wayne/10 Bsc IT 1st
Program 14
Write a C program to Create a file and store the Information
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 1000
int main(){
char data[DATA_SIZE];
FILE * fPtr;
fPtr = fopen("C:/file1.txt", "w");
if(fPtr == NULL) {
printf("Unable to create file.\n");
exit(EXIT_FAILURE);
}
printf("Enter contents to store in file : \n");
fgets(data, DATA_SIZE, stdin);
fputs(data, fPtr);
fclose(fPtr);
printf("File created and saved successfully. \n"); return 0; }
Output:-

19
Wayne/10 Bsc IT 1st
Program 15
Write a C program to illustrate reading of Data from a File
#include <stdio.h>
#include <stdlib.h>
int main(){
char c[1000];
FILE *fptr;
if ((fptr = fopen("program.txt", "r")) == NULL)
{
printf("Error! opening file");
exit(1); }
fscanf(fptr,"%[^\n]", c);
printf("Data from the file:\n%s", c);
fclose(fptr);
return 0; }
Output:-

20
Wayne/10 Bsc IT 1st

Das könnte Ihnen auch gefallen