Professional Documents
Culture Documents
CP .Practical New
CP .Practical New
A)
#include<stdio.h>
int main()
int v,l,b,d;
printf("ENTER LENGTH");
scanf("%d",&l);
printf("ENTER BREATH");
scanf("%d",&b);
printf("ENTER DEPTH");
scanf("%d",&d);
v=l*b*d;
printf("%d",v);
return 0;
b)
#include<stdio.h>
int main()
int i;
scanf("%d", &n);
scanf("%d",&element);
if(a[i]==element)
return 0;
return 0;
#include<stdio.h>
int main()
int a,u,v,t;
printf("ENTER VALUES");
scanf("%d%d%d",&a,&u,&t);
v=u+(a*t);
printf("%d",v);
return 0;
}
2.
#include<stdio.h>
int main()
int a,b,c,t;
printf("ENTER VALUES");
scanf("%d%d%d",&a,&b,&c);
t=2*a+(b+9*c)^1/2; or t=2*a+(b^1/2)+9*c;
printf("%d",t);
return 0;
B)
#include<stdio.h>
int main()
int a[100][100],b[100][100],sum[100][100];
int r,c,i,j;
scanf("%d %d",&r,&c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);}
printf("Enter element of M2:");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{scanf("%d",&b[i][j]);}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{sum[i][j]=a[i][j]+b[i][j];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",sum[i][j]);
return 0;
c) Enter a 4-digit number from keyboard. Add 8 to the number and then divide it
by 3. Now, the modulus of that number is taken with 5 and then multiplies the
resultant value by 5. Display the final result.
d) To perform addition of all elements in Array and display the result.
A)
#include<stdio.h>
int main()
{
int n,a,b,c,d;
printf("enter number");
scanf("%d",&n);
a=n+8;
b=a/3;
c=b%5;
d=c*5;
printf("%d",d);
return 0;
}
B)
#include <stdio.h>
void main()
int a[100];
int i, n, sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("element - %d : ",i);
scanf("%d",&a[i]);
sum += a[i];
#include <stdio.h>
int main()
int n;
scanf("%d", &n);
if(n % 2 == 0)
else
return 0;
B)1.CALL BY REFERENCE
#include <stdio.h>
void swap(int*x,int*y);
int main()
int a,b;
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("In main:a=%d,b=%d",a,b);
return 0;
}
void swap(int*x,int*y)
int t;
t=*x;
*x=*y;
*y=t;
2.CALL BY VALUE
#include <stdio.h>
int main()
int a,b;
scanf("%d%d",&a,&b);
swap(a,b);
printf("In main:a=%d,b=%d",a,b);
return 0;
int t;
t=x;
x=y;
y=t;
printf("In swap function:x=%d,y=%d",x,y);
a) Write a program to find the largest and smallest among three entered numbers and
also display whether the identified largest/smallest number is even or odd.
A)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,min,max;
printf("Enter Three number : ");
scanf("%d%d%d",&a,&b,&c);
if(a < b&& a < c){
min = a;
}
else if (b < a && b < c)
{
min = b;
}
else
{
min = c;
}
if(a > b && a > c){
max = a;
}
else if ( b > a && b > c){
max = b;
}
else{
max = c;
}
printf("\nThe smallest number is : %d",min);
if(min%2 == 0){
printf("\n The smallest number is even");
}
else
{
printf("\n The smallest number is odd");
}
printf("\nThe largest number is : %d",max);
if(max%2 == 0){
printf("\n The largest number is even");
}
else
{
printf("\n The largest number is odd");
}
}
B)
#include<stdio.h>
void main()
int a[100], i, n;
printf("Enter n:\n");
scanf("%d", &n);
for(i=0;i< n;i++)
printf("a[%d]=",i);
scanf("%d", &a[i]);
sort(a,n);
for(i=0;i< n;i++)
printf("%d\t", a[i]);
{
int i, j, temp;
for(i=0;i< n-1;i++)
for(j=i+1;j< n;j++)
if(a[i]>a[j])
temp = a[i];
a[i] = a[j];
a[j] = temp;
7. A student will not be allowed to sit in exam if his/her attendance is less than 75%.
Write and implement a C program to take following input from user
A)
#include<stdio.h>
int main()
{ int a,h=100;
float p;
char c='%';
printf("Total no of days held:%d\n",h);
scanf("%d",&a);
p=a*100/100;
if(p>75)
else
return 0;
B)
#include <stdio.h>
void main()
int sum;
sum = addnum(array);
return(total);
8. a) Write and implement a C program to check whether the entered year is leap year or
not (a year is leap if it is divisible by 4 and divisible by 100 or 400.)
A)
#include <stdio.h>
int main()
int year;
scanf("%d", &year);
if (year % 400 == 0)
}
else if (year % 4 == 0)
else
return 0;
B)
#include<stdio.h>
int main()
int number=50;
int*p,*q;
p=&number;
p=p-3;
q=&number;
q=q-3;
return 0;
2.
#include <stdio.h>
#include <stdlib.h>
int main()
int num1,num2;
int *ptr1,*ptr2;
int sub;
num1=350;
num2=50;
ptr1=&num1;
ptr2=&num2;
sub=*ptr1 - *ptr2;
return 0;
3.
#include <stdio.h>
int main(void)
int i,*p;
for(p=a;p<=a+4;p++)
printf("%d\n",*p);
return 0;
A)
#include<stdio.h>
int main()
int i=1,fact=1,n;
printf("enter number");
scanf("%d",&n);
while(i<=n)
fact=fact*i;
i=i+1;
return 0;
B)
#include <stdio.h>
struct student {
char firstName[50];
int roll;
int mat;
int phy;
int che;
float percentage;
} s[5];
int main() {
int i;
s[i].roll = i + 1;
scanf("%s", s[i].firstName);
scanf("%d", &s[i].mat);
scanf("%d", &s[i].phy);
scanf("%d",&s[i].che);
s[i].percentage=(s[i].che+s[i].mat+s[i].phy)/3;}
printf("Displaying Information:\n\n");
printf("Name: ");
puts(s[i].firstName);
printf("Chemistry Mark:%d\n",s[i].che);
printf("Maths Mark:%d\n",s[i].mat);
printf("\n");
return 0;
a) Use structures to get user input of name, rollno and marks obtained by a student in 4
subjects of 100 marks each and display the name, rollno with percentage score secured.
A)
#include <stdio.h>
struct student {
char firstName[50];
int roll;
int m1;
int m2;
int m3;
int m4;
float percentage;
} s;
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 1; ++i) {
s.roll = i + 1;
printf("Enter roll no:");
scanf("%d",&s.roll);
printf("Enter first name: ");
scanf("%s", s.firstName);
printf("Enter Mark1: ");
scanf("%d", &s.m1);
printf("Enter Mark2: ");
scanf("%d", &s.m2);
printf("Enter Mark3: ");
scanf("%d",&s.m3);
printf("Enter Mark4: ");
scanf("%d",&s.m4);
s.percentage=(s.m1+s.m2+s.m3+s.m4)/4;}
printf("Displaying Information:\n\n");
for (i = 0; i < 1; ++i) {
printf("\nRoll number: %d\n",s.roll);
printf("Name: ");
puts(s.firstName);
printf("Mark1:%d\n",s.m1);
printf("Mark2:%d\n",s.m2);
printf("Mark3:%d\n",s.m3);
printf("Mark3:%d\n",s.m4);
printf("percentage :%.1f\n",s.percentage);
printf("\n");
}
return 0;
}
B)
#include <stdio.h>
int main()
{
char ch;
printf("Enter any alphabet: ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
return 0;
}
A)
#include <stdio.h>
int main()
scanf("%f", &celsius);
return 0;
B)
#include<stdio.h>
#include<string.h>
int main()
char name[10];
int n=0,count=0;
printf("Input:");
scanf("%s",name);
while(name[n]!='\0')
count ++;
n ++;
return 0;
A)
#include<stdio.h>
int main()
return 0;
B)
#include<stdio.h>
int main(){
char name[20];
int age;
float salary;
FILE *fp;
fp=fopen("emp.txt","w+");
printf("enter name");
scanf("%s",&name);
printf("enter age");
scanf("%d",&age);
printf("Enter salary");
scanf("%f",&salary);
fprintf(fp,"%s %d %f",name,age,salary);
printf("\n \n");
fscanf(fp,"%s %d %f",name,age,salary);
printf("Name:%s\nAge:%d\nSalary:%f\n",name,age,salary);
return 0;
A)
#include<stdio.h>
int main()
int a,b,c,t,max;
printf("enter 3 numbers\n");
scanf("%d%d%d",&a,&b,&c);
t=a>b?a:b;
max=t>c?t:c;
return 0;
B)
#include<stdio.h>
#include<stdlib.h>
int main()
char name[10];
FILE*fp;
fp=fopen("student.txt","w+");
if(fp==NULL)
exit(1);}
printf("Enter name:");
scanf("%s",name);
fprintf(fp,"%s",name);
printf("%d",ftell(fp));
rewind(fp);
printf("%d",ftell(fp));
fseek(fp,3,2);
printf("%d",ftell(fp));
return 0;
int main()
{
int bonus,salary,year;
printf("Enter your years of service:");
scanf("%d",&year);
if(year>5)
{printf("Enter your salary:");
scanf("%d",&salary);
bonus=salary*5/100;
printf("Your bonus amount: %d",bonus);}
else
{
printf("You are not eligible for bonus");}
return 0;}
B)
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
return 0;
}
**
***
****
*****
b) To add, subtract, multiply and divide two integers using user defined function
for each.
A)
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
B)
ADD
#include<stdio.h>
#include<conio.h>
struct sum
{
int a;
int b;
};
void main()
{
int sum1;
struct sum s;
clrscr();
sum1=s.a+s.b;
printf("Sum=%d",sum1);
SUBTRACT
#include<stdio.h>
#include<conio.h>
struct sum
{
int a;
int b;
};
void main()
{
int sub1;
struct sub s;
sum1=s.a-s.b;
printf("Sub=%d",sub1);
MULTIPLY
#include<stdio.h>
#include<conio.h>
struct mult
{
int a;
int b;
};
void main()
{
int mult1;
struct mult s;
mult1=s.a*s.b;
printf("mult=%d",mult1);
DIVIDE
#include<stdio.h>
#include<conio.h>
struct divd
{
int a;
int b;
};
void main()
{
int divd1;
struct divd s;
divd1=s.a/s.b;
printf("divd=%d",divd1);
A)
#include <stdio.h>
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}
int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}
B)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,count,*arr,sum = 0,x;
printf("Enter the total number: ");
scanf("%d", &count);
arr = (int *)malloc(count * sizeof(int));
for (i = 0; i < count; i++)
{
printf("Enter element %d : ", (i + 1));
scanf("%d", arr + i);
sum += *(arr + i);
}
printf("sum by malloc is %d \n", sum);
sum= 0;
printf("Enter the resizing number: ");
scanf("%d", &x);
arr= realloc(arr,x);
for (i = 0; i < x; i++)
{
printf("Enter element %d : ", (i + 1));
scanf("%d", arr + i);
sum += *(arr + i);
}