Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

1 Write a C program to find sum and average of three numbers.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
int sum;
float average;
clrscr();
printf("Enter any three integers:\n");
scanf("%d%d %d",&a,&b,&c);
sum = a+b+c;
average=sum/3.0;
printf("Sum= %d\taverage= %0.2f of three
integers",sum,average);
getch();
}

OUTPUT;
2 Write a C program to find the greatest of three number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d%d%d",&n1,&n2,&n3);
if (n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);
if (n2 >= n1 && n2 >= n3)
printf("%d is the largest number.", n2);
if (n3 >= n1 && n3 >= n2)
printf("%d is the largest number.", n3);
getch();
}
3 Write a C program to find the greatest of three number using conditional
operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,large;
printf("Enter three different numbers: ");
scanf("%d%d%d",&a,&b,&c);
large=a>b?(a>c?a:c):(b>c?b:c);
printf("\nThe largest number is : %d",large) ;
getch();
}
4 Write a C program to find the average of 5 subject marks and assign grade.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
float avg;
clrscr();
printf("This program is to find the average and assign
grade");
printf("\nEnter the marks in 5 subjects:");
scanf("%d%d%dd%d ",&a,&b,&c.&d,&e);
sum=a+b+c+d+e;
avg=(float)sum/3;
if(avg>=75)
printf("\nGrade='A'");
else if(avg>=50 && avg<75)
printf("\nGrade='B'");
else if(avg>=25 && avg<50)
printf("\n Grade='C'") ;
else
printf("\n Grade='D'");
getch();
}
5.Write a C program to generate the first n terms of the Fibonacci sequence.
#include<stdio.h>
#include<conio.h>
void main()
{
int f0,f1,f,n,i;
clrscr();
printf("ENTER THE VALUE FOR n \n");
scanf("%d",&n);
f0=0;
f1=1;
printf("FIBONACCI SEQUENCE FOR THE FIRST %d TERMS:\
n",n);
i=0;
while(i<n)
{
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
}
getch();
}
6 Write a C program to Check whether given number is prime or not
#include<stdio.h>
#include<conio.h>
void main()
{
int i=2,n,flag=0,opt;
clrscr();
do
{printf("\n\n\tPRIME NUMBER CHECKER\n");
printf("\t*******************\n");
printf("\nEnter the number :");
scanf("%d",&n);
while(i<=n/2)
{
if(n%i==0)
{
flag=1;
break;
}
i++;
}
if(flag==0)
printf("\n%d is a prime number",n);
else
printf("%d is not a prime number ",n);
printf("\n\nPress 1 to continue.otherwise 0\n");
scanf("%d",&opt);
}
while(opt==1);
getch();
}
7 Write a C program to generate prime numbers between 1 to n.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact,j;
clrscr();
printf("enter the number:");
scanf("%d",&n);
printf("PRIME NO.IS:\n");
for(i=1;i<=n;i++)
{
fact=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
fact++;
}
if(fact==2)
printf(" %d",i);
}
getch( );
}
8 Write a C program to Check whether given number is Armstrong Number
orNot
#include<stdio.h>
#include<conio.h>
void main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
getch();
}
9 Write a C program perform arithmetic operations using switch statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch;
clrscr();
printf("ENTER TWO VALUES FOR a & b\n");
scanf("%d %d",&a,&b);
while(1)
{
printf("MENU OPTIONS \n");
printf("************\n");
printf("1.Addition ");
printf("2.Subtraction ");
printf("3.Multiplication\n");
printf("4.Division ");
printf("5.Modulus ");
printf(“6.Exit\n”);
printf("ENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch)
{
case 1: c=a+b;
printf("The addition of %d and %d is..%d\n",a,b,c);
break;
case 2: c=a-b;
printf("The subtraction of %d and %d is..%d\n",a,b,c);
break;
case 3: c=a*b;
printf("The multiplication of %d and %d is..%d\
n",a,b,c);
break;
case 4: c=a/b;
printf("The division of %d and %d is..%d\n",a,b,c);
break;
case 5: c=a%b;
printf("The modulus of %d and %d is..%d\n",a,b,c);
break;
case 6:exit(0);
default:printf("INVALID CHOICE\n");
}
}
getch();
}
10 WAP in C to find factorial of a given integer using non-recursive function.
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,i,f;
clrscr();
printf("ENTER A VALUE FOR n:\n");
scanf("%d",&n);
f=fact(n);
printf("THE FACTORIAL OF A GIVEN NO IS..%d",f);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return(f);
}
11 Write a C program to find factorial of a given integer using recursive
function
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,res;
clrscr();
printf("ENETR A NUMBER:\n");
scanf("%d",&n);
res=fact(n);
printf("THE FACTORIAL OF A GIVEN NUMBER IS..%d",res);
getch();
}
int fact(int n)
{
int r;
if(n==0)
return(1);
else
{
r=n*fact(n-1);
return(r);
}
}
12 WAP in C to swap the values of two variables using CALL by VALUE
#include<stdio.h>
#include<conio.h>
void swap(int x, int y);
void main ()
{
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
swap(a, b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
getch();
}
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
13 WAP in C to swap the values of two variables using CALL by REFRENCE
#include<stdio.h>
#include<conio.h>
void swap(int *x, int *y);
void main ()
{
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
getch();
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
14 WAP in C to find the value of one number raised to the power of another
number using function.
#include<stdio.h>
#include<conio.h>
void power(int base,int power);
void main()
{
int num1,pow;
printf("Enter the number:");
scanf("%d",&num1);
printf("Enter the power:");
scanf("%d",&pow);
power(num1,pow);
getch();
}
power(int base,int power)
{
int result,temp;
result=1;
temp=1;
while(temp<=power)
{
result = result*base;
temp++;
}
printf("the power of the no= %d",result);
}
15 WAP in C to find both the largest and smallest number in a list of integers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,small,large;
clrscr();
printf("Enter The Array Size:");
scanf("%d",&n);
printf("ENTER ELEMENTS OF ARRAY");
for(i=0;i<n;i++) // read the elements of an array
scanf("%d",&a[i]);
small=a[0];
large=a[0];
for(i=0;i<n;i++)// read the elements of an array
{
if(a[i]<small)// check the condition for minimum value
small=a[i];
if(a[i]>large)//check the condition for maximum value
large=a[i];
}
printf("largest value is:%d\n",large);
printf("smallest value is:%d\n",small);
getch();
}
16 Write a C Program to Sort the Array in an Ascending Order.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are
given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
getch();
}
17 Write a C program to perform addition of two matrices..
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2];
int i,j;
clrscr();
printf("ENTER A MATRIX\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("ENTER B MATRIX\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf(" After addition of two matrices :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
18 Write a C program that uses functions to perform Multiplication of Two
Matrices
#include<stdio.h >
#include<conio.h>
int i,j,k;
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q;
void mul(int x[10][10],int y[10][10],int z[10][10],int
m,int n,int p,int q);
void read(int x[10][10],int m,int n);
void display(int x[10][10], int m,int n);
clrscr();
printf("Enter the size of A Mtrix (Row and Col): \n");
scanf("%d%d",&m,&n);
printf("Enter the size of B Mtrix (Row and Col): \n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Multiplication Not Possible\n Please re-enter\
n");
printf("correct size and try again .....\n");
}
else
{
read(a,m,n);
read(b,m,n);
mul(a,b,c,m,n,p,q);
printf("A Matrix is :\n");
display(a,m,n);
printf("B Matrix is :\n");
display(b,p,q);
printf("C Matrix is :\n");
display(c,m,q);
}
getch();
}
void mul(int x[10][10],int y[10][10],int z[10][10],int
m,int n,int p,int q)
{
for (i=0;i<m;i++)
for(j=0;j<q;j++)
{
z[i][j]=0;
for(k=0;k<n;k++)
z[i][j]+= x[i][k]*y[k][j];
}
}
void read(int x[10][10], int m,int n)
{
printf("Enter Matrix Value Row by Row\n");
for (i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
}
void display(int x[10][10], int m,int n)
{
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",x[i][j]);
printf("\n");
}
printf("\n");
}
19 Write C program to count the number of lines, words and characters in a
given text.
#include<stdio.h>
#include<conio.h>
void main()
{
char line[81], ctr;
int i,c,end = 0,characters = 0,words = 0,lines = 0;
printf("TYPE ANY TEXT.\n");
printf("GIVE ONE SPACE AFTER EACH WORD.\n");
while( end == 0)
{
c = 0;
while((ctr=getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
if(line[0] == '\0')
break ;
else
{
words++;
for(i=0; line[i] != '\0';i++)
if(line[i] == ' ' || line[i] == '\t')
words++;
}
lines = lines +1;
characters = characters + strlen(line);
}
printf ("\n");
printf("Number of lines = %d\n", lines);
printf("Number of words = %d\n", words);
printf("Number of characters = %d\n", characters);
getch();
}
20 Write a C program to find the length of the string using Pointer.
#include<stdio.h>
#include<conio.h>
int string_ln(char*);
void main()
{
char str[20];
int length;
clrscr();
printf("\nEnter any string : ");
gets(str);
length = string_ln(str);
printf("The length of the given string %s is : %d",
str, length);
getch();
}
int string_ln(char*p)
{
int count = 0;
while (*p != '\0')
{
count++;
p++;
}
return count;
}
21 Write a C program for student mark sheet generation using structure.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int regno;
int sub1,sub2,sub3,sub4,sub5;
}
s[20];
void main()
{
int i,n,total[30];
int rno=10;
clrscr();
printf("\t\t\tSTUDENT MARKSHEET\n");
printf("\t\t\t****************\n");
printf("Enter the number of students:");
scanf("%d",&n);for(i=1;i<=n;i++)
{
printf("\nENTER DETAILS OF STUDENT %d ",i);
rno=rno+1;
s[i].regno=rno;
printf("\nEnter the name:");
scanf("%s",s[i].name);
printf("\nEnter the mark of subject1:");
scanf("%d",&s[i].sub1);
printf("\nEnter the mark of subject2:");
scanf("%d",&s[i].sub2);
printf("\nEnter the mark of subject3:");
scanf("%d",&s[i].sub3);
printf("\nEnter the mark of subject4:");
scanf("%d",&s[i].sub4);
printf("\nEnter the mark of subject5:");
scanf("%d",&s[i].sub5);
total[i]=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].su
b5;
}
printf("\nDETAILS OF STUDENTS ");
printf("\n REG NO\t NAME\t SUB1\tSUB2\tSUB3\tSUB4\tSUB5\
tTOTAL\n");
for(i=1;i<=n;i++){
printf(" %d\t",s[i].regno);
printf("%s\t",s[i].name);
printf("%d\t%d\t%d\t%d\t%d\t%d\
n",s[i].sub1,s[i].sub2,s[i].sub3,s[i].sub4,s[i].sub5,tot
al[i]);
}
getch();
}
22.WAP in C to find the sum of n elements entered by user, to perform this
program, allocate memory dynamically using calloc( ) function.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i, n;
int *a;
printf("Number of elements to be entered:");
scanf("%d",&n);
a = (int*)calloc(n, sizeof(int));
printf("Enter %d numbers:\n",n);
for( i=0 ; i < n ; i++ )
{
scanf("%d",&a[i]);
}
printf("The numbers entered are: ");
for( i=0 ; i < n ; i++ )
{
printf("%d ",a[i]);
}
getch();
}
23 Write a C program to display the contents of a file

#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fs;
char ch;
char *fname;
printf("Enter the file name :");
gets(fname);
fs = fopen("c:\\users\\viraj\\desktop\Y.txt","r");
if(fs==NULL)
{
puts("Source file cannot be opened.");
getch();
}
else
{
while((ch=fgetc(fs))!=EOF)
{
putchar(ch);
}
}
getch();
}
24 Write a C program to copy the contents of one file to another.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <process.h>
void main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", "c:\\users\\viraj\\desktop\\Y.txt");
fptr1 = fopen("c:\\users\\viraj\\desktop\\Y.txt", "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("c:\\users\\viraj\\desktop\\z.txt", "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();
}
25 Write a C program to merge two files into a third file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2,*fp3;
char file1[20],file2[20],file3[20],ch;
puts("Program to merge two files....\n");
puts("Enter first file name:");
gets("c:\\users\\viraj\\desktop\\x.txt");
puts("Enter Second file name:");
gets("c:\\users\\viraj\\desktop\\y.txt");
puts("Enter Destination file name:");
gets("c:\\users\\viraj\\desktop\\z.txt");
fp1=fopen("c:\\users\\viraj\\desktop\\x.txt","r");
fp2=fopen("c:\\users\\viraj\\desktop\\y.txt","r");
fp3=fopen("c:\\users\\viraj\\desktop\\z.txt","w");
if(fp1==NULL&&fp2==NULL)
printf("Error opening file1 and file2.....\n");
else
{
if(fp3==NULL)
printf("Error in creating destination file....\n");
else
{
while((ch=fgetc(fp1))!=EOF)
putc(ch,fp3);
while((ch=fgetc(fp2))!=EOF)
putc(ch,fp3);
}
printf("File Merging Sucessfull....");
fcloseall();
}
getch();
}
26 Write a C program to reverse the first n characters in a file.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
void main(int argc, char *argv[])
{
FILE *fs, *fd;
char s[20], d[20];
int c = 0, count = 0, n;
clrscr();
strcpy("c:\\users\\viraj\\desktop\\y.txt", argv[1]);
n = atoi(argv[2]);
fs = fopen("c:\\users\\viraj\\desktop\\y.txt", "r");
if(s == NULL)
printf("\n FILE ERROR");
fclose(fs);
fs = fopen("c:\\users\\viraj\\desktop\\y.txt", "r+");
count = 0;
while(count < n)
{
d[count] = fgetc(fs);
count++;
}
d[count] = '\0';
fseek(fs, 0L, 0);
fputs(strrev(d), fs);
fclose(fs);
fs = fopen("c:\\users\\viraj\\desktop\\y.txt","r");
while(!feof(fs))
{
printf("%c", fgetc(fs));
c++;
}
fclose(fs);
getch();
}
27 WAP in C to draw basic graphics construction like line, circle, arc, ellipse
and rectangle
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph (&gd,&gm,"c:\\turboc3\\bgi");
setbkcolor(GREEN);
printf("\t\t\t\n\nLINE");
line(50,40,190,40);
printf("\t\t\n\n\n\nRECTANGLE");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(120,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30);
printf("\t\n\n\n\nECLIPSE");
ellipse(120,350,0,360,30,20);
getch();
}

You might also like