Pracs To Write
Pracs To Write
#include<stdio.h>
void main()
{
float n,t,i;
float sum=0;
printf(ENTER No. OF TERMS);
scanf(%d,&n);
for(i=1;i<=n;i++)
{
t=i/((i+1)*(i+2));
sum=sum+t;
}
printf(SUM OF SERIES IS %f,sum);
getch();
}
25. WAP to print the following pattern
1
1 2
1 2 3
1 2 3 4
#include<stdio.h>
void main()
{
int n,i,j;
printf(ENTER No. OF TERMS);
scanf(%d,&n);
for(i=1;i<=n;i++) //outer loop for rows
{
for(j=1;j<=i;j++) // inner loop for columns
{
printf(%2d,j);
}
printf(\n);
getch();
}
26. WAP to print the following pattern.
1
1 2
1 2 3
1 2 3 4
#include<stdio.h>
void main()
{
int n,i,j,k;
printf(ENTER No. OF TERMS);
scanf(%d,&n);
for(i=1;i<=n;i++) //loop for rows
{
for(j=n;j>i;j--) //loop for printing space
{
printf( );
}
for(k=1;k<=i;k++) //loop for printing value
{
printf(%2d,k);
}
printf(\n);
getch();
}
27. WAP to sort 'n' numbers in ascending order.
#include<stdio.h>
void main()
{
int arr[10],n,temp,k;
printf(ENTER SIZE OF ARRAY );
scanf(%d,&n); // size should be< =10
for(i=0;i<n;i++)
{
scanf(%d,&arr[i]);
}
for(j=0;j<n-1;j++)
{
for(i=1;i<n-j-1;i++)
{
if(arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
printf(\n ARRAY OF ELEMENTS AFTER SORTING)
for(i=0;i<n;i++)
{
printf(%d,&arr*i+);
}
printf(\n);
getch();
}
28. WAP perform matrix addition using 2D arrays.
C
ij
=A
ij
+ B
ij
#include<stdionh>
void main()
{
float A[10][10],B[10][10],C[10][10];
int n,m,I,j;
printf(\nENTER SIZE OF MATRICES as m,n);
scanf(%d%d,&m,&n);
printf(\nENTER %d elements of matrix A row-wise ,m*n);
for(i=0;i<m;i++)
{
for(i=0;j<n;j++)
{
scanf(%f,&A*i+*j+);
}
}
printf(\nENTER %d elements of matrix B row-wise ,m*n);
for(i=0;i<m;i++)
{
for(i=0;j<n;j++)
{
scanf(%f,&B*i+*j+);
}
}
for(i=0;i<m;i++)
{
for(i=0;j<n;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
printf(\nSUM OF A+B matrix is:);
for(i=0;i<m;i++)
{
for(i=0;j<n;j++)
{ printf(%7.2f,&C*i+*j+); }
printf(\n);
} getch();
}
29. WAP to create a structure with tag as STUDENT having the following members & read the values
using scanf() & display values
FIELD NAME
DATA TYPE
Name String
Department String
Roll No. Integer
Mark
Integer
#include<stdio.h>
#include<string.h>
struct STUDENT
{
char name[25];
int Roll_no;
int marks;
char dept[20];
};
void main()
{
struct STUDENT stud1;
clrscr();
printf("ENTER STUDENT NAME");
gets(stud1.name);
printf("\nENTER BRANCH");
gets(stud1.dept);
//scanf("%s",&stud1.name);
printf("\nENTER STUDENT ROLLNO.");
scanf("%d",&stud1.Roll_no);
printf("\nENTER STUDENT MARKS");
scanf("%d",&stud1.marks);
printf("\nSTUDENT DETAILS");
printf("\n\nNAME OF STUDENT::%s",stud1.name);
printf("\nSTUDENT BRANCH::%s",stud1.dept);
printf("\nROLL NO. OF STUDENT::%d",stud1.Roll_no);
printf("\nMARKS OBTAINED ::%d",stud1.marks);
getch();
}
30.WAP to swap two numbers using call by reference
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b;
void swap(int *,int *);
printf(Enter two values :);
scanf(%d%d,&a,&b);
printf(Values before swapping: a=%d,b=%d,a,b);
swap(&a,&b);
printf(Values after swapping : a=%d, b=%d,*p,*p1);
getch( );
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
31.WAP to swap two numbers using call by value
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b;
void swap(int *,int *);
printf(Enter two values :);
scanf(%d%d,&a,&b);
printf(Values before swapping: a=%d,b=%d,a,b);
swap(a,b);
printf(Values after swapping : a=%d, b=%d,*p,*p1);
getch( );
}
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
}
32.WAP to reverse a number
#include<stdio.h>
int rev(int n);
int s=0;
void main()
{
int n,r;
printf("enter a number:");
scanf("%d",&n);
r=rev(n);
printf("reverse of %d is=%d",n,r);
getch();
}
int rev(int n)
{
if(n==0)
return 0;
else
{
s=s*10+n%10;
rev(n/10);
}
return s;
}