C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 1
1(A)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
printf("enter the a and b values");
scanf("%d%d",&a,&b);
temp=a;
a=b;
b=temp;
printf("after swapping the a and b values are %d and %d",a,b);
getch();
}
1(B)
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, num, isprime;
printf("enter any number to print prime");
scanf("%d",&num);
printf("all prime factors of %d are:",num);
for(i=2;i<=num;i++)
{
if(num%i==0)
{
isprime=1;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
isprime=0;
break;
}
}
if(isprime==1)
{
printf("%d",i);
}
}
}
getch();
}
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 2
1(C)
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
float x, y, pi=3.14;
printf("enter cosine value");
scanf("%d", &n);
x=(n*pi)/180;
y=cos(x);
printf("the value of cos is %f is %f",x,y);
getch();
}
2(A)
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum=0,d;
clrscr();
printf("Enter any integer:");
scanf("%d", &n);
while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
printf("sum of individual digits is %d",sum);
getch();
}
2(B)
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf("Enter no. of terms:");
scanf("%d", &n);
printf("The Fibonacci sequence is:");
printf("%d%d", a,b);
for(i=3;i<=n;i++)
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 3
{
c=a+b;
printf("%d",c);
a=b;
b=c;
}
getch();
}
2(C)
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n, count=0;
clrscr();
printf("Enter the limit:");
scanf("%d", &n);
printf("The prime numbers are:");
for(i=2;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%dt", i);
}
getch();
}
3(A)
#include<stdio.h>
#include<conio.h>
void main()
{
float fh, cl;
int choice;
printf("n1. convert temperature from fahrenheit to celcius");
printf("n2. convert temperature from celsius to fahrenheit");
printf("n enter your choice");
scanf("%d",&choice);
if(choice==1)
{
printf("n enter temperature in fahrenheit");
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 4
scanf("%f",&fh);
cl=(fh-32)/1.8;
printf("temperature in celsius is %2f", cl);
}
else if(choice==2)
{
printf("n enter temperature in celsius");
scanf("%f",&cl);
fh=(cl*1.8)+32;
printf("temperature in fahrenheit is %2f",fh);
}
else
{
printf("n invalid choice");
}
getch();
}
3(B)
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
printf("enter the pyramid range");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=0;j<n-i;j++)
{
printf(" ");
}
for(j=0;j<i;j++)
{
printf("%4d",i);
}
printf("n");
}
getch();
}
3(C)
#include<stdio.h>
#include<conio.h>
void main()
{
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 5
int i, j, s, r, c=1;
printf("enter the number of rows");
scanf("%d",&r);
for(i=0;i<r;i++)
{
for(s=1;s<=r-i;s++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
if(j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("%3d",c);
}
printf("n");
}
getch();
}
4(A)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n=4,x;
long int fact=1;
float sum=1;
printf("enter the x value:");
scanf("%d", &x);
for(i=1;i<=n;i++)
{
fact=fact*i;
if(i%2==0)
{
if(i==2||i==10||i==6)
sum+=-pow(x,i)/fact;
else
sum+=pow(x,i)/fact;
}
}
printf("sum is %f", sum);
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 6
getch();
}
4(B)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a, b, c;
char ch;
printf("enter your operator (+, -, *, %, /)n");
scanf("%c",&ch);
printf("enter the values of a, b");
scanf("%d%d",&a,&b);
switch(ch)
{
case '+':c=a+b;
printf("addition of two numbers is %d",c);
break;
case '-':c=a-b;
printf("subtraction of two numbers is %d",c);
break;
case '*':c=a*b;
printf("multiplication of two numbers is %d",c);
break;
case '%':c=a%b;
printf("remainder of a and b is %d",c);
break;
case '/':if(b==0)
printf("divide by zero errorn");
else
{
c=a/b;
printf("quotient of a and b is %d",c);
}
break;
default:
printf("invalid operator");
break;
}
getch();
}
5(A)(i)
#include<stdio.h>
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 7
#include<conio.h>
unsigned int factorial(int n);
void main()
{
int n,i;
long int fact;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
if(n==0)
printf("Factorial of 0 is 1n");
else
printf("Factorial of %d Using Recursive Function is %dn",n,factorial(n));
getch();
}
/* Recursive Function*/
unsigned int factorial(int n)
{
return n>=1 ? n * factorial(n-1) : 1;
}
5(A)(ii)
#include<stdio.h>
#include<conio.h>
#include<math.h>
unsigned int GCDRecursive(unsigned m, unsigned n);
int main(void)
{
int a,b;
clrscr();
printf("Enter the two numbers whose GCD is to be found: ");
scanf("%d%d",&a,&b);
printf("GCD of %d and %d Using Recursive Function is %dn",a,b,GCDRecursive(a,b));
getch();
}
/* Recursive Function*/
unsigned int GCDRecursive(unsigned m, unsigned n)
{
if(n>m)
return GCDRecursive(n,m);
if(n==0)
return m;
else
return GCDRecursive(n,m%n);
}
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 8
5(B)
#include<conio.h>
#include<stdio.h>
/* Recursive Function*/
void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
{
if ( num == 1 ) {
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
return;
}
hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
}
void main()
{
int no;
clrscr();
printf("Enter the no. of disks to be transferred: ");
scanf("%d",&no);
if(no<1)
printf("nThere's nothing to move.");
else
printf("nRecursive");
hanoiRecursion(no,'A','B','C');
getch();
}
6(A)
#include <stdio.h>
#include <conio.h>
/* Main function is starting */
main()
{
/* Variable Declaration part */
int i,n,small=0,large=0;
int a[30];
/* Clearing previous output on output screen */
clrscr();
/* Giving Upper bound + 1 of the array */
printf("n Enter size of the array:");
scanf("%d",&n);
/* Array Initialization using scanf function and for loop */
printf("n Enter values in array elements:");
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 9
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
/* logic to find smallest element in array */
small = a[0];
for(i=0;i<n;i++)
{
if(small > a[i])
small = a[i];
}
printf("n The smallest element in given array is %d",small);
/* logic to find largest element in array */
large=0;
for(i=0;i<n;i++)
{
if(large < a[i])
large = a[i];
}
printf("n The largest element in given array is %d",large);
printf("n :End of the Main Program:");
getch();
}
6(B)(i)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int a[10][10],b[10][10],d[10][10],n,i,j,r,c;
printf("enter no. of rows");
scanf("%d",&r);
printf("enter no. of col");
scanf("%d",&c);
printf("enter 1st matrix ele");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter 2nd matrix ele");
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 10
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("resultant matrix is");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=a[i][j]+b[i][j];
printf("%d",d[i][j]);
}
printf("n");
}
getch();
}
6(B)(ii)
#include <conio.h>
#include <stdio.h>
/* declaring function prototypes */
void init_mat (int [][10], int, int);
void print_mat (int [][10], int, int);
void add_mat (int [][10], int [][10], int [][10], int, int);
/* Main Function starting */
main()
{
int r1,r2,c1,c2;
int a[10][10],b[10][10],c[10][10];
clrscr();
/* Giving order of the Matrix - A */
printf("n Enter the order of Matrix – A:");
scanf("%d%d",&r1,&c1);
/* Giving order of the Matrix - B */
printf("n Enter the order of Matrix – B:");
scanf("%d%d",&r2,&c2);
if(r1!=r2 || c1!=c2)
{
printf("n Matrix Addition is not possible ");
getch();
exit(0);
}
else
{
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 11
/* Matrix - A */
printf("n Enter the elements of Matrix – A:");
init_mat(a,r1,c1);
printf("n The elements of Matrix - A");
print_mat(a,r1,c1);
/* Matrix - B */
printf("n Enter the elements of Matrix - B");
init_mat(b,r2,c2);
printf("n The elements of Matrix - B");
print_mat(b,r2,c2);
/* Function call to Matrix addition logic */
add_mat(a,b,c,r1,c1);
/* Matrix after addition */
printf("n The elements of Matrix - C after addition of A & B");
print_mat(c,r1,c1);
}
getch();
}
/* Function for two dimensional array initialization */
void init_mat(int mat[][10],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&mat[i][j]);
}
}
}
/* Function for printing element in Matrix form */
void print_mat(int mat[][10],int r, int c)
{
int i,j;
printf("n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(" %d ",mat[i][j]);
}
printf("n");
}
}
/* function for matrix addition logic */
void add_mat(int a[][10],int b[][10],int c[][10],int r1,int c1)
{
int i,j;
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 12
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j] = a[i][j]+b[i][j];
}
}
}
7(A)
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char str[32],strdp[32];
int mask,i;
clrscr();
printf("Enter a binary number:");
scanf("%s",str);
strcpy(strdp,str); /* creating duplicate copy */
for(i=0;i<strlen(str);i++) /* computing 1's complement */
{
if(str[i]=='1')
str[i]='0';
else
str[i]='1';
}
printf("1's complement of %s is %sn",strdp,str);
mask=1;
for(i=strlen(str)-1;i>=0;i--) /* computing 2's complement */
{
if(mask==1)
{
if(str[i]=='1')
{
str[i]='0';
mask=1;
}
else
{
str[i]='1';
mask=0;
}
}
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 13
}
printf("2's complement of %s is %s",strdp,str);
getch();
}
7(B)
#include <stdio.h>
#include <conio.h>
main()
{
char roman[30];
int deci=0;
int len,i,d[30];
clrscr();
printf("Enter a Roman numeral:");
scanf("%s",roman);
len=strlen(roman);
for(i=0;i<len;i++)
{
switch(roman[i])
{
case 'M': d[i]=1000; break;
case 'D': d[i]= 500; break;
case 'C': d[i]= 100; break;
case 'L': d[i]= 50; break;
case 'X': d[i]= 10; break;;
case 'V': d[i]= 5; break;
case 'I': d[i]= 1;
}
}
for(i=0;i<len;i++)
{
if(i==len-1 || d[i]>=d[i+1])
deci += d[i];
else
deci -= d[i];
}
printf("The Decimal equivalent of Roman numeral %s is %d", roman, deci);
getch();
}
8(A)(i)
#include <stdio.h>
#include <conio.h>
/* Declaring function prototypes */
void ins_substr(char [], char [], int, int);
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 14
/* main function is starting */
main()
{
int p,n,i,j;
char str[50],substr[50];
clrscr();
/* Initializing character array */
puts("n Enter the String:");
gets(str);
fflush(stdin);
/* Entering the position where you want to insert a substrprintf("Enter the specific position
");
scanf("%d",&p);
printf("n Enter the Number of Characters:");
scanf("%d",&n);
fflush(stdin);
puts("n Enter Sub-String:");
gets(substr);
/* function call to inserting string in main string */
ins_substr(str,substr,p,n);
printf("n :: End of the main program ::");
getch();
}
/* logic to insert sub string in main string */
void ins_substr(char str[], char substr[], int p, int n)
{
int q,i,j;
q=p-1;
for(i=q,j=n;str[i]!='0';i++,j++)
substr[j]=str[i];
substr[j]='0';
for(j=0,i=q;substr[j]!='0';j++,i++)
str[i]=substr[j];
str[i]='0';
printf("n The string after inserting substring :");
puts(str);
}
8(A)(ii)
#include <stdio.h>
#include <conio.h>
/* declaring prototype of function */
void del_str(char [],int, int);
/* Main function starting */
main()
{
int n,p;
char str[30];
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 15
clrscr();
printf("n Enter the String::");
gets(str);
fflush(stdin);
printf("n Enter the position from where the characters are to be deleted:");
scanf("%d",&p);
printf("n Enter Number of characters to be deleted:");
scanf("%d",&n);
/* function call to deletion of n-characters */
del_str(str,p,n);
printf("::End of the Main program::");
getch();
}
/* function call to Logic of delete n-characters from string */
void del_str(char str[],int p, int n)
{
int i,j;
for(i=0,j=0;str[i]!='0';i++,j++)
{
if(i==(p-1))
{
i=i+n;
}
str[j]=str[i];
}
str[j]='0';
/* the string after deletion */
puts(" The string after deletion of characters::");
puts(str);
}
8(B)
#include <stdio.h>
#include <conio.h>
/* Main function definition */
main()
{
int i,n,j,len=0;
char str[30];
clrscr();
printf("n Enter String:");
gets(str);
/* logic to checking string for palindrome */
for(i=0;str[i]!='0';i++)
len++;
printf("n The length of the string is %d",len);
for(i=0,j=len-1;str[i]!='0';i++,j--)
{
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 16
if(str[i]!=str[j])
{
printf("n :The given string is not a palindrome:");
getch();
exit(0);
}
}
printf("n :the given string is palindrome:");
getch();
}
9(A)
#include <stdio.h>
#include <conio.h>
#include <string.h>
/* main function is starting */
main()
{
int i,j,n;
char s[40],t[40];
clrscr();
printf("n Enter the string:");
gets(s);
fflush(stdin);
printf("n Enter the sub string:");
gets(t);
/* logic to search sub string */
i=0;
j=0;
while( s[i] != '0')
{
if((s[i] == t[j]) && (s[i+1] == t[j+1]))
break;
if(i==strlen(s))
{
printf("-1");
getch();
exit(0);
}
i++;
}
j=0;
n=i;
while(t[j] != '0')
{
if(s[i] == t[j])
{
i++;
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 17
j++;
}
else
{
printf("-1");
getch();
exit(0);
}
}
printf("n The string is found at %d",n+1);
getch();
}
9(B)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[100];
int i,l=0,c=0,w=0;
clrscr();
puts("enter a string");
scanf("%[^#]s",s);
for(i=0;s[i]!='0';i++)
{
c=c+1;
if(s[i]==' '||s[i]=='n')
w=w+1;
if(s[i]=='n')
l=l+1;
}
printf("the number of characters in a given text is %dn",c);
printf("the number of words in a given text is %dn",w+1);
printf("the number of lines in a given text is %dn",l+1);
getch();
}
10
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct comp
{
int real,img;
};
struct comp a,b,c;
void read();
void write();
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 18
void add();
void mul();
void main()
{
int ch;
clrscr();
printf("enter choicen1.addition of two complex numbersn2. multiplication of two complex
numbersn");
scanf("%d",&ch);
switch(ch)
{
case 1:
read();
add();
write();
break;
case 2:
read();
mul();
write();
break;
default:
printf("invalid choice");
}
getch();
}
void read()
{
printf("enter real and imaginary part of first complex number");
scanf("%d%d",&a.real,&a.img);
printf("enter real and imaginary part of second complex number");
scanf("%d%d",&b.real,&b.img);
}
void write()
{
printf("the resultant complex number is %d+%di",c.real,c.img);
}
void add()
{
c.real=a.real+b.real;
c.img=a.img+b.img;
}
void mul() //(a+bi)(c+di)=(ac-bd)+(ad+bc)i//
{
c.real=a.real*b.real-a.img*b.img;
c.img=a.real*b.img+a.img*b.real;
}
11(A)
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 19
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen("8a.c","r");
if(fp==NULL)
printf("error reading filen");
else
while((ch=fgetc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
getch();
}
11(B)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *fptr1,*fptr2;
char file1[100],c;
printf("enter the file name to open for readingn");
scanf("%s",file1);
fptr1=fopen(file1,"r");
if(fptr1==NULL)
{
printf("cannot open file%s",file1);
exit(0);
}
printf("enter the filename to open for writingn");
scanf("%s",file1);
fptr2=fopen(file1,"w");
if(fptr2==NULL)
{
printf("cannot open file %sn",file1);
exit(0);
}
c=fgetc(fptr1);
while(c!=EOF)
{
fputc(c,fptr2);
c=fgetc(fptr1);
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 20
}
printf("contents copied to %s",file1);
fclose(fptr1);
fclose(fptr2);
return 0;
}
12(A)
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
void main(int argc, char *argv[])
{
char a[15];
char s[20];
char n;
int k;
int j=0;
int i;
int len;
FILE *fp;
if(argc!=3)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}
k=atoi(argv[2]);
n = fread(a,1,k,fp);
a[n]='0';
len=strlen(a);
for(i=len-1;i>=0;i--)
{
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
}
s[j+1]='0';
getch();
}
12(B)
#include <stdio.h>
C Programming Laboratory
Vikram Neerugatti, Assistant Professor, SVCET, Chittoor Page 21
#include <stdlib.h>
int main()
{
FILE *fs1,*fs2,*ft;
char ch, file1[20], file2[20], file3[20];
printf("Enter name of first filen");
gets(file1);
printf("Enter name of second filen");
gets(file2);
printf("Enter name of file which will store contents of two filesn");
gets(file3);
fs1 =fopen(file1,"r");
fs2 =fopen(file2,"r");
if( fs1 == NULL || fs2 == NULL )
{
perror("Error ");
printf("Press any key to exit...n");
getch();
exit(EXIT_FAILURE);
}
ft =fopen(file3,"w");
if( ft == NULL )
{
perror("Error ");
printf("Press any key to exit...n");
exit(EXIT_FAILURE);
}
while(( ch =fgetc(fs1))!= EOF )
fputc(ch,ft);
while(( ch =fgetc(fs2))!= EOF )
fputc(ch,ft);
printf("Two files were merged into %s file successfully.n",file3);
fclose(fs1);
fclose(fs2);
fclose(ft);
return 0;
}

C Programming lab

  • 1.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 1 1(A) #include<stdio.h> #include<conio.h> void main() { int a,b,temp; printf("enter the a and b values"); scanf("%d%d",&a,&b); temp=a; a=b; b=temp; printf("after swapping the a and b values are %d and %d",a,b); getch(); } 1(B) #include<stdio.h> #include<conio.h> void main() { int i, j, num, isprime; printf("enter any number to print prime"); scanf("%d",&num); printf("all prime factors of %d are:",num); for(i=2;i<=num;i++) { if(num%i==0) { isprime=1; for(j=2;j<=i/2;j++) { if(i%j==0) { isprime=0; break; } } if(isprime==1) { printf("%d",i); } } } getch(); }
  • 2.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 2 1(C) #include<stdio.h> #include<conio.h> void main() { int n; float x, y, pi=3.14; printf("enter cosine value"); scanf("%d", &n); x=(n*pi)/180; y=cos(x); printf("the value of cos is %f is %f",x,y); getch(); } 2(A) #include<stdio.h> #include<conio.h> void main() { int n, sum=0,d; clrscr(); printf("Enter any integer:"); scanf("%d", &n); while(n>0) { d=n%10; sum=sum+d; n=n/10; } printf("sum of individual digits is %d",sum); getch(); } 2(B) #include<stdio.h> #include<conio.h> void main() { int a=0,b=1,c,n,i; clrscr(); printf("Enter no. of terms:"); scanf("%d", &n); printf("The Fibonacci sequence is:"); printf("%d%d", a,b); for(i=3;i<=n;i++)
  • 3.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 3 { c=a+b; printf("%d",c); a=b; b=c; } getch(); } 2(C) #include<stdio.h> #include<conio.h> void main() { int i, j, n, count=0; clrscr(); printf("Enter the limit:"); scanf("%d", &n); printf("The prime numbers are:"); for(i=2;i<=n;i++) { for(j=1;j<=i;j++) { if(i%j==0) count++; } if(count==2) printf("%dt", i); } getch(); } 3(A) #include<stdio.h> #include<conio.h> void main() { float fh, cl; int choice; printf("n1. convert temperature from fahrenheit to celcius"); printf("n2. convert temperature from celsius to fahrenheit"); printf("n enter your choice"); scanf("%d",&choice); if(choice==1) { printf("n enter temperature in fahrenheit");
  • 4.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 4 scanf("%f",&fh); cl=(fh-32)/1.8; printf("temperature in celsius is %2f", cl); } else if(choice==2) { printf("n enter temperature in celsius"); scanf("%f",&cl); fh=(cl*1.8)+32; printf("temperature in fahrenheit is %2f",fh); } else { printf("n invalid choice"); } getch(); } 3(B) #include<stdio.h> #include<conio.h> void main() { int i, j, n; printf("enter the pyramid range"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=0;j<n-i;j++) { printf(" "); } for(j=0;j<i;j++) { printf("%4d",i); } printf("n"); } getch(); } 3(C) #include<stdio.h> #include<conio.h> void main() {
  • 5.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 5 int i, j, s, r, c=1; printf("enter the number of rows"); scanf("%d",&r); for(i=0;i<r;i++) { for(s=1;s<=r-i;s++) { printf(" "); } for(j=0;j<=i;j++) { if(j==0||i==0) c=1; else c=c*(i-j+1)/j; printf("%3d",c); } printf("n"); } getch(); } 4(A) #include<stdio.h> #include<conio.h> #include<math.h> void main() { int i,n=4,x; long int fact=1; float sum=1; printf("enter the x value:"); scanf("%d", &x); for(i=1;i<=n;i++) { fact=fact*i; if(i%2==0) { if(i==2||i==10||i==6) sum+=-pow(x,i)/fact; else sum+=pow(x,i)/fact; } } printf("sum is %f", sum);
  • 6.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 6 getch(); } 4(B) #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a, b, c; char ch; printf("enter your operator (+, -, *, %, /)n"); scanf("%c",&ch); printf("enter the values of a, b"); scanf("%d%d",&a,&b); switch(ch) { case '+':c=a+b; printf("addition of two numbers is %d",c); break; case '-':c=a-b; printf("subtraction of two numbers is %d",c); break; case '*':c=a*b; printf("multiplication of two numbers is %d",c); break; case '%':c=a%b; printf("remainder of a and b is %d",c); break; case '/':if(b==0) printf("divide by zero errorn"); else { c=a/b; printf("quotient of a and b is %d",c); } break; default: printf("invalid operator"); break; } getch(); } 5(A)(i) #include<stdio.h>
  • 7.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 7 #include<conio.h> unsigned int factorial(int n); void main() { int n,i; long int fact; clrscr(); printf("Enter the number: "); scanf("%d",&n); if(n==0) printf("Factorial of 0 is 1n"); else printf("Factorial of %d Using Recursive Function is %dn",n,factorial(n)); getch(); } /* Recursive Function*/ unsigned int factorial(int n) { return n>=1 ? n * factorial(n-1) : 1; } 5(A)(ii) #include<stdio.h> #include<conio.h> #include<math.h> unsigned int GCDRecursive(unsigned m, unsigned n); int main(void) { int a,b; clrscr(); printf("Enter the two numbers whose GCD is to be found: "); scanf("%d%d",&a,&b); printf("GCD of %d and %d Using Recursive Function is %dn",a,b,GCDRecursive(a,b)); getch(); } /* Recursive Function*/ unsigned int GCDRecursive(unsigned m, unsigned n) { if(n>m) return GCDRecursive(n,m); if(n==0) return m; else return GCDRecursive(n,m%n); }
  • 8.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 8 5(B) #include<conio.h> #include<stdio.h> /* Recursive Function*/ void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3) { if ( num == 1 ) { printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 ); return; } hanoiRecursion( num - 1,ndl1, ndl3, ndl2 ); printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 ); hanoiRecursion( num - 1,ndl3, ndl2, ndl1 ); } void main() { int no; clrscr(); printf("Enter the no. of disks to be transferred: "); scanf("%d",&no); if(no<1) printf("nThere's nothing to move."); else printf("nRecursive"); hanoiRecursion(no,'A','B','C'); getch(); } 6(A) #include <stdio.h> #include <conio.h> /* Main function is starting */ main() { /* Variable Declaration part */ int i,n,small=0,large=0; int a[30]; /* Clearing previous output on output screen */ clrscr(); /* Giving Upper bound + 1 of the array */ printf("n Enter size of the array:"); scanf("%d",&n); /* Array Initialization using scanf function and for loop */ printf("n Enter values in array elements:");
  • 9.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 9 for(i=0;i<n;i++) { scanf("%d",&a[i]); } /* logic to find smallest element in array */ small = a[0]; for(i=0;i<n;i++) { if(small > a[i]) small = a[i]; } printf("n The smallest element in given array is %d",small); /* logic to find largest element in array */ large=0; for(i=0;i<n;i++) { if(large < a[i]) large = a[i]; } printf("n The largest element in given array is %d",large); printf("n :End of the Main Program:"); getch(); } 6(B)(i) #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> void main() { int a[10][10],b[10][10],d[10][10],n,i,j,r,c; printf("enter no. of rows"); scanf("%d",&r); printf("enter no. of col"); scanf("%d",&c); printf("enter 1st matrix ele"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&a[i][j]); } } printf("enter 2nd matrix ele");
  • 10.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 10 for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&b[i][j]); } } printf("resultant matrix is"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { d[i][j]=a[i][j]+b[i][j]; printf("%d",d[i][j]); } printf("n"); } getch(); } 6(B)(ii) #include <conio.h> #include <stdio.h> /* declaring function prototypes */ void init_mat (int [][10], int, int); void print_mat (int [][10], int, int); void add_mat (int [][10], int [][10], int [][10], int, int); /* Main Function starting */ main() { int r1,r2,c1,c2; int a[10][10],b[10][10],c[10][10]; clrscr(); /* Giving order of the Matrix - A */ printf("n Enter the order of Matrix – A:"); scanf("%d%d",&r1,&c1); /* Giving order of the Matrix - B */ printf("n Enter the order of Matrix – B:"); scanf("%d%d",&r2,&c2); if(r1!=r2 || c1!=c2) { printf("n Matrix Addition is not possible "); getch(); exit(0); } else {
  • 11.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 11 /* Matrix - A */ printf("n Enter the elements of Matrix – A:"); init_mat(a,r1,c1); printf("n The elements of Matrix - A"); print_mat(a,r1,c1); /* Matrix - B */ printf("n Enter the elements of Matrix - B"); init_mat(b,r2,c2); printf("n The elements of Matrix - B"); print_mat(b,r2,c2); /* Function call to Matrix addition logic */ add_mat(a,b,c,r1,c1); /* Matrix after addition */ printf("n The elements of Matrix - C after addition of A & B"); print_mat(c,r1,c1); } getch(); } /* Function for two dimensional array initialization */ void init_mat(int mat[][10],int r,int c) { int i,j; for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&mat[i][j]); } } } /* Function for printing element in Matrix form */ void print_mat(int mat[][10],int r, int c) { int i,j; printf("n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf(" %d ",mat[i][j]); } printf("n"); } } /* function for matrix addition logic */ void add_mat(int a[][10],int b[][10],int c[][10],int r1,int c1) { int i,j;
  • 12.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 12 for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { c[i][j] = a[i][j]+b[i][j]; } } } 7(A) #include <stdio.h> #include <conio.h> #include <string.h> main() { char str[32],strdp[32]; int mask,i; clrscr(); printf("Enter a binary number:"); scanf("%s",str); strcpy(strdp,str); /* creating duplicate copy */ for(i=0;i<strlen(str);i++) /* computing 1's complement */ { if(str[i]=='1') str[i]='0'; else str[i]='1'; } printf("1's complement of %s is %sn",strdp,str); mask=1; for(i=strlen(str)-1;i>=0;i--) /* computing 2's complement */ { if(mask==1) { if(str[i]=='1') { str[i]='0'; mask=1; } else { str[i]='1'; mask=0; } }
  • 13.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 13 } printf("2's complement of %s is %s",strdp,str); getch(); } 7(B) #include <stdio.h> #include <conio.h> main() { char roman[30]; int deci=0; int len,i,d[30]; clrscr(); printf("Enter a Roman numeral:"); scanf("%s",roman); len=strlen(roman); for(i=0;i<len;i++) { switch(roman[i]) { case 'M': d[i]=1000; break; case 'D': d[i]= 500; break; case 'C': d[i]= 100; break; case 'L': d[i]= 50; break; case 'X': d[i]= 10; break;; case 'V': d[i]= 5; break; case 'I': d[i]= 1; } } for(i=0;i<len;i++) { if(i==len-1 || d[i]>=d[i+1]) deci += d[i]; else deci -= d[i]; } printf("The Decimal equivalent of Roman numeral %s is %d", roman, deci); getch(); } 8(A)(i) #include <stdio.h> #include <conio.h> /* Declaring function prototypes */ void ins_substr(char [], char [], int, int);
  • 14.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 14 /* main function is starting */ main() { int p,n,i,j; char str[50],substr[50]; clrscr(); /* Initializing character array */ puts("n Enter the String:"); gets(str); fflush(stdin); /* Entering the position where you want to insert a substrprintf("Enter the specific position "); scanf("%d",&p); printf("n Enter the Number of Characters:"); scanf("%d",&n); fflush(stdin); puts("n Enter Sub-String:"); gets(substr); /* function call to inserting string in main string */ ins_substr(str,substr,p,n); printf("n :: End of the main program ::"); getch(); } /* logic to insert sub string in main string */ void ins_substr(char str[], char substr[], int p, int n) { int q,i,j; q=p-1; for(i=q,j=n;str[i]!='0';i++,j++) substr[j]=str[i]; substr[j]='0'; for(j=0,i=q;substr[j]!='0';j++,i++) str[i]=substr[j]; str[i]='0'; printf("n The string after inserting substring :"); puts(str); } 8(A)(ii) #include <stdio.h> #include <conio.h> /* declaring prototype of function */ void del_str(char [],int, int); /* Main function starting */ main() { int n,p; char str[30];
  • 15.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 15 clrscr(); printf("n Enter the String::"); gets(str); fflush(stdin); printf("n Enter the position from where the characters are to be deleted:"); scanf("%d",&p); printf("n Enter Number of characters to be deleted:"); scanf("%d",&n); /* function call to deletion of n-characters */ del_str(str,p,n); printf("::End of the Main program::"); getch(); } /* function call to Logic of delete n-characters from string */ void del_str(char str[],int p, int n) { int i,j; for(i=0,j=0;str[i]!='0';i++,j++) { if(i==(p-1)) { i=i+n; } str[j]=str[i]; } str[j]='0'; /* the string after deletion */ puts(" The string after deletion of characters::"); puts(str); } 8(B) #include <stdio.h> #include <conio.h> /* Main function definition */ main() { int i,n,j,len=0; char str[30]; clrscr(); printf("n Enter String:"); gets(str); /* logic to checking string for palindrome */ for(i=0;str[i]!='0';i++) len++; printf("n The length of the string is %d",len); for(i=0,j=len-1;str[i]!='0';i++,j--) {
  • 16.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 16 if(str[i]!=str[j]) { printf("n :The given string is not a palindrome:"); getch(); exit(0); } } printf("n :the given string is palindrome:"); getch(); } 9(A) #include <stdio.h> #include <conio.h> #include <string.h> /* main function is starting */ main() { int i,j,n; char s[40],t[40]; clrscr(); printf("n Enter the string:"); gets(s); fflush(stdin); printf("n Enter the sub string:"); gets(t); /* logic to search sub string */ i=0; j=0; while( s[i] != '0') { if((s[i] == t[j]) && (s[i+1] == t[j+1])) break; if(i==strlen(s)) { printf("-1"); getch(); exit(0); } i++; } j=0; n=i; while(t[j] != '0') { if(s[i] == t[j]) { i++;
  • 17.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 17 j++; } else { printf("-1"); getch(); exit(0); } } printf("n The string is found at %d",n+1); getch(); } 9(B) #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[100]; int i,l=0,c=0,w=0; clrscr(); puts("enter a string"); scanf("%[^#]s",s); for(i=0;s[i]!='0';i++) { c=c+1; if(s[i]==' '||s[i]=='n') w=w+1; if(s[i]=='n') l=l+1; } printf("the number of characters in a given text is %dn",c); printf("the number of words in a given text is %dn",w+1); printf("the number of lines in a given text is %dn",l+1); getch(); } 10 #include<stdio.h> #include<conio.h> #include<math.h> struct comp { int real,img; }; struct comp a,b,c; void read(); void write();
  • 18.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 18 void add(); void mul(); void main() { int ch; clrscr(); printf("enter choicen1.addition of two complex numbersn2. multiplication of two complex numbersn"); scanf("%d",&ch); switch(ch) { case 1: read(); add(); write(); break; case 2: read(); mul(); write(); break; default: printf("invalid choice"); } getch(); } void read() { printf("enter real and imaginary part of first complex number"); scanf("%d%d",&a.real,&a.img); printf("enter real and imaginary part of second complex number"); scanf("%d%d",&b.real,&b.img); } void write() { printf("the resultant complex number is %d+%di",c.real,c.img); } void add() { c.real=a.real+b.real; c.img=a.img+b.img; } void mul() //(a+bi)(c+di)=(ac-bd)+(ad+bc)i// { c.real=a.real*b.real-a.img*b.img; c.img=a.real*b.img+a.img*b.real; } 11(A)
  • 19.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 19 #include<stdio.h> #include<conio.h> void main() { char ch; FILE *fp; fp=fopen("8a.c","r"); if(fp==NULL) printf("error reading filen"); else while((ch=fgetc(fp))!=EOF) printf("%c",ch); fclose(fp); getch(); } 11(B) #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { FILE *fptr1,*fptr2; char file1[100],c; printf("enter the file name to open for readingn"); scanf("%s",file1); fptr1=fopen(file1,"r"); if(fptr1==NULL) { printf("cannot open file%s",file1); exit(0); } printf("enter the filename to open for writingn"); scanf("%s",file1); fptr2=fopen(file1,"w"); if(fptr2==NULL) { printf("cannot open file %sn",file1); exit(0); } c=fgetc(fptr1); while(c!=EOF) { fputc(c,fptr2); c=fgetc(fptr1);
  • 20.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 20 } printf("contents copied to %s",file1); fclose(fptr1); fclose(fptr2); return 0; } 12(A) #include <stdio.h> #include <conio.h> #include <string.h> #include <process.h> void main(int argc, char *argv[]) { char a[15]; char s[20]; char n; int k; int j=0; int i; int len; FILE *fp; if(argc!=3) { puts("Improper number of arguments."); exit(0); } fp = fopen(argv[1],"r"); if(fp == NULL) { puts("File cannot be opened."); exit(0); } k=atoi(argv[2]); n = fread(a,1,k,fp); a[n]='0'; len=strlen(a); for(i=len-1;i>=0;i--) { s[j]=a[i]; printf("%c",s[j]); j=j+1; } s[j+1]='0'; getch(); } 12(B) #include <stdio.h>
  • 21.
    C Programming Laboratory VikramNeerugatti, Assistant Professor, SVCET, Chittoor Page 21 #include <stdlib.h> int main() { FILE *fs1,*fs2,*ft; char ch, file1[20], file2[20], file3[20]; printf("Enter name of first filen"); gets(file1); printf("Enter name of second filen"); gets(file2); printf("Enter name of file which will store contents of two filesn"); gets(file3); fs1 =fopen(file1,"r"); fs2 =fopen(file2,"r"); if( fs1 == NULL || fs2 == NULL ) { perror("Error "); printf("Press any key to exit...n"); getch(); exit(EXIT_FAILURE); } ft =fopen(file3,"w"); if( ft == NULL ) { perror("Error "); printf("Press any key to exit...n"); exit(EXIT_FAILURE); } while(( ch =fgetc(fs1))!= EOF ) fputc(ch,ft); while(( ch =fgetc(fs2))!= EOF ) fputc(ch,ft); printf("Two files were merged into %s file successfully.n",file3); fclose(fs1); fclose(fs2); fclose(ft); return 0; }