You are on page 1of 25

PROBLEM SOLVING

USING C
Practical File

Submitted by : Shubham Shrivastav


Branch : MCA 1st sem
Submitted to : Arvind Pawar Sir
INDEX
S.no Name of Practical Remark

1. WAP in C to print the Fibonacci Series for 1 to n values.

2. WAP in C that use non recursive function to find the Factorial of given
integer.
3. WAP in C to Check leap year.

4. WAP in C to Check weather a number is even or odd using ternary operator.

5. WAP in C for Changing the value of two variable without using third
variable.
6. WAP in C to generate all the prime numbers between 1 and n, where n is a
value supplied by the user.
7. WAP in C to print the numbers in triangular form

8. WAP in C to find sum and average of two numbers.

9. WAP in C to perform addition of two matrices.

10. WAP in C to perform the multiplication of two matrices.

11. WAP in C to display position or index in the string.

12. WAP to concatenate two strings using pointer.

13. WAP in C to traverse the string using pointers.

14. WAP in C to compare two strings using pointers.

15. WAP in C for using macro to print the elements of the array.

16. Create a Book Structure containing book_id, title, author name, price. Write
a C program to pass a structure as a function argument and print the book
details.
17. WAP in C for nested structure.

18. WAP in C to display the contents of a file.

19. WAP in C to copy the content of one file to another.

20. WAP in C to read and write to a text file.


1. WAP in C to print the Fibonacci Series for 1 to n values.

#include <stdio.h>
#include <conio.h>
void main()
{

int i, n;
int a = 0, b = 1;
int fab;

printf("Enter the number of terms: ");


scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", a, b);

for (i = 3; i <= n; ++i)


{
fab = a + b;
printf("%d, ", fab);
a = b;
b = fab;
}

getch();
}
Output:
2. WAP in C that use non recursive function to find the Factorial of given
integer.

#include<stdio.h>
#include<conio.h>
void main()
{
int factorial(int);
int fact,num;
printf("ENTER NUMBER : ");
scanf("%d",&num);
fact=factorial(num);
printf("\nFACTORIAL OF GIVEN NUMBER IS %d ",fact);
getch();
}

int factorial(int n)
{
int result=1,i;
for(i=n;i>0;i--)
result=result*i;
return(result);
}

Output:
3. WAP in C to Check leap year.

#include<stdio.h>
#include<conio.h>

void main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);

if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}

getch();
}

Output:
4. WAP in C to Check weather a number is even or odd using ternary
operator.

#include<stdio.h>
#include<conio.h>

void main()
{

int n;

printf("Enter an integer number\n");


scanf("%d", &n);

(n % 2 == 0) ?
(printf("%d is Even number\n", n)) :
(printf("%d is Odd number\n", n));

getch();
}

Output:

5. WAP in C for Changing the value of two variable without using third
variable.
#include<stdio.h>
#include<conio.h>

void main()
{

int a,b,c;

printf("Enter an integer numbers\n");


scanf("%d %d", &a,&b);
printf("a=%d \nb=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("after swap \na=%d \nb=%d",a,b);

getch();
}

Output:

6. WAP in C to generate all the prime numbers between 1 and n, where n


is a value supplied by the user.
#include<stdio.h>
#include<conio.h>
void main()
{

int num,i,count,n;
printf("Enter max range: ");
scanf("%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 ",num);
}

getch();
}

Output:

7. WAP in C to print the numbers in triangular form


1
12
123
1234
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter number of lines: ");
scanf("%d", &n);
printf("\n");
for(int i = 1; i <= n; i++)
{
for(int k= 0; k<= n - i; k++)
{
printf(" ");
}
for(int j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}
getch();
}
Output:

8. WAP in C to find sum and average of two numbers.

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,sum;
float avg;

printf("Enter first number :");


scanf("%d",&a);
printf("Enter second number :");
scanf("%d",&b);

sum=a+b;
avg=(float)(a+b)/2;

printf("\nSum of %d and %d is = %d",a,b,sum);


printf("\nAverage of %d and %d is = %f",a,b,avg);

getch();
}

Output:

9. WAP in C to perform addition of two matrices.


#include <stdio.h>
#include <conio.h>
int main()
{
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
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]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
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");
}
}

getch();
}

Output:
10. WAP in C to perform the multiplication of two matrices.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("matrix 1\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("matrix 2\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
getch();
}

Output:
11.WAP in C to display position or index in the string.
#include <stdio.h>
#include <conio.h>
void main()
{
char str[30],ch;
int ind[10],i,j;

printf("Enter string: ");


scanf("%[^\n]s",str);

printf("Enter character: ");


getchar();
ch=getchar();

j=0;
for(i=0; str[i]!='\0'; i++)
{
if(str[i]==ch)
ind[j++]=i;
}

printf("Input string is: %s\n",str);


printf("Indexes: ");
for(i=0; i<j; i++)
printf("%d \t",ind[i]);
getch();
}

Output:

Enter string: My name is Shubham


Enter Character: n
Input String is: My name is Shubham
Indexes: 3 15

12.WAP to concatenate two strings using pointer.


#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str1[20], str2[20];
char * s1 = str1;
char * s2 = str2;

printf("Enter 1st string: ");


gets(str1);
printf("Enter 2nd string: ");
gets(str2);

while(*(++s1));

while(*(s1++) = *(s2++));

printf("Concatenated string: %s", str1);


getch();
}
Output:

Enter 1st string: Shubham


Enter 2nd string : Shrivastav
Concatenated string :ShubhamShrivastav

13.WAP in C to traverse the string using pointers.


#include <stdio.h>
#include <conio.h>
#include <string.h>
int string_length(char*);
void reverse(char*);
void main()
{
char string[100];
printf("Enter a string\n");
gets(string);
reverse(string);
printf("Reverse of entered string is \"%s\".\n", string);
getch();
}
void reverse(char *string)
{
int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;
for ( c = 0 ; c < length/2 ; c++ )
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer+c) != '\0' )
c++;
return c;
}

Output:

Enter a String: Shubham


Reverse of entered string is: mahbuhS

14.WAP in C to compare two strings using pointers.

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{

char string1[50],string2[50],*str1,*str2;
int i,equal = 0;

printf("Enter The First String: ");


scanf("%s",string1);

printf("Enter The Second String: ");


scanf("%s",string2);

str1 = string1;
str2 = string2;
while(*str1 == *str2)
{

if ( *str1 == '\0' || *str2 == '\0' )


break;
str1++;
str2++;
}
if( *str1 == '\0' && *str2 == '\0' )
printf("\n\nBoth Strings Are Equal.");
else
printf("\n\nBoth Strings Are Not Equal.");
getch();
}

Output:
Enter 1st string: Shubham
Enter 2nd string: Shubham

Both String Are Equal.

15.WAP in C for using macro to print the elements of the array.


#include <stdio.h>
#include <conio.h>
#define PRINTARRAY(array, length) \
for(int i = 0; i < length; i++) \
printf("%d\t", array[i]);
void main()
{
int array[5] = {4, 2, 3, 1, 0};
PRINTARRAY(array, 5);
getch();
}

Output:
16. Create a Book Structure containing book_id,title,author,name ,price.
Write a C program to pass a structure as a function argument and print
the book details.

#include <stdio.h>
#include <conio.h>
#include<string.h>
struct book
{
int book_id;
char title[30];
char author[20];
float price;
};
void print_book(struct book b1);
void main()
{
struct book b1;
printf("\n enter book details: ");
printf("\nBook Id ");
scanf("%d",&b1.book_id);
printf("Book title ");
scanf("%s",&b1.title);
printf("Book author ");
scanf("%s",&b1.author);
printf("Book price ");
scanf("%f",&b1.price);
print_book(b1);
getch();
}
void print_book(struct book b1)
{
printf("\n book details:\n book id%d\nbook title %s\nbook author %s\nbook price
%f",b1.book_id,b1.title,b1.author,b1.price);
}

Output:

17.WAP in C for nested structure.


#include <stdio.h>
#include <conio.h>
#include <string.h>
struct student{
char name[30];
int rollNo;
struct dateOfBirth{
int dd;
int mm;
int yy;
}DOB;
};
void main()
{
struct student std;
printf("\nEnter name: ");
gets(std.name);
printf("Enter roll number: ");
scanf("%d",&std.rollNo);
printf("Enter Date of Birth [DD MM YY] format: ");
scanf("%d%d%d",&std.DOB.dd,&std.DOB.mm,&std.DOB.yy);
printf("\nName : %s \nRollNo : %d \nDate of birth : %02d/%02d/%02d\
n",std.name,std.rollNo,std.DOB.dd,std.DOB.mm,std.DOB.yy);
getch();
}

Output:

Enter name: Shubham


Enter roll number:170938
Enter date of Birth[DD MM YY] format :14
08
2001
Name: Shubham
Roll No:170938
Date of Birth:14/08/2001

18.WAP in C to display the contents of a file.


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void main()
{
FILE *fptr;

char filename[100], c;

printf("Enter the filename to open \n");


scanf("%s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}

fclose(fptr);
getch();
}

Output:

19.WAP in C to copy the content of one file to another.


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
getch();
}

20. WAP in C to read and write to a text file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
FILE* ptr;
char ch;
ptr = fopen("test.txt", "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
do {
ch = fgetc(ptr);
printf("%c", ch);
} while (ch != EOF);
fclose(ptr);
getch();
}

You might also like