Sie sind auf Seite 1von 35

COMPUTER PRACTICAL ASSIGNMENT-3

Q-1. Write a program to check whether the number entered by the user is
divisible by 4 or not.
Ans-1. (INPUT)
#include<stdio.h>
#include<conio.h>
main()
{int a;
printf("Enter the number");
scanf("%d",&a);
if(a%4==0)
printf("The number is divisible");
else
printf("the number is not divisble");
getch();}
(OUTPUT)

Q-2. Write a program to check if the year entered by the user is leap year or not.
Ans-2. (INPUT)
#include<stdio.h>
#include<conio.h>
main()
{int a;
printf("Enter the number");
scanf("%d",&a);
if(a%4==0)
printf("The year is a leap year");
else
printf("The year is not leap year");
getch();
}
(OUTPUT)

Q-3.Write a program to check the type of triangle from its sides entered by the
user whether it is isosceles or equilateral or scalene.
Ans-3.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{float a,b,c;
printf("enter values of 3 sides of triangle");
scanf("%f %f %f",&a,&b,&c);
if((a==b) && (c==a))
printf("The triangle is equilateral");
else if((a==b) && (b!=c))
printf("The triangle is isosceles");
else
printf("The triangle is scalene");
getch();
}
(OUTPUT)



Q-4.Write a program to print the weekday name from weekday number entered
by the user.
Ans-4.(INPUT)
#include<stdio.h>
#include<conio.h>
main()
{int a;
printf("Enter the number");
scanf("%d",&a);
switch (a)
{

case 1:
printf("The day of week is sunday");
break;
case 2:
printf("The day of week is monday");
break;
case 3:
printf("The day of week is tuesday");
break;
case 4:
printf("The day of week is wednesday");
break;
case 5:
printf("The day of week is thursday");
break;
case 6:
printf("The day of week is friday");
break;
case 7:
printf("The day of week is saturday");
break;
default: ("Invalid entry");
}
}
(OUTPUT)

Q-5.Write a program in C to print the number of days in years entered by the
user.
Ans-5.(INPUT)
#include<stdio.h>
#include<conio.h>
main()
{
float a,b;
printf("Enter the days to convert it in a year");
scanf("%f",&a);
b=a/365;
printf("The days in a year is %f",b);
getch();
}
(OUTPUT)

Q-6.Write a program to convert the uppercase alphabet into lowercase or vice
versa.
Ans-6.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
char a;
printf("enter alphabet");
scanf("%c",&a);
if((a>=97)&&(a<=123))
{
a=a-32;
printf("The upper case alphabet is %c",a);
}
if((a>=65)&&(a<=91))
{
a=a+32;
printf("The lower case alphabet is %c",a);
}
getch();}
(OUTPUT)

Q-7Write a program to display the category of character entered by the user
whether it is alphabet or integer or special character.
Ans-7.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int a;
printf("Enter the character\t");
scanf("%c",&a);
if((a<=90&&a>=65)||(a<=122&&a>=97))
printf("character is an alphabet");
else if(a<=57&&a>=48)
printf("Character is a integer");
else
printf("Character is special character");
getch();
}
(OUTPUT)



Q-8.Write a program to check the case (lowercase or uppercase) of the vowel
entered by the user.
Ans-8.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
char a;
printf("enter alphabet");
scanf("%c",&a);
if((a>=97)&&(a<=123))
{
printf("The lower case alphabet is %c",a);
}
if((a>=65)&&(a<=91))
{
printf("The upper case alphabet is %c",a);
}
getch();}
(OUTPUT)

Q-9.Write a program to determine whether a given number is odd or even and
print the message corresponding to the number.
Ans-9.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int a;
printf("Enter the numbers \t");
scanf("%d",&a);
if(a%2==0)
printf("It is an even number");
else
printf("It is an odd number");
getch();}
(OUTPUT)

Q-10.Write a program to find the roots of quadratic equations
ax^2+bx+c, where a,b,c are input by the user
Ans-10.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
float x1,x2,d;
printf("Enter a,b,c in equation ax^2+bx+c \t");
scanf("%d %d %d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d>0)
{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("Roots are real and unequal\n");
printf("\nx1=%f\t,\tx2=%f",x1,x2);
}
else if(d==0)
{
x1=-b/(2*a);
printf("Roots are real and equal\n");
printf("\nx1=%f\t,\tx2=%f",x1,x2);
}
else
printf("Roots are imaginary");
}
(OUTPUT)

Q-11.Write aprogram to find the smallest of three numbers.
Ans-11. (INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
printf("Enter the values of a,b,c \t");
scanf("%d %d %d",&a,&b,&c);
if(a<b)
{
if(a<c)
printf("%d is the smallest",a);
else
printf("%d is the smallest",c);
}
else if(b<c)
printf("%d is the smallest",b);
else
printf("%d is the smallest",c);
getch();
}
(OUTPUT)

Q-12.Write a program to find the area of circle , rectangle and triangle depending
on users choice.
Ans-12. (INPUT)

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define pi 3.14
main()
{
float area,r,a,b,c,s,l,w;
int n;
printf("Enter 1 for circle , 2 for triangle and 3 for rectangle \t");
scanf("%d",&n);
switch(n)
{case 1:
{printf("Enter the radius \t");
scanf("%f",&r);
area=3.14*r*r;
printf("\n Area of the circle is %f",area);
break;
}
case 2:
{printf("Enter the sides of triangle \t");
scanf("%f %f %f",&a,&b,&c);
if((a+b>c) && (b+c>a) && (c+a>b))
{s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n Area of triangle is %f",area);
}
else
printf("\n Triangle can't be formed");
break;
}
case 3:
{
printf("Enter the length and breadth \t");
scanf("%f %f",&l,&w);
area=l*w;
printf("\n Area of rectangle is %f",area);
break;
}
default:
{
printf("Option is wrong");
}
}
}
(OUTPUT)




Q-13.Write a program in C to reverse a negative number.
Ans-13 (INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int num,rev=0,temp;
printf("enter the number\t");
scanf("%d",&num);
do
{
temp=num%10;
rev=rev*10+temp;
num=num/10;
}
while(num>0);
printf("\nReverse of the number is %d",rev);
}
(OUTPUT)


COMPUTER PRACTICAL ASSIGNMENT-4
Q-1. Write a program to print a list of 10 characters and get their ASCII
values.Add 2 to each ASCII value and again convert this new ASCII value
again to its character form.
Ans-1.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
char c;
int x,y,i=1;
while(i<=10)
{printf("Enter the character \t");
c=getch();
printf("%c\n",c);
x=c;
printf("The ASCII value is %d\t",x);
y=x+2;
printf("The charactere corrresponding to %d is %c\n",y,y);
i++;
}
}
(OUTPUT)

Q-2.Write a program to display the table of n and n^2 for integer value of n
ranging from 10 to 20 along with appropriate column headings.
Ans-2.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{int i,n,a=1,b=1;
printf("Enter the value of n between 10 to 20 \t");
scanf("%d",&n);
if((n>=10)&&(n<=20))
{printf("\n|_________________________________________________________
_________________|\n");
printf("|\t N \t N*N\t\t|\n");
printf("|____________________________________________________________
_______________|\n");
for(i=1;i<=10;i++)
{
a=n*i;
b=n*n*i;
printf("|\t%d * %d = %d\t|",n,i,a);
printf(" \t%d * %d = %d\t|\n",(n*n),i,b);
}
printf("_____________________________________________________________
________________");
}
else
printf("Option is wrong");
}
(OUTPUT)

Q-3.Write a program count to print the pattern
x+x^2+x^3+..x^n
Ans-3. (INPUT)
#include<stdio.h>
#include<conio.h>
main()
{
int x,b,c;
printf("Enter value of b\t");
scanf("%d",&b);
for(c=1;c<=b;c++)
{
printf("x^%d",c);
if(c!=b)
printf(" +");
}
}
(OUTPUT)


Ques-4.Write a program count to print the pattern
1-1/2+1/3-1/n
Ans-4 (INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int i,n;
printf("enter n\t");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
printf("1/%d",i);
if(i!=n)
if(i%2==0)
printf("+ ");
else
printf("- ");
}
}
(OUTPUT)

Q-5.Write a program to print the first n Fibonacci series.
Ans-5.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int a=0,b=1,n,sum=0,i;
printf("Enter n \t");
scanf("%d",&n);
printf("FIBONACCI SERIES \t %d\t",a,b);
for(i=1;i<=n;i++)
{
sum=a+b;
a=b;
b=sum;
printf("%d\t",sum);
}
}
(OUTPUT)

Q-6.Write a program to read the age of 10 persons and count the number of
persons with age groups 50 to 60.Make use of loop and continue statement.
Ans-6.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int i,n,sum=0;
for(i=1;i<=10;i++)
{
printf("Enter the age of person\t");
scanf("%d",&n);
if((n>=50) && (n<=60))
sum=sum+1;
continue;

}
printf("\nnumber of persons having age between 50 and 60 is %d\t",sum);
}
(OUTPUT)

Q-7.Write a program to find the sum of the series: 1!+2!+3!+....n!.
Ans-7.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int fact=1,n,i,sum=0,j;
printf("enter the number \t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
fact=fact*j;
}
sum=sum+fact;
fact=1;
}
printf("Sum of factorials upto n is %d",sum);
}
(OUTPUT)

Q-8.Write a program to print the pyramids given below.
1
232
34543
4567654
Ans-8.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,x,y;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
printf(" ");
x=i+1;
for(k=0;k<i+1;k++)
{printf("%d",x);
x++;
}
y=2*i;
for(k=0;k<i;k++)
{printf("%d",y);
y--;
}
printf("\n");
}
}
(OUTPUT)

Q-9.Write a program to print the pattern
1
2 2
3 3
4 4
3 3
2 2
1
Ans-9.
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,x,k;
for(i=0;i<=3;i++)
{
for(j=0;j<3-i;j++)
printf(" ");
x=i+1;
printf("%d",x);
for(k=0;k<2*i-1;k++)
printf(" ");
if(x>1)
printf("%d",x);
printf("\n");
}
if(i=2)
{for(i=2;i>=0;i--)
{
for(j=0;j<3-i;j++)
printf(" ");
x=i+1;
printf("%d",x);
for(k=0;k<2*i-1;k++)
printf(" ");
if(x>1)
printf("%d",x);
printf("\n");
}
}

}
(OUTPUT)

Q-10.Write a program to print first n prime numbers where n is the maximum
limit giiven by user upto which the prime numbers are to be printed.
Ans-10.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
printf("enter the value of n\t");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
for(j=2;j<=i;j++)
if(i%j==0)
break;
if(i==j)
printf("\n %d is a prime number \t",j);
}
}
(OUTPUT)

Q-11.Write a program to find if the number entered by the user is prime or not.
Ans-11.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int n,i=1;
printf("enter the number\t");
scanf("%d",&n);
for(i=2;i<=n;i++)
if(n%i==0)
break;
if(i==n)
printf("\n %d is a prime number",n);
else
printf("\n %d is not a prime number",n);
}
(OUTPUT)

Q-12.Write a program to print the pyramids given below.
12345
1234
123
12
1
Ans-12.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
}
(OUTPUT)

Q-13.Write a program to reverse a number using do-while.
Ans-13.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int num,rev=0,temp;
printf("enter the number\t");
scanf("%d",&num);
do
{
temp=num%10;
rev=rev*10+temp;
num=num/10;
}
while(num>0);
printf("\nReverse of the number is %d",rev);
}
(OUTPUT)

Q-14.Write a program to find the sum of even numbers from 1 to 10.
Ans-14.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int i,sum=0;
for(i=1;i<=10;i++)
if(i%2==0)
sum=sum+i;
printf("sum of even numberss from 1 to 10 is %d\t",sum);
}
(OUTPUT)

Q-15.Write a program to find the sum of digits of a number entered by the user.
Ans-15.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int num,sum=0,temp;
printf("enter the number \t");
scanf("%d",&num);
do
{
temp=num%10;
sum=sum+temp;
num=num/10;
}
while(num>0);
printf("\nSum of digits of number is %d",sum);
}
(OUTPUT)

Q-16.Write a program to find the factorial of a number entered by the user.
Ans-16.(INPUT)

#include<stdio.h>
#include<conio.h>
main()
{
int i,fact=1,n;
printf("enter the value of n\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("factorial of %d is %d",n,fact);
}
(OUTPUT)

Das könnte Ihnen auch gefallen