PSP ASSIGNMENT
NAME: UDIT PATHAK
Class: Artificial Intelligence
USN: 19BTRCI031
1. WAP to print your name using gets() function.
#include<stdio.h>
void main ()
{
char s[30];
printf("Enter the string? ");
gets(s);
printf("You entered %s",s);
}
2. WAP to find the ASCII of a given character.
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("ASCII value of %c = %d",c,c);
return 0;
}
3. WAP to convert temperature from degree Centigrade to
Fahrenheit.
#include<stdio.h>
int main() {
float celsius, fahrenheit;
printf("\nEnter temp in Celsius : ");
scanf("%f", &celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("\nTemperature in Fahrenheit : %f ", fahrenheit);
return (0);
}
4. WAP to calculate sum of 5 subjects and find percentage.
#include <stdio.h>
void main()
{
int eng, phy, chem, math, comp;
int sum;
float percentage;
printf("Enter marks of five subjects: \n");
scanf("%d%d%d%d%d",&eng,&phy,&chem,&math,&comp);
sum = eng + phy + chem + math + comp;
percentage = (sum/500.0)*100;
printf("Sum of all five is =%d\n",sum);
printf("Percentage =%f",percentage);
}
5. WAP to swap two numbers without using third variable.
#include<stdio.h>
void main()
{
int a,b;
printf("Enter the Values for a & b to Swap: ");
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("After Swapping the Values of a & b are :%d %d",a,b);
}
6. WAP to reverse a given string.
#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
printf("Enter a string to reverse\n");
gets(s);
strrev(s);
printf("Reverse of the string: %s\n", s);
return 0;
}
7. WAP to find gross salary.
#include<stdio.h>
int main()
{
float basic,da,hra,gs;
printf("Enter the basic salary :");
scanf("%f",&basic);
da=basic*40/100;
hra=basic*20/100;
gs=basic+da+hra;
printf("DA=%f \n HRA=%f \n Gross salary=%f",da,hra,gs);
return 0;
}
8. WAP to print a table of any given number.
#include<stdio.h>
void main()
{
int i,no,tbl=1;
printf("Enter any number : ");
scanf("%d",&no);
printf("Table of %d\n",no);
for(i=1;i<=10;i++)
{
tbl =no*i;
printf("%d",tbl);
printf("\n");
}
}
9. WAP to find greatest among 3 given numbers.
#include <stdio.h>
void main() {
int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d%d%d",&n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);
if (n2 >= n1 && n2 >= n3)
printf("%d is the largest number.", n2);
if (n3 >= n1 && n3 >= n2)
printf("%d is the largest number.", n3);
}
10. WAP to show the use of conditional operator.
#include <stdio.h>
void main()
{
int mark;
printf("Enter mark: ");
scanf("%d", &mark);
puts(mark >= 40 ? "Passed" : "Failed");
}
11. WAP to find the given year is leap year or not.
#include <stdio.h>
void main()
{
int l;
printf("Enter year: ");
scanf("%d",&l);
if(l% 4 == 0)
{
if( l% 100 == 0)
{
if (l % 400 == 0)
printf("%d is a Leap Year",l);
else
printf("%d is not a Leap Year",l);
}
else
printf("%d is a Leap Year",l);
}
else
printf("%d is not a Leap Year",l);
}
12. WAP to shift inputted data by two bits to the left.
#include<stdio.h>
void main()
{
int n,i;
printf("Enter the number on which left shift operation is to be performed:");
scanf("%d",&n);
printf("\nBefore shifting the number was: %d\n",n);
i=n<<2;
printf("After shifting the number is: %d\n",i);
}
13. WAP to use switch case to display days of week from Monday
to Sunday.
#include<stdio.h>
void main()
{
int n;
printf("Enter any number b/w 1 and 7:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Sunday is the first day of the week\n");
break;
case 2:
printf("Monday is the second day of the week\n");
break;
case 3:
printf("Tuesday is the third day of the week\n");
break;
case 4:
printf("Wednesday is the fourth day of the week\n");
break;
case 5:
printf("Thursday is the fifthe day of the week\n");
break;
case 6:
printf("Friday is the sixth day of the week\n");
break;
case 7:
printf("Saturday is the seventh day of the week\n");
break;
default:
printf("You have entered a wrong choice");
}
}
14. WAP to find if input character is vowel or not.
include <stdio.h>
void main()
{
char c;
int LC,UC;
printf("Enter an alphabet: ");
scanf("%c", &c);
LC = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
UC = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (LC||UC)
printf("%c is a vowel.", c);
else printf("%c is a not vowel.", c); }
15. WAP to display the use of arithmetic operator using switch
case.
#include <stdio.h>
void main() {
char operator;
double n1,n2;
printf("Enter an operator (+, -, *,/):");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&n1,&n2);
switch (operator) {
case 1:
printf("%lf+%lf = %lf",n1,n2,n1+n2);
break;
case 2:
printf("%lf- %lf = %lf", n1,n2, n1 -n2);
break;
case 3:
printf("%lf * %lf = %lf",n1,n2,n1 *n2);
break;
case 4:
printf("%lf / %lf = %lf",n1,n2, n1 / n2);
break;
default:
printf("Error! operator is not correct");
}
}
16. WAP to print the first 10 natural numbers and display their
sum.
#include <stdio.h>
void main()
{
int j, sum = 0;
printf("The first 10 natural number is :\n")
for (j = 1; j <= 10; j++)
{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}
17. WAP to print star sequence1:
*
**
***
****
*****
#include<stdio.h>
void main()
{
int i,j;
for(i=1; i<=6; i++)
{
for(j=1; j<i; j++)
{
printf("*");
}
printf("\n");
}
}
18. WAP to print star sequence2:
*
**
***
****
*****
#include<stdio.h>
void main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=1;j<i;j++)
{
printf(" ");
}
for(k=5;k>=i;k--)
{
printf("*");
}
printf("\n");
}
}
19. WAP to print star sequence3:
8
888
88888
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=3;i++)
{
for (j=4;j>=i;j--)
{
printf(" ");
}
for(j=1;j<=(2*i-1);j++)
{
printf("8");
}
printf("\n");
}
return 0;
}
20. WAP to print star sequence4:
88888
888
8
#include<stdio.h>
int main()
{
int i,j,k;
for(i=3;i>=1;i--)
{
for (j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("8");
}
printf("\n");
}
return 0;
}
21. WAP to print Fibonacci series up to 200.
#include <stdio.h>
void main()
{
int a=1,b=1,c=0,i;
printf("Fibonacci series upto 100\n");
printf("%d\t%d\t",a,b);
for(i=0;i<=10;i++)
{
c=a+b;
if(c<=200)
{
printf("%d\t",c);
}
a=b;
b=c;
}
22. WAP to find factors of a given number.
#include <stdio.h>
void main()
{
int i, Number;
printf("\n Please Enter any number to Find Factors \n");
scanf("%d", &Number);
printf("\n Factors of the Given Number are:\n");
for (i = 1; i <= Number; i++)
{
if(Number%i == 0)
{
printf(" %d ", i);
}
}
}
23. WAP to find exponential without using pow() method.
#include <stdio.h>
int main() {
int base, exp;
long long result = 1;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exp);
while (exp != 0) {
result *= base;
--exp;}
printf("Answer = %lld", result);
return 0; }
24. WAP to find Palindrome of a given number.
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
25. WAP to find the given number is Armstrong or not (upto
1000).
#include<stdio.h>
int main()
{ int n,r,sum=0,temp;
printf("enter the number:");
scanf("%d",&n);
temp=n;
while(n>0)
{
r= n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("Armstrong number");
else
printf("not armstrong number");
return 0;
}
26. WAP to find the Factorial of a given number.
#include <stdio.h>
int main()
{
int c, n, f = 1;
printf("Enter a number\n");
scanf("%d",&n);
for (c = 1; c <= n; c++)
f = f*c;
printf("Factorial of %d = %d\n",n,f);
return 0; }
27. WAP to find whether the given number is prime or not.
#include <stdio.h>
int main() {
int n, i, c = 0;
printf("Enter any number :");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
if (c == 2) {
printf("n is a Prime number");
}
else
{
printf("n is not a Prime number");
}
return 0;
}
28. WAP to find the sum of series 1+1/2+1/3+........+1/n.
#include <stdio.h>
int main()
{
int n;
float s =0.0,n1,n2;
printf("\n Enter the value of number: ");
scanf ("%d",&n);
for(n2=1.0;n2<=n;n2++)
{
n1=1/n2;
s=s+n1;
}
printf("\n The sum of series 1/1 + 1/2 +.... + l/%d = %f",n,s);
return 0;
}
29. WAP to display the series and sum of 1+3+5+........+n.
#include<stdio.h>
void main()
{
int n, i, sum = 0;
printf(" Enter any number: " );
scanf("%d",&n);
for(i = 1; i<n; i = i + 2 )
{
printf("%d+", i);
sum = sum + i;
}
printf("%d",n);
printf(" \nSum = %d ", sum + n );
}
30. WAP to use bitwise AND operator between the two integers.
#include<stdio.h>
void main()
{
int a,b,d=0;
printf("Enter any two integers:");
scanf("%d%d",&a,&b);
d=a&b;
printf("After using bitwise AND on %d and %d, the result is: %d",a,b,d);
}
31. WAP to display sum of 10 elements of array and show the
mean and median as well.
#include <stdio.h>
int main(void) {
int array[10];
int sum =0;
for(int i=0;i<10;i++){
array[i] =i;
}
for(int i=0;i<10;i++){
sum += array[i];
}
printf("%d",sum);
return 0;
}
32. WAP to find the maximum no. in an array.
#include <stdio.h>
int main(void) {
int array[10] = {1,2,3,4,5,6,7,8,9,10};
int max =0;
for(int i=0;i<11;i++){
if(array[i]>max){
max = array[i];
}
}
printf("max number is %d",max);
return 0;
}
33. WAP to display a matrix.
#include <stdio.h>
int main(void) {
int array[3][3] ;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
array[i][j] =j;
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("%d ",array[i][j]);
}
printf("\n \n");
}
return 0;
}
34. WAP to find the sum of two matrices.
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows : ");
scanf("%d", &r);
printf("Enter the number of columns : ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
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];
}
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}
35. WAP to find the subtraction of two matrices.
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], difference[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &second[c][d]);
printf("Difference of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
difference[c][d] = first[c][d] - second[c][d];
printf("%d\t",difference[c][d]);
}
printf("\n");
}
return 0;
}
36. WAP to find the multiplication of two matrices.
#include <stdio.h>
void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2);
void multiplyMatrices(int first[][10], int second[][10], int multResult[][10], int r1, int c1, int r2,
int c2);
void display(int mult[][10], int r1, int c2);
int main() {
int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}
enterData(first, second, r1, c1, r2, c2);
multiplyMatrices(first, second, mult, r1, c1, r2, c2);.
display(mult, r1, c2);
return 0;
}
void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2) {
printf("\nEnter elements of matrix 1:\n");
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c1; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &first[i][j]);
}
}
printf("\nEnter elements of matrix 2:\n");
for (int i = 0; i < r2; ++i) {
for (int j = 0; j < c2; ++j) {
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%d", &second[i][j]);
}}}
void multiplyMatrices(int first[][10], int second[][10], int mult[][10], int r1, int c1, int r2, int c2)
{
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
mult[i][j] = 0;
}
}
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
mult[i][j] += first[i][k] * second[k][j];
}
}
}
}
void display(int mult[][10], int r1, int c2) {
printf("\nOutput Matrix:\n");
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
printf("%d ", mult[i][j]);
if (j == c2 - 1)
printf("\n");
}
}
}
37. WAP to find the transpose of a matrix.
#include<stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
38. WAP to show the input and output of a string.
#include <stdio.h>
int main(void)
{
char string[50];
gets(string);
printf("%s",string);
return 0;
}
39. For the numbers 3, 14, 12, and 37, WAP to check whether
number is a multiple of 3 or a multiple
of 7. (Display 1 if condition is true else 0)
#include <stdio.h>
int main(void) {
int array[4] ={3,14,12,37};
for(int i=0;i<4;i++){
int state =0;
if(array[i]%3==0 || array[i]%7==0){
state =1;
}
printf("d \n",state);
}
}
40. WAP to check whether two given integer values are in the
range 20.....50 inclusive. Return true if one or other is in the said
range otherwise false. Range = (20, 84) (14, 50) (11,45) (25, 40).
#include <stdio.h>
int main(void) {
int array[4][2] ={{20,84},{14,50},{11,45},{25,40}};
for(int i=0;i<4;i++){
if(array[i][0]>19 || array[i][1]<51){
printf("true");
}
else{printf("false");}
printf("\n"); }
}
41. WAP to check if two given non-negative integers have the
same last digit. Numbers = (123, 456)(12, 512) (7, 87) (12, 45).
#include <stdio.h>
#include <stdlib.h>
void check(int x, int y)
{
if(abs(x % 10) == abs(y % 10))
printf("yes\n");
else
printf("no\n");
}int main(){
check(123, 456);
check(12, 512);
check(7, 87);
check(12, 45);
return 0;
}
42. WAP to get the C version you are using.
#include <stdio.h>
int main() {
if(__STDC_VERSION__ >= 201710L)
printf("We are using C18!\n");
else if(__STDC_VERSION__ >= 201112L)
printf("We are using C11!\n");
else if(__STDC_VERSION__ >= 199901L)
printf("We are using C99!\n");
else
printf("We are using C89/C90!\n");
return 0;
}
43. WAP program to calculate the distance between the two
points. (x1, y1, x2, y2 are all double values)
#include <stdio.h>
#include <math.h>
int main() {
float x1, y1, x2, y2, with_out_sqrt;
printf("Input x1: ");
scanf("%f", &x1);
printf("Input y1: ");
scanf("%f", &y1);
printf("Input x2: ");
scanf("%f", &x2);
printf("Input y2: ");
scanf("%f", &y2);
with_out_sqrt = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
printf("Distance between the said points: %.4f", sqrt(with_out_sqrt));
printf("\n");
return 0;
}
OUTPUT:
44. WAP to read an amount (integer value) and break the amount
into smallest possible number of bank notes. (The possible
banknotes are 100, 50, 20, 10, 5, 2 and 1)
#include <stdio.h>
int main() {
int amt, total;
printf("Input the amount: ");
scanf("%d",&amt);
total = (int)amt/100;
printf("There are: ");
printf("%d Note(s) of 100.00\n", total);
amt = amt-(total*100);
total = (int)amt/50;
printf("%d Note(s) of 50.00\n", total);
amt = amt-(total*50);
total = (int)amt/20;
printf("%d Note(s) of 20.00\n", total);
amt = amt-(total*20);
total = (int)amt/10;
printf("%d Note(s) of 10.00\n", total);
amt = amt-(total*10);
total = (int)amt/5;
printf("%d Note(s) of 5.00\n", total);
amt = amt-(total*5);
total = (int)amt/2;
printf("%d Note(s) of 2.00\n", total);
amt = amt-(total*2);
total = (int)amt/1;
printf("%d Note(s) of 1.00\n", total);
return 0;
}
OUTPUT:
45. WAP that reads three floating values and check if it is possible
to make a triangle with them. Also calculate the perimeter of the
triangle if the said values are valid.
#include <stdio.h>
int main() {
float x, y, z, P, A;
printf("Input the first number: ");
scanf("%f", &x);
printf("Input the second number: ");
scanf("%f", &y);
printf("Input the third number: ");
scanf("%f", &z);
if(x < (y+z) && y < (x+z) && z < (y+x))
{ P = x+y+z;printf("Triangle is possible and \nPerimeter is %.1f\n", P); }
else
{printf("Not possible to create a triangle..!"); } }
OUTPUT:
START
X,Y,Z
x < (y+z) && y < (x+z) && z < (y+x)
NOT
POSSIBLE
P=X+Y+Z
P
STOP
46. WAP to read a password until it is correct. For wrong password
print "Incorrect password" and for correct password print "Correct
password" and quit the program. (Use condition Password cannot
be 0 and more than 10 integers, the correct password is 1234)
#include <stdio.h>
int main() {
int passWORD, x=10;
while (x!=0)
{
printf("\nInput the password: Four digits");
scanf("%d",&passWORD);
if (passWORD==1812)
{
printf("Correct password");
x=0; }
else
{ printf("Wrong password, try another");
}
printf("\n"); }
return 0;
}
OUTPUT:
47. WAP to read the coordinate(x, y) (in Cartesian system) and find the quadrant to which it
belongs (Quadrant -I, Quadrant -II, Quadrant -III, Quadrant -IV). (A Cartesian coordinate system
is a coordinate system that specifies each point uniquely in a plane by a pair of numerical
coordinates. These are often numbered from 1st to 4th and denoted by Roman numerals: I (where
the signs of the (x, y) coordinates are I(+,+), II (−,+), III (−,−), and IV (+,−))
#include <stdio.h>
int main()
{
int x, y;
printf("Input the Coordinate(x,y): ");
printf("\nx: ");
scanf("%d", &x);
printf("y: ");
scanf("%d", &y);
if(x > 0 && y > 0)
{
printf("Quadrant-I(+,+)\n");
}
else if(x> 0 && y < 0)
{
printf("Quadrant-II(+,-)\n");
}
else if(x < 0 && y < 0)
{
printf("Quadrant-III(-,-)\n");
}
else if(x < 0 && y > 0) {
printf("Quadrant-IV(-,+)\n");
}
return 0;
}
OUTPUT:
48. WAP to print a number, it’s square and cube in a line, starting from 1 and print n lines.
Accept number of lines (n, integer) from the user.
#include <stdio.h>
int main() {
int a, i, j = 1, x = 0;
printf("Input number of lines: ");
scanf("%d", &a);
for(i = 1; i <= a; i++) {
printf("%d %d %d\n", i, i*i, i*i*i);
}
return 0;
}
OUTPUT:
49. WAP that accepts a distance in centimeters and prints the
corresponding value in inches.
#include <stdio.h>
int main()
{
double inch, cm,conversion_rate=2.54;
printf("Input the distance in cm:\n");
scanf("%lf", &cm);
inch = cm / conversion_rate;
printf("Distance of %0.2lf cms is = %0.2lf inches\n", cm, inch);
return 0;
}
OUTPUT:
FLOW CHART:
START
dist
inch = dist/2.54
inch STOP
50. WAP to calculate the Body Mass index (BMI). Display the
message for its fitness.
#include<stdio.h>
float BMI(float weight, float height) {
return weight/(height*height);
}
int main() {
float weight;
printf("Give weight in kg");
scanf("%f",&weight);
float height;
printf("Give height in m");
scanf("%f",&height);
float bmi = BMI(weight,height);
printf("BMI index is : %.2f ",bmi);
if(bmi<=18.5)
printf("\nUnderweight..... eat something you will die out of starvation");
else if(bmi>18.5&&bmi<=24.9)
printf("\nNormal.... fine but you have to maintain it ");
else
printf("\nOverWeight....Go to gym and work out");
return 0;
}
OUTPUT:
FLOW CHART:
START
weight
height
Bmi = weight/(height/height)
Bmi STOP