Vous êtes sur la page 1sur 22

IB.

TECH -COMPUTER PROGRAMMING LAB

Program1:
/* Write a C program to find the sum of individual digits of a positive integer.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0;
clrscr();
printf("enter the number \n");
scanf("%d",&n);
do
{
r=n%10;
sum=sum+r;
n=n/10;
}
while(n>0);
printf("sum of digits of integer numbers=%d",sum);
getch();
}

Program2:
/* A Fibonacci Sequence is defined as follows: the first and second terms in the
sequence are 0 and 1. Subsequent terms are found by adding the preceding two
terms in the sequence. Write a C program to generate the first n terms of the
sequence.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,f0,f1,f2,i;
clrscr();
printf("enter the number \n");
scanf("%d",&n);
f0=0;
f1=1;
printf("%d\t%d\t",f0,f1);
for(i=1;i<=(n-2);i++)
{
f2=f0+f1;
printf("%2d",f2);
f0=f1;
f1=f2;
}
getch();
}

Program3:
/* Write a C program 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 i,j,n,cnt=0;
clrscr();
printf("enter the number \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i==1)
{
continue;
}
for(j=1;j<=i;j++)
{
if(i%j==0)
cnt++;
}
if(cnt==2)
printf("%d\n",i);
cnt=0;
}
getch();
}

Program4:
/* Write a C program toe find the roots of a quadratic equation. */

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int r1,r2,a,b,c;
clrscr();
printf("enter the values of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
if((b*b)==(4*a*c))
{
r1=-b/(2*a);
r2=-b/(2*a);
printf("the roots are r1=%d,r2=%d",r1,r2);
}
else if((b*b)>(4*a*c))
{
r1=(-b+sqrt((b*b)-(4*a*c)))/(2*a);
r2=(-b-sqrt((b*b)-(4*a*c)))/(2*a);
printf("the roots are r1=%d,r2=%d",r1,r2);
}
else
{
printf("roots are imajinary\n");
}
getch();
}

Program5:
/* Write a C program to calculate the following Sum:
Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! */

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i,j;
int n,m;
double x;
int sum=0;
clrscr();
printf("enter x value to evalute serise \n");
scanf("%lf",&x);
scanf("%d",&n);
for(i=0,j=0;j<=n;i++,j=j+2)
{
sum=sum+(pow(-1,i)*pow(x,j)/fact(j));
}
printf("sum=%d",sum);
}
int fact(int m)
{
if(m==0||m==1)
return 1;
else
return(m*fact(m-1));
}

Program6:
/* Write C programs that use both recursive and non-recursive functions
To find the factorial of a given integer.*/

#include<stdio.h>
#include<conio.h>
main()
{
int n,ans;
printf("enter n value \n");
scanf("%d",&n);
ans=fact(n);
printf("factorial=%d",ans);
}
int fact(int m)
{
if(m==0||m==1)
return 1;
else
return(m*fact(m-1));
}

#include<stdio.h>
#include<conio.h>
main()
{
int n,ans;
clrscr();
printf("enter n value \n");
scanf("%d",&n);
ans=fact(n);
printf("factorial=%d",ans);
}
int fact(int m)
{
int i,fact=1;
for(i=m;i>=1;i--)
{
fact*=i;
}
return(fact);
getch();
}

Program7:
/* Write C programs that use both recursive and non-recursive functions
To find the GCD (greatest common divisor) of two given integers.*/

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,gcd;
clrscr();
printf("enter valus of a,b\n");
scanf("%d%d",&a,&b);
gcd=gcdrecursive(a,b);
printf("gcd of given numbers=%d",gcd);
}
int gcdrecursive(int m,int n)
{
if(n>m)
return gcdrecursive(n,m);
if(n==0)
return m;
else
return(gcdrecursive(n,m%n));
}

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,gcd,GCD;
printf("enter valus of a,b\n");
scanf("%d%d",&a,&b);
if(a<b)
{
gcd=gcdnonrecursive(a,b);
printf("gcd of given no's=%d",gcd);
}
else
{
GCD=gcdnonrecursive (b,a);
printf("GCD of given no's=%d",GCD);
}
int gcdnonrecursive(int m,int n)
{
int r;
do
{
r=n%m;
n=m;
m=r;
}
while(r!=0)
return n;
getch();
}

Program8:
/* The total distance travelled by vehicle in 't' seconds is given by distance =
ut+1/2at2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2).
Write C program to find the distance travelled at regular intervals of time given
the values of 'u' and 'a'. The program should provide the flexibility to the user
to select his own time intervals and repeat the calculations for different values of
'u' and 'a'.*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int u,a,t;
float s;
char ch='y';
while(ch=='y')
{
printf("enter the valus of u,a,t\n");
scanf("%d%d%d",&u,&a,&t);
s=(u*t)+((0.5*a*pow(t,2)));
printf("distance=%f",s);
ffiush(stdin);
printf("do u want to continue \n");
scanf("%c",&ch);
}
getch();
}
Program9:
/* Write C programs that use both recursive and non-recursive functions
To solve Towers of Hanoi problem.*/

#include<conio.h>
#include<stdio.h>
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
goto four;
}
two:
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;
goto one;
three:
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;
goto one;
four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}
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("Non-Recursive");
hanoiNonRecursion(no,'A','B','C');
printf("\nRecursive");
hanoiRecursion(no,'A','B','C');
getch();
}
Program10:
/* Write a C program, which takes two integer operands and one operator form the
user, performs the operation and then prints the result. (Consider the operators
+,-,*, /, % and use Switch Statement)
*/

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

void main()
{
int a,b,res;
char ch;
clrscr();
printf("\n\n\tEnter your choice:");
scanf("%c",&ch);
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
switch(ch)
{
case ‘+’:
res=a+b;
printf("\n Addition:%d",res);
break;
case ‘-‘:
res=a-b;
printf("\n Subtraction:%d",res);
break;
case ‘*’:
res=a*b;
printf("\n Multiplication:%d",res);
break;
case ‘/’:
res=a/b;
printf("\n Division:%d",res);
break;
default:
printf("\n Invalid Choice");
}
getch();
}
Program11:
/* Write a C program to generate Pascal's triangle. */

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,n;
clrscr()
printf("enter the value \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=(n-i);j++)
printf(" ");
for(k=1;k<=1;k++)
printf("%d",i);
printf("\n");
}
}

Program12:
/* Write a C program to construct a pyramid of numbers. */

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,n;
clrscr();
printf("enter the value \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=(n-i);j++)
printf(" ");
for(k=(2-i);k<=i;k++)
printf("%d",i);
printf("\n");
}
getch();
}
Program13:
/*Write a C program to read in two numbers, x and n, and then compute the sum of
this geometric progression:1+x+x2+x3+………….+xn */

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int x,i,n;
int sum;
clrscr();
printf("enter the value of x ,n");
scanf("%d",&x);
scanf("%d",&n);
for(i=0;i<=n;i++)
{
sum=sum+pow(x,i);
}
printf("%d",sum);
}

Program14:
/* Write a C program to find both the largest and smallest number in a list of
integers*/

#include<stdio.h>
#include<conio.h>
main()
{
int A[10],i,j,max,min,n;
clrscr();
printf("eter the number of items in a array \n");
scanf("%d",&n);
printf("enter the value in an array\n");
for(i=0;i<=(n-1);i++)
{
scanf("%d",&A[i]);
}
max=min=A[0];
for(i=1;i<=(n-1);i++)
{
if(max<A[i])
max=A[i];
if(min>A[i])
min=A[i];
}
printf("max:%d,min=%d",max,min);
getch();
}

Program15:
/* Write a C program that uses functions to perform the following:
i) Addition of Two Matrices
ii) Multiplication of Two Matrices*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[10][10],b[10][10];
int c[10][10];
clrscr();
printf("enter order of matrx \n");
scanf("%d",&n);
printf("enter values ofmatrix a");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the valus of matrix b \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("result matrix c is \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%5d",c[i][j]);
}
printf("\n");
}
}

#include<stdio.h>
#include<math.h>
void main()
{
int r1,r2,c1,c2,a[10][10],b[10][10];
int c[10][10];
int i,j,k;
clrscr();
printf("enter order of matrix a \n");
scanf("%d%d",&r1,&c1);
printf("enter order of matrix b \n");
scanf("%d%d",&r2,&c2);
printf("enter the values of matrix a \n");
for(i=0;i<=r1-1;i++)
{
for(j=0;j<=c1-1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the valus in matrix b \n");
for(i=0;i<=r2-1;i++)
{
for(j=0;j<=c2-1;j++)
{
scanf("%d",&b[i][j]);
}
}
if(c1==r1)
{
for(i=0;i<=r1-1;i++)
{
for(j=0;j<=c2-1;j++)
{
c[i][j]=0;
for(k=0;k<=c1-1;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
}
printf("the resultant matrix c is \n");
for(i=0;i<=r1-1;i++)
{
for(j=0;j<=c2-1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

Program16:
/* Write a C program to determine if the given string is a palindrome or not */

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10],s2[10];
clrscr();
printf("enter the string");
scanf("%s",s1);
strcpy(s2,s1);
strrev(s1);
if(strcmp(s1,s2)==0)
printf("string is palindrom \n");
else
printf("string is not palindrom");
}
Program17:
/* Write a C program that displays the position or index in the string S
where the string T begins, or - 1 if S doesn't contain T.
*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s[20];
char t[20];
char *ptr;
int pos;
clrscr();
printf("enter string s \n");
gets(s);
printf("enter string t \n");
gets(t);
ptr=strstr(s,t);
if(ptr)
printf("the string %s is contained in string%s with pos:%d",t,s,ptr-s);
else
printf("-1");
}

Program18:
/* Write a C program that uses functions to perform the following operations:
To insert a sub-string in to given main string from a given position.
*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char mainstr[20];
char substr[20];
char tempstr[20];
int pos,l1,l2;
char resstr[50];
clrscr();
printf("enter main string \n");
gets(mainstr);
printf("enter sub string \n");
gets(substr);
printf("enter position to insert \n");
scanf("%d",&pos);
l1=strlen(mainstr);
l2=strlen(substr);
if(pos>l1)
{
strcat(mainstr,substr);
strcpy(resstr,mainstr);
}
else
{
strcpy(tempstr,mainstr+pos);
strcpy(mainstr+pos,substr);
strcpy(mainstr+pos+l2,tempstr);
strcpy(resstr,mainstr);
}
printf("the resultant string:%s",resstr);
getch();
}

Program19:
/* Write a C program that uses functions to perform the following operations:
To delete n Characters from a given position in a given string.
*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char mainstr[20];
int n;
int pos;
char resstr[50];
clrscr();
printf("enter main string");
gets(mainstr);
printf("enter the position to delete \n");
scanf("%d",&pos);
printf("enter no of characters to delete");
scanf("%d",&n);
strcpy(mainstr+pos,mainstr+pos+n);
strcpy(resstr,mainstr);
printf("the result string is %s",resstr);
}

Program20:
/* Write a C program to count the lines, words and characters in a given text*/

#include<stdio.h>
#include<conio.h>
main()
{
char str[100];int noc,now,nos,nol;
int i=0;
clrscr();
printf("enter the string \n");
gets(str);
nos=0;
nol=1;
now=1;
while(str[i]!=NULL)
{
if(str[i]==' ')
{
nos++;
now++;
}
if(str[i]=='\n')
nol++;
i++;
}
noc=i;
printf("no of lines=%d\n",nol);
printf("no of spaces=%d\n",nos);
printf("no of words=%d\n",now);
printf("no of chars=%d\n",noc);
getch();
}
Program21:
/* 2’s complement of a number is obtained by scanning it from right to left and
complementing all
the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100.
Write a C program to find the 2’s complement of a binary number.*/

#include<stdio.h>
#include<conio.h>
main()
{
char bin[100];int pos,i,len;
clrscr();
printf("enter the binary no \n");
scanf("%s",bin);
len=strlen(bin);
printf("%d",len);
printf("%s",bin);
for(i=len-1;i>=0;i--)
{
if(bin[i]=='1')
{
pos=i;
printf("%d",pos);
break;
}
}
for(i=0;i<pos;i++)
{
if(bin[i]=='0')
bin[i]='1';
if(bin[i]=='1')
bin[i]='0';
}
printf("2's compliment=%s",bin);
getch();
}

Program22:
/* Write a C program to convert a Roman numeral to its decimal equivalent. */

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

void main()
{

int *a,len,i,j,k;
char *rom;

clrscr();

printf("Enter the Roman Numeral:");


scanf("%s",rom);

len=strlen(rom);

for(i=0;i<len;i++)
{
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}

Program23:
/* Write a C program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.) */

#include<stdio.h>
#include<conio.h>
struct complex
{
int a;
int b;
}c1,c2;
main()
{
int r1,r2;
float r3,r4;
printf("enter the complex no:\n");
scanf("%d%d",&c1.a,&c1.b);
printf("enter the complex no2=%n");
scanf("%d%d",&c2.a,&c2.b);
r1=c1.a+c2.a;
r2=c2.b+c2.b;
printf("addition of complex no:%d+i%d",r1,r2);
r1=c1.a-c2.a;
r2=c1.b-c2.b;
printf("subtraction of complex no :%d-i%d",r1,r2);
r1=(c1.a*c2.a)-(c1.b*c2.b);
r2=(c1.a*c2.b)+(c1.b*c2.a);
printf("multiplication of complex no:%d+i%d",r1,r2);
r3=(((c1.a*c2.a)+(c1.b*c2.b))/((c2.a*c2.a)+(c2.b*c2.b)));
r4=(((c1.b*c2.a)-(c1.a*c2.b))/((c2.a*c2.a)+(c2.b*c2.b)));
printf("division 0f complex no:%f+i%f",r3,r4);
getch();
}
Program24:
/* Write a C program which copies one file to another.*/

#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp,*fp1,*fp2,*fp3;
int ch,ch1;
char *filename1,*filename2;
clrscr();
printf("enter the filename\n");
gets(filename1);
printf("enter the filename2\n");
gets(filename2);
fp=fopen(filename2,"w");
fclose(fp);
fp1=fopen(filename1,"r");
fp2=fopen(filename2,"w");
while((ch=getc(fp1))!=EOF)
{
putc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
fp3=fopen(filename2,"r");
while(ch=getc(fp3)!=EOF)
{
printf("%c",ch1);
}
fclose(fp3);
}

Program25:
/* Write a C program to reverse the first n characters in a file.
(Note: The file name and n are specified on the command line.)*/

#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=*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();
}

Vous aimerez peut-être aussi