Documente Academic
Documente Profesional
Documente Cultură
#include<stdio.h>
int main()
{
int a=5;
int b=3;
int c,d;
c=a+b;
d=c/2;
printf("\nSum=%d\n",c);
printf("Average=%d",d);
return 0;
}
/*OUTPUT
Sum=8
Average=4
*/
Pgm:2 SAJESH S B
10/1/11 ROLL NO: 45
AIM:Write a program to accept two numbers from the user and to find
its product
#include<stdio.h>
int main()
{
int a,b;
int c;
printf("Enter 2 numbers to find the product of:\n");
scanf("%d %d",&a,&b);
c=a*b;
printf("Product=%d",c);
return 0;
}
/*OUTPUT
*/
Pgm:3 SAJESH S B
10/1/11 ROLL NO: 46
AIM:Write a program to accept two numbers from the user and to find
the greatest number
#include<stdio.h>
int main()
{
int a,b;
printf("Enter 2 numbers to find the greatest of:\n");
scanf("%d %d",&a,&b);
if(a>b)
printf("Greatest=%d",a);
else
printf("Greatest=%d",b);
return 0;
}
/*OUTPUT
*/
Pgm:4(a) SAJESH S B
17/1/11 ROLL NO: 46
#include<stdio.h>
int main()
{
int n;
printf("Enter the marks obtained:");
scanf("%d",&n);
if(n<40)
printf("Grade is F");
else if(n>=40&&n<50)
printf("Grade is E");
else if(n>=50&&n<60)
printf("Grade is D");
else if(n>=60&&n<70)
printf("Grade is C");
else if(n>=70&&n<80)
printf("Grade is B");
else if(n>=80&&n<90)
printf("Grade is A");
else if(n>=90&&n<=100)
printf("Grade is S");
return 0;
}
/*OUTPUT
*/
#include<stdio.h>
int main()
{
int a;
printf("\nenter your marks\n");
scanf("%d",&a);
switch (a)
{
case 0 ... 34:printf("\ngrade is E\n");
break;
case 35 ... 49:printf("\ngrade is D\n");
break;
case 50 ... 59:printf("\ngrade is C\n");
break;
case 60 ... 79:printf("\ngrade is B\n");
break;
case 80 ... 100:printf("\ngrade is A\n");
break;
default:printf("invalid");
break;
}
return 0;
}
/* OUTPUT
enter your marks
65
grade is B
/*
Pgm:5 SAJESH S B
17/1/11 ROLL NO: 46
#include<stdio.h>
int main()
{
int a,b,c,p;
printf("Enter 2 numbers to do the operation:\n");
scanf("%d %d",&a,&b);
printf("1.To add\n");
printf("2.To subtract\n");
printf("3.To multiply\n");
printf("4.To divide\n");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:p=a+b;
printf("Sum=%d",p);break;
case 2:p=a-b;
printf("Difference=%d",p);break;
case 3:p=a*b;
printf("Product=%d",p);break;
case 4:p=a/b;
printf("Quotient=%d",p);break;
default:printf("Wrong choice entered");break;
}
return 0;
}
/*OUTPUT
Enter 2 numbers to do the operation:
4
5
1.To add
2.To subtract
3.To multiply
4.To divide
Enter your choice:3
Product=20
*/
Pgm:8 SAJESH S B
1/2/11 ROLL NO: 46
#include<stdio.h>
int main()
{
int i=1,p=1,n;
printf("Enter the number you want to find the factorial of:");
scanf("%d",&n);
while(i<=n)
{
p=p*i;
i++;
}
printf("Factorial:%d",p);
return 0;
}
/*OUTPUT
*/
Pgm:9(a) SAJESH S B
1/2/11 ROLL NO: 46
AIM:/* To print *
* *
* * * */
#include<stdio.h>
int main()
{
int n,i,j;
printf("Enter the number of lines:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*\t");
}
printf("\n");
}
return 0;
}
/*OUTPUT
/*
Pgm:9(b) SAJESH S B
1/2/11 ROLL NO: 46
AIM:/* to print 1
2 3
3 4 5 */
#include<stdio.h>
int main()
{
int n,i,j;
printf("Enter the number of lines:");
scanf("%d",&n);
for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
printf("%d\t",(i+j));}
printf("\n");
}
return 0;
}
/*OUTPUT
*/
Pgm 10 SAJESH S B
1/2/11 ROLL NO: 46
#include<stdio.h>
#include<math.h>
int main()
{
int i,j,x,n,f=1;
float s=1.0;
printf("Enter the value for limit and value of x:");
scanf("%d %d",&n,&x);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
f=f*j;}
s=s+(pow(x,i)/f);
}
printf("Sum=%f",s);
return 0;
}
/*OUTPUT
*/
Pgm 12 SAJESH S B
8/2/11 ROLL NO: 46
#include<stdio.h>
int main()
{
int x,i,n,j,a[100];
printf("Enter a value for array limit:");
scanf("%d",&n);
printf("Enter 10 values:\n");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("The values you have entered are:\n");
for(j=0;j<10;j++)
{
printf("\n%d",a[j]);
}
return 0;
}
/*OUTPUT
*/
Pgm 13 SAJESH S B
8/2/11 ROLL NO: 46
#include<stdio.h>
int main()
{
int i,j,n,s=0;
int a[100];
printf("Enter the array limit:");
scanf("%d",&n);
printf("Enter the numbers in array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(j=0;j<n;j++)
{ s=s+a[j];}
printf("Sum=%d",s);
return 0;
}
/*OUTPUT
/*
Pgm 14 SAJESH S B
8/2/11 ROLL NO: 46
#include<stdio.h>
int main()
{
int i,a[10],s,m,j,k=0;
printf("Enter the array of 10 elements:");
for(i=0;i<10;i++)
{ scanf("%d",&a[i]);}
printf("Enter the number you want to search:");
scanf("%d",&m);
for(j=0;j<10;j++)
{
if(a[j]==m)
{ s=j;
k++;}
}
if(k==0)
printf("the number not found");
}
else
printf("The location of %d is %d",m,s);
return 0;
}
/*OUTPUT
/*
Pgm 15 SAJESH S B
8/2/11 ROLL NO: 46
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);break;
default: printf("Wrong choice entered");break;
}
return 0;
}
/* OUTPUT
Enter the limit of the array: 5
Enter the numbers of the array:
3
2
1
4
5
1.SORT BY SELECTION SORT TECHNIQUE
2.SORT BY BUBBLE SORT TECHNIQUE
Enter your choice: 1
Sorted array:
1
2
3
4
5
Enter the limit of the array: 5
Enter the numbers of the array:
3
2
1
4
5
1.SORT BY SELECTION SORT TECHNIQUE
2.SORT BY BUBBLE SORT TECHNIQUE
Enter your choice: 2
The sorted array is:
1
2
3
4
5
*/
Pgm 16 SAJESH S B
8/2/11 ROLL NO: 46
#include<stdio.h>
int main()
{
int a[10][10],r,c,i,j;
printf("Enter the number of rows and columns:\n");
scanf("%d %d",&r,&c);
printf("Enter the elements of the matrix:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
printf("The resulting matrix is:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
}
return 0;
}
/*OUTPUT
*/
Pgm 19 SAJESH S B
15/3/11 ROLL NO: 46
#include<stdio.h>
int main()
{
int i,len=0,j;
char a[20],b[20];
printf("Enter the string:\n");
scanf("%[^\n]",a);
for(i=0;a[i]!='\0';i++)
{ len++;}
b[len]='\0';
for(i=0,j=len-1;a[i]!='\0';i++,j--)
{
b[j]=a[i];
}
printf("The reverse of string:%s",b);
return 0;
}
/*OUTPUT
*/
Pgm 20 SAJESH S B
15/3/11 ROLL NO: 46
#include<stdio.h>
#include<string.h>
int main()
{
int len1=0,len2=0;
char a[20];
char b[20];
printf("Enter the strings:\n");
scanf("%s\n",a);
scanf("%s\n",b);
len1=strlen(a);
len2=strlen(b);
if(len1==len2)
{
printf("Both strings have same length\n");
int x=strcmp(a,b);
if(x==0)
{
printf("They are same\n");
}
else
printf("They are different words of same length\n");
}
else
printf("Not of the same length\n");
return 0;
}
/*OUTPUT
*/
Pgm 21 SAJESH S B
15/3/11 ROLL NO: 46
/*OUTPUT
*/
Pgm 22 SAJESH S B
15/3/11 ROLL NO: 46
AIM:/*To enter a string and convert lower case letters to upper case and
vice versa*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
char s1[20];
int i;
printf("Enter the string:");
scanf("%s",s1);
int l=strlen(s1);
for(i=0;i<l;i++)
{
if(islower(s1[i]))
s1[i]=toupper(s1[i]);
else
s1[i]=tolower(s1[i]);
}
puts(s1);
return 0;
}
/*OUTPUT
*/
Pgm 23 SAJESH S B
22/3/11 ROLL NO: 46
#include<stdio.h>
int fact(int);
int main()
{
int n,p;
printf("Enter the number to find factorial of:");
scanf("%d",&n);
p=fact(n);
printf("Factorial of %d:%d",n,p);
}
int fact(int n)
{
if(n<2)
return 1;
else
return n*(fact(n-1));
}
/*OUTPUT
*/
Pgm 24 SAJESH S B
22/3/11 ROLL NO: 46
#include<stdio.h>
int magic(int,int);
int sum=0;
int main()
{
int n;
printf("Enter number to check if magic number or not:");
scanf("%d",&n);
int i=1;
int mag=magic(n,i);
if(mag==n)
printf("%d is a magic number",mag);
else
printf("%d is not a magical number",n);
return 0;
}
int magic(int n,int p);
{
if(p<n)
{ if(n%p==0)
{ sum=sum+p;
return magic(n,p+1); }
else
return sum;
}
}
/*OUTPUT
*/
Pgm SAJESH S B
22 . 3. 11 ROLL NO: 46
#include<stdio.h>
int sine()
{
int n,i;
float x,y,sinx;
printf("enter the value and limit");
scanf("%f%d",&x,&n);
x=x*22/(180*7);
y=x;
sinx=x;
printf("sin(%0.3f)=%0.3f",x,sinx);
for(i=2;i<=n;i++)
{
y=y*x*x/((2*i-1)*(2*i-2));
if(i%2==0)
{
sinx=sinx-y;
printf("-");
}
else
{
sinx=sinx+y;
printf("+");
}
printf("(%0.3f^%d)/%d!",x,(2*i-1),(2*i-1));
}
printf("\n=%0.3f\n",sinx);
}
int cosine()
{
int n,i;
float x,y=1,cosx=1;
printf("enter the value and limit");
scanf("%f%d",&x,&n);
x=x*22/(180*7);
printf("cos(%0.3f)=1",x);
for(i=2;i<=n;i++)
{
y=y*x*x/((2*i-3)*(2*i-2));
if(i%2==0)
{
cosx=cosx-y;
printf("-");
}
else
{
cosx=cosx+y;
printf("+");
}
printf("(%0.3f^%d)/%d!",x,(2*i-2),(2*i-2));
}
printf("\n=%0.3f\n",cosx);
}
int logseries()
{
int i,n;
float x,y,log,z;
printf("enter value and limit");
scanf("%f %d",&x,&n);
z=x-1;
y=z;
log=z;
printf("log(%0.3f)=(%0.3f-1)",x,x);
for(i=2;i<=n;i++)
{
y=y*z*(i-1)/i;
if(i%2==0)
{
log=log-y;
printf("-");
}
else
{
log=log+y;
printf("-");
}
printf("(%0.3f-1)^%d/%d!",x,i,i);
}
printf("\n=%0.3f\n",log);
}
int main()
{
int ch;
printf("Type 1 for sine series, 2 for cosine series, 3 for log series \n");
scanf("%d",&ch);
switch(ch)
{
case 1: sine();break;
case 2: cosine();break;
case 3: logseries();break;
default: printf("invalid choice\n");break;
}
return 0;
}
/*OUTPUT-1
Type 1 for sine series, 2 for cosine series, 3 for log series
1
enter the value and limit90
5
sin(1.571)=1.571-(1.571^3)/3!+(1.571^5)/5!-(1.571^7)/7!+(1.571^9)/9!
=1.000
/*OUTPUT-2
Type 1 for sine series, 2 for cosine series, 3 for log series
2
enter the value and limit56
5
cos(0.978)=1-(0.978^2)/2!+(0.978^4)/4!-(0.978^6)/6!+(0.978^8)/8!
=0.559
/*
#include<stdio.h>
int main()
{
FILE *p;
char c;
p=fopen("SAJESH.txt","w");
printf("Enter character:");
while((c=getchar())!='\n')
putc(c,p);
fclose(p);
p=fopen("SAJESH.txt","r");
printf("Entered string is \n");
while((c=fgetc(p))!=EOF)
{
printf("%c",c);
}
fclose(p);
return 0;
}
/*OUTPUT
Enter character:hello
Entered string is
hello
*/
Pgm 26(c) SAJESH S B
22/3/11 ROLL NO: 46
#include<stdio.h>
#include<ctype.h>
int main()
{
FILE *p;
char c;
p=fopen("SAJESH.txt","a");
printf("Enter string:");
while((c=getchar())!='\n')
putc(c,p);
fclose(p);
p=fopen("SAJESH.txt","r");
printf("Entered string is \n");
while((c=fgetc(p))!=EOF)
{
printf("%c",c);
}
fclose(p);
return 0;
}
/*OUTPUT
Enter string:HELLO
Entered string is
helloHELLO
*/
Pgm 26(d) SAJESH S B
22/3/11 ROLL NO: 46
#include<stdio.h>
int main()
{
FILE *p;
FILE *q;
char c,d;
p=fopen("SAJESH.txt","r");
q=fopen("point.txt","w");
printf("Copying:");
while((c=fgetc(p))!=EOF)
{
putc(c,q);
}
fclose(p);
fclose(q);
p=fopen("point.txt","r");
printf("The entered string is \n");
while((c=fgetc(p))!=EOF)
{
printf("%c",c);
}
fclose(p);
return 0;
}
/*OUTPUT
*/
Pgm 16 SAJESH S B
22/03/11 Roll No:46
else
{
n=0;
m++;
}
}
}
}
det+=s*(a[0][c]*detrm(b,k-1));
s*=-1;
}
}
return det;
}
void inverse(float num[25][25],float f)
{
float b[25][25],fac[25][25],ft[25][25];
int p,q,m,n,i,j,detr;
detr=detrm(num,f);
for(p=0;p<f;p++)
{
for(q=0;q<f;q++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
b[i][j]=0;
if(i!=p && j!=q)
{
b[m][n]=num[i][j];
if(n<(f-2))
{
n++;
}
else
{
n=0;
m++;
}
}
}
}
fac[p][q]=pow(-1,p+q)*detrm(b,f-1);
ft[q][p]=fac[p][q];
ft[q][p]/=detr;
}
}
printf("Inverse of matrix is \n");
writematrix(ft,f,f);
}
int main()
{
float a[25][25],b[25][25],k,d,r1,c1,m,n,p,q;
int i,j,ch;
printf("1.Add 2.Multiply 3.Determinant 4.Inverse ?\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter rows and columns of A and B matrix\n");
scanf("%f%f",&r1,&c1);
printf("Enter elemnts of A\n");
readmatrix(a,r1,c1);
printf("Enter B\n");
readmatrix(b,r1,c1);
addmatrix(a,b,r1,c1);
break;
/*
Pgm 18 SAJESH S B
19/3/11 ROLL NO:46
Aim: WAP to enter string and perform the following using structure
a)Find length b)Copy one string to other
c)Reverse String d) concatenate
#include<stdio.h>
#include<string.h>
struct str
{
char s1[20],s2[20];
int len;
}
int main()
{
struct str st;
int i,ch,j;
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Read string");
scanf(" %[^\n]",st.s1);
st.len=strlen(st.s1);
printf("\n The length of string is %d ",st.len);
break;
case 2:printf("Read string");
scanf(" %[^\n]",st.s1);
st.len=strlen(st.s1);
st.s2[st.len]='\0';
for(i=0,j=st.len-1;st.s1[i]!='\0';i++,j--)
{
st.s2[j]=st.s1[i];
}
printf("The reversed string is %s", st.s2);
break;
case 3:printf("Read string");
scanf(" %[^\n]",st.s1);
strcpy(st.s2,st.s1);
printf("The string is %s", st.s2);
case 4:printf("Read string1");
scanf(" %[^\n]",st.s1);
printf("Read string2");
scanf(" %[^\n]",st.s2);
strcat(st.s1,st.s2);
printf("The string is %s", st.s1);
break;
//default :
}
return 0;
}
/*OUTPUT
Enter choice(1-4)
1
Read string Hello
/*