c programming lab (jntuk)
TRANSCRIPT
-
I.B.TECH - CSE C Programming Lab P a g e | 1
Program No: 1
Aim: To convert the temperature from Fahrenheit to Celsius.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 2
Source Code:
#include
main( )
{
float c,f;
printf( Enter the temperature in Fahrenheit: ); scanf(%f, &f); c=(f-32)/1.8;
printf(\n The temperature in Celsius is: %f, c); }
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 3
Program No: 2
Aim: To calculate the total marks obtained by a student in four subjects
and verify to which grade the student belongs.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 4
Source Code:
#include
main( )
{
int m1,m2,m3,m4,total,avg;
char grade;
clrscr( );
printf( Enter 4 subjects marks: ); scanf(%d %d %d %d, &m1, &m2, &m3, &m4); total=m1+m2+m3+m4;
avg=total/4;
printf(\n Total marks: %d, total); printf(\n Average marks: %d, avg);
if(avg>=90)
grade=A; else if(avg>=80)
grade=B; else if(avg>=70)
grade=C; else if(avg>=60)
grade=D; else
grade=F; printf(\n The grade = %c, grade); getch( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 5
Program No: 3
Aim: To find the sum of individual digits of a positive integer.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 6
Source Code:
#include
main( )
{
int n, sum;
sum = 0;
printf( Enter a positive integer: ); scanf(%d, &n); while(n>0)
{
sum = sum + n%10;
n = n/10;
}
printf(\n Sum of the digits of the given integer is: %d, sum); getch( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 7
Program No: 4
Aim: To generate the first n terms of the sequence (Fibonacci sequence).
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 8
Source Code:
#include
main( )
{
int a=0,b=1,n,c;
printf("Enter any number: ");
scanf("%d",&n);
printf("\n The Fibonacci numbers are . . . .:");
printf("\n %3d%3d",a,b);
c=a+b;
while(c
-
I.B.TECH - CSE C Programming Lab P a g e | 9
Program No: 5
Aim: To generate all the prime numbers between 1 and n, where n is a
value supplied by the user.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 10
Source Code:
#include
main( )
{
int n,i,t;
printf(Enter a value: ); scanf(%d,&n); printf(\n Prime numbers between 1 to %d are: \n, n); for(i=1;i
-
I.B.TECH - CSE C Programming Lab P a g e | 11
Program No: 6
Aim: To check a given integer is Fibonacci number or not.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 12
Source Code:
#include
#include
#include
main( )
{
int f1=0,f2=1,f3,n,i;
clrscr( );
printf("Enter any number: ");
scanf("%d",&n);
for(i=1;i
-
I.B.TECH - CSE C Programming Lab P a g e | 13
Program No: 7
Aim: To find the roots of a quadratic equation.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 14
Source Code:
#include
#include
#include
main( )
{
float a,b,c,r1,r2;
clrscr( );
printf("Enter the three values:");
scanf("%f%f%f",&a,&b,&c);
if(a!=0)
{
if(b*b>4*a*b*c)
{
r1=-b+sqrt(b*b-4*a*c)/2*a;
r2=-b-sqrt(b*b-4*a*c)/2*a;
printf("\n roots are real");
printf("\n r1=%f\n r2=%f",r1,r2);
}
else
printf("\n roots are imaginary");
}
else
printf("\n roots are linear");
getch( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 15
Program No: 8
Aim: To find the factorial of a given integer.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 16
Source Code:
#include
main( )
{
int n,fact;
printf(Enter a value: ); scanf(%d,&n); fact = n;
while(n>1)
{
fact = fact*(n-1);
n--;
}
printf(\n Factorial of a given number is %d,fact); getch( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 17
Program No: 9
Aim: To find the GCD (greatest common divisor) of two given integers.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 18
Source Code:
#include
main( )
{
int a,b,gcd,temp;
printf(Enter two numbers:); scanf(%d%d,&a,&b); while(1)
{
if(b= =0)
{
gcd = a;
break;
}
temp = a%b;
a = b;
b = temp;
}
printf( \n GCD is: %d,gcd); getch( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 19
Program No: 10
Aim: To find the distance traveled by a vehicle using the formula
S = ut+ at2.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 20
Source Code:
#include
#include
#include
main( )
{
float s,u,t,a;
clrscr( );
printf("\n\n Enter the time interval value in seconds:);
scanf(f,&t);
printf(\n Enter the acceleration value in m/(sec square):);
scanf(%f,&a);
printf(\n Enter the initial velocity value in m/sec:);
scanf(%f,&u);
s=(u*t)+(0.5*a*t*t);
printf(\n The distance traveled in meters is: %f\n,s);
getch( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 21
Program No: 11
Aim: To read two integer operands and one operator from the user and
perform the operation and then print the result using switch statement.
Algorithm:
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 22
Source Code:
#include
main( )
{
int x,y,rst;
char opt;
printf("Enter first number:");
scanf("%d",&x);
printf("\n Enter second number:");
scanf("%d",&y);
getchar( );
printf("\n Enter your option + - X / %% : ");
scanf("%c",&opt);
switch(opt)
{
case '+':
rst=x+y;
break;
case '-':
rst=x-y;
break;
case '*':
rst=x*y;
break;
case '/':
rst=x/y;
break;
case '%':
rst=x%y;
break;
default:
printf("\n No operation");
break;
}
printf( %d %c %d = %d\n,x,opt,y,rst);
getch( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 23
Program No: 12
Aim: To find both the largest and smallest number in a list of integers.
Source Code:
#include
main( )
{
int a[10],small,lar,i;
printf(Enter 10 integers : \n); for(i=0;i
-
I.B.TECH - CSE C Programming Lab P a g e | 24
Program No: 13
Aim: To find the addition of two matrices.
Source Code:
#include
main( )
{
int a[5][5], b[5][5], c[5][5];
int i,j,row,col;
clrscr( );
/* Collecting the number of rows and number of columns */
printf("Enter number of rows: ");
scanf("%d",&row);
printf("\n Enter number of columns: ");
scanf("%d",&col);
/* First Matrix Reading */
for(i=0;i
-
I.B.TECH - CSE C Programming Lab P a g e | 25
/* Printing the resultant matrix */
for(i=0;i
-
I.B.TECH - CSE C Programming Lab P a g e | 26
Program No: 14
Aim: To find the multiplication of two matrices.
Source Code:
#include
#include
main( )
{
int A[3][3], B[3][2], C[3][2], i, j, k;
clrscr();
printf("\n Enter A Matrix values:");
for(i=0;i
-
I.B.TECH - CSE C Programming Lab P a g e | 27
/* Print the resultant C Matrix */
printf( The resultant Matrix is: \n);
for(i=0;i
-
I.B.TECH - CSE C Programming Lab P a g e | 28
Program No: 15
Aim: To insert a sub-string in to given main string from a given position.
Source Code:
#include
#include
main( )
{
char mainstr[100], insstr[30], temp[50];
int l1, l2, l, i, j, pos;
clrscr();
printf("Enter main String:");
gets(mainstr);
l1=strlen(mainstr);
printf("\n Enter the string to be inserted: ");
gets(insstr);
printf("\n Enter the position to be inserted: ");
scanf("%d",&pos);
l2=strlen(insstr);
if(pos>l1)
{
for(i=0;i
-
I.B.TECH - CSE C Programming Lab P a g e | 29
Program No: 16
Aim: To delete n characters from a given position in a given string.
Source Code:
#include
#include
main( )
{
char str[100];
int l, i, j, pos, del;
clrscr( );
printf("Enter a String:");
gets(str);
l=strlen(str);
printf("\n Enter how many characters you want to delete: ");
scanf("%d",&del);
printf("\n Enter the position to delete: ");
scanf("%d",&pos);
if(pos>l)
printf("\n Deletion is not possible");
else
{
j=pos;
for(i=pos+del;i
-
I.B.TECH - CSE C Programming Lab P a g e | 30
Program No: 17
Aim: To determine if the given string is a palindrome or not.
Source Code:
#include
#include
main( )
{
char str[20];
int len,flag=1,i,l;
printf( Enter a string : ); scanf(%s,str); len = strlen(str);
l=len-1;
for(i=0;i
-
I.B.TECH - CSE C Programming Lab P a g e | 31
Program No: 18
Aim: To display the position or index in the string S where the string T
begins, or 1 if S doesnt contain T.
Source Code:
#include
#include
main( )
{
char S[100], T[100], *pos;
int posn;
printf("Enter the main string: ");
scanf("%s",S);
printf("\n Enter the substring:);
scanf(" %s",T);
pos=strstr(S,T);
if(pos)
posn = pos - S + 1;
else
posn = -1;
printf("\n %s found in %s at position %d",T,S,posn);
getch( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 32
Program No: 19
Aim: To count the lines, words and characters in a given text.
Source Code:
#include
main( )
{
char str[80], c;
int i=0, wc=1, lc=1;
printf("Enter a string ending with # : \n");
c=getchar( );
while(c!='#')
{
str[i]=c;
i++;
c=getchar( );
}
str[i]='\0';
i = 0;
while(str[i]!='\0')
{
if(str[i]=='\n')
{
lc++;
wc++;
}
else
{
if(str[i]==' '&&str[i+1]!=' ')
wc++;
}
i++;
}
printf("\n The Character count is: %d",i);
printf("\n The word count is: %d",wc);
printf("\n The line count is: %d",lc);
getch( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 33
Program No: 20
Aim: To find the factorial of a given integer using recursive function.
Source Code:
#include
void main( )
{
int n, rst;
int fact(int);
printf("Enter any number:");
scanf("%d",&n);
rst=fact(n);
printf("\n Factorial of %d is:%d\n",n,rst);
}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 34
Program No: 21
Aim: To find the GCD (greatest common divisor) of two given integers
using recursive function.
Source Code:
#include
void main( )
{
int a, b, rst;
int gcd(int,int);
clrscr( );
printf(" Enter first number:");
scanf("%d",&a);
printf("\n Enter second number:");
scanf("%d",&b);
rst=gcd(a,b);
printf("\n GCD of %d and %d is : %d",a,b,rst);
getch( );
}
int gcd(int x, int y)
{
if(y==0)
return x;
else
return(gcd(y,x%y));
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 35
Program No: 22
Aim: To exchange two integers using passing by address (call by
reference).
Source Code:
#include
#include
main( )
{
int x,y, change( int *, int *);
clrscr( );
printf(\n Enter values of x and y:);
scanf(%d %d, &x,&y);
change(&x,&y);
printf(\n In main( ) x=%d y=%d,x,y);
return 0;
}
change(int *a, int *b)
{
int *k;
*k=*a;
*a=*b;
*b=*k;
printf(\n In change( ) x=%d y=%d,*a,*b);
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 36
Program No: 23
Aim: To read in two numbers, x and n, and then compute the sum of this
geometric progression:1+x+x2+x
3+.+xn
Source Code:
#include
#include
#include
void main( )
{
int s_sum,i,x,n;
clrscr();
printf("Enter the values for x and n:");
scanf("%d %d",&x,&n);
if(n
-
I.B.TECH - CSE C Programming Lab P a g e | 37
Program No: 24
Aim: To perform the addition and multiplication of two complex
numbers using structures.
Source Code:
#include
#include
void arithmetic(int opern);
struct comp
{
double realpart;
double imgpart;
};
void main( )
{
int opern;
clrscr();
printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1:ADD\n 2:MULTIPLY\n 0:EXIT
\n\n\t\t Enter your Option [ ]\b\b");
scanf("%d",&opern);
switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
main( );
}
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 38
void arithmetic(int opern)
{
struct comp w1, w2, w;
printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First
Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First Number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:");
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second Number:");
scanf("%lf",&w2.imgpart);
switch(opern)
{
/*addition of complex number*/
case 1:
w.realpart = w1.realpart+w2.realpart;
w.imgpart = w1.imgpart+w2.imgpart;
break;
/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
break;
}
if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
getch( );
main( );
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 39
Program No: 25
Aim: To copy the contents of one file to another file.
Source Code:
#include
int main(int argc,char *argv[ ])
{
FILE *fp,*ft;
char ch;
if(argc!=3)
{
printf("Error, You should give the destination file name");
return 0;
}
fp=fopen(argv[1],"r");
ft=fopen(argv[2],"w");
while(!feof(fp))
{
ch=fgetc(fp);
fputc(ch,ft);
}
printf("\n File has been copied");
fclose(fp);
fclose(ft);
return 0;
}
www.jntuworld.com
www.jntuworld.com
-
I.B.TECH - CSE C Programming Lab P a g e | 40
Program No: 26
Aim: To reverse the first n characters in a file.
Source Code:
#include
#include
#include
#include
void main(int argc, char *argv[ ])
{
char a[15];
char s[20];
char n;
int i,k,len,j=0;
FILE *fp;
if(argc!=3)
{
puts("\n Improper number of arguments");
exit(0);
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
puts("\n File cannot be opened");
exit(0);
}
k=*argv[2]-48;
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( );
}
www.jntuworld.com
www.jntuworld.com