C Programs
C Programs
C Programs
USING C
Practical File
2. WAP in C that use non recursive function to find the Factorial of given
integer.
3. WAP in C to Check leap year.
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
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.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, n;
int a = 0, b = 1;
int 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;
(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;
getch();
}
Output:
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;
}
}
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
float avg;
sum=a+b;
avg=(float)(a+b)/2;
getch();
}
Output:
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;
j=0;
for(i=0; str[i]!='\0'; i++)
{
if(str[i]==ch)
ind[j++]=i;
}
Output:
while(*(++s1));
while(*(s1++) = *(s2++));
Output:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char string1[50],string2[50],*str1,*str2;
int i,equal = 0;
str1 = string1;
str2 = string2;
while(*str1 == *str2)
{
Output:
Enter 1st string: Shubham
Enter 2nd string: Shubham
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:
Output:
void main()
{
FILE *fptr;
char filename[100], c;
fclose(fptr);
getch();
}
Output:
#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();
}