Sei sulla pagina 1di 64

1.

Sum of two short integers

#include <stdio.h>

#include <stdlib.h>

int main()

float x,y;

float sum,mul,div;

printf("SUM OF TWO SHORT INTEGERS \n\n\n");

printf("ENTER THE FIRST NUMBER=\n");

scanf("%f",&x);

printf("ENTER THE SECOND NUMBER=\n");

scanf("%f",&y);

sum = x + y;

printf("THE SUM = %f\n",sum);

mul = x*y;

printf("a*b= %f\n",mul);

div = x/y;

printf("a/b= %f\n",div);

return 0;

}
2. SUM OF UNSIGNED INTEGERS CONSTANT AND LONG INTEGER
CONSTANT.

#include <stdio.h>

#include <stdlib.h>

int main()

unsigned const int x=8;

long const int y=9;

int sum=0;

printf("SUM OF UNSIGNED INTEGERS CONSTANT AND LONG INTEGER CONSTANT\n\n\n");

printf("ENTER THE FIRST NUMBER=\n");

scanf("%d",&x);

printf("ENTER THE SECOND NUMBER=\n");

scanf("%d",&y);

sum = x + y;

printf("THE SUM = %d\n",sum);

printf("Hello world!\n");

return 0;

}
3. The area of circle by taking radius.

/* program that calculates the area of circle by taking radius as an input from keyboard. */

#include <stdio.h>

#include <stdlib.h>

int main()

float r;

float area;

printf("CALCULATE THE AREA OF CIRCLE BY GIVEN RADIUS\n\n\n");

printf("ENTER THE RADIUS = \n");

scanf("%f",&r);

area = 3.14285 * r * r;

printf("THE AREA OF THE CIRCLE= %f\n",area);

return 0;

}
4. Compute the simple interest with user given input of P, R, and T.

/* program that compute the simple interest with user given input of P, R, and T. */

#include <stdio.h>

#include <stdlib.h>

int main()

float p,r,t;

float si;

printf("CALCULATION OF SIMPLE INTEREST\n\n\n");

printf("ENTER THE PRINCIPLE AMOUNT =\n");

scanf("%f",&p);

printf("ENTER THE RATE =\n");

scanf("%f",&r);

printf("ENTER TIME(in years) =\n");

scanf("%f",&t);

si= (p*r*t)/100;

printf("SIMPLE INTEREST = %f",si);

return 0;

}
5. To print " how are you ?"

/* to print " how are you ?" */

#include <stdio.h>

int main()

printf("\"\ how are you?\"\\n");

return 0;

}
6. PERFORM SUMMATION, SUBSTRACTION, MULTIPLICATION AND DIVISION
of INTEGERS.

/* PROGRAM TO PERFORM SUMMATION, SUBSTRACTION,MULTIPLICATION AND DIVISION */

#include <stdio.h>

int main()

int x,y;

int sum;

int diff;

int mul;

float div;

printf(" PROGRAM TO PERFORM SUMMATION, SUBSTRACTION,MULTIPLICATION AND DIVISION\n\n\n");

printf("ENTER THE FIRST NUMBER-\n");

scanf("%d",&x);

printf("ENTER THE SECOND NUMBER-\n");

scanf("%d",&y);

sum = x+y;

printf("THE SUM = %d \n",sum);

diff = x-y;

printf("THE difference = %d \n",diff);

mul = x*y;

printf("THE a * b = %d \n",mul);

div = x/y;

printf("THE a / b = %f \n",div);

return 0;

}
7. PROGRAM TO PERFORM SUMMATION, SUBSTRACTION,MULTIPLICATION
AND DIVISION .

/* PROGRAM TO PERFORM SUMMATION, SUBSTRACTION,MULTIPLICATION AND DIVISION (FLOAT) */

#include <stdio.h>

int main()

float x,y;

float sum;

float diff;

float mul;

float div;

printf(" PROGRAM TO PERFORM SUMMATION, SUBSTRACTION,MULTIPLICATION AND DIVISION\n\n\n");

printf("ENTER THE FIRST NUMBER-\n");

scanf("%f",&x);

printf("ENTER THE SECOND NUMBER-\n");

scanf("%f",&y);

sum = x+y;

printf("THE SUM = %f \n",sum);

diff = x-y;

printf("THE difference = %f \n",diff);

mul = x*y;

printf("THE a * b = %f \n",mul);

div = x/y;

printf("THE a / b = %f \n",div);

return 0;

}
8. Print octal and hexadecimal values of an integer.

#include <stdio.h>

int main()

int x;

printf("enter the integer :\n");

scanf("%d",&x);

printf("octal number is %o \n",x);

printf("hexadecimal number is %x \n",x);

return 0;

}
9. To print string.

#include <stdio.h>

int main()

char A[30];

printf("enter the string to be shown\n");

scanf("%s",&A);

printf("The string is : %s",A);

return 0;

}
10. Size of data type int depends on cpu registor size.

/* size of data type int depends on cpu registor size */

#include <stdio.h>

#include <limits.h>

void main()

int a;

short int b;

long int c;

unsigned int d;

float e;

double f;

char g;

long long h;

unsigned long int k;

printf("%d %d %d %d %d %d %d %d %d", sizeof(a),


sizeof(b),sizeof(c),sizeof(d),sizeof(e),sizeof(f),sizeof(g),sizeof(h),sizeof(k));

}
11. or and xor.

#include <stdio.h>

int main()

int a=38, b=77;

printf("OR=%d\n",a|b);

printf("NOTa=%d\n",!a);

printf(" AND=%d\n",a&b);

printf("XOR=%d\n",a^b);

printf("the product is left shifted by %ld\n",(a*b)<<8);

printf("the product is right shifted by %ld\n",(a*b)>>8);

return 0;

}
12. Add two short integers and store the result in a long variable. Print the results
case 1: by shifting 8 positions right and case 2: by shifting 8 positions left.

#include <stdio.h>

int main()

int x,y;

long int sum;

long int a = sum << 8;

long int b = sum >> 8;

printf("Enter the first sum: \n");

scanf("%d",&x);

printf("Enter the second number: \n");

scanf("%d",&y);

sum=x+y;

printf("The sum: %ld \n ",sum);

printf(" The sum is shifted toward left : %ld \n",a);

printf(" the sum is shifted toward right : %ld \n",b);

return 0;

}
13. Printf how are you?.

/* Use scanf ("%[^\n]%*c", name); take the following input:

Hey how are you? and print the same.

where the [] is the scanset character, [^\n] tells that while the input is not a newline ('\n') take input and
the %*c reads till the newline character from the input buffer.*/

#include <stdio.h>

int main()

char name[50];

printf("What is your name?\n ");

scanf("%s", name);

printf("Hello %s. How are you? \n", name);

return 0;

}
14. Use the following format specifiers: %3.2f, %-4d, %1s, %15.8s, %+6d, %#x, %#f

/* Use the following format specifiers:

%3.2f, %-4d, %1s, %15.8s, %+6d, %#x, %#f */

#include <stdio.h>

int main()

int a=346678,i=1;

float d=234.675;

printf("%6d%15.8s:%-4d\n",i++,"%d",a);

printf("%6d%1s:%3.2f\n",i++,"%f",d);

printf("%6d%15.8s:%#f\n",i++,"%f",d);

printf("%6d%15.8s:%#X\n",i++,"%X",a);

return 0;

}
15. Take any interger and check whether its prime.

#include <stdio.h>

//prime check

void main()

int num,count=0;

printf("Enter an integer : \n");

scanf("%d",&num);

for(int i=2;i<=num;i++)

if (num%i==0)

count++;

if(count==1)

printf("\n %d is prime number.",num);

else

printf("\n %d is not prime number.",num);

}
16. Display and justify the output of each with the sizeof(value) operator.

/* Take suitable value for each of the following data types:

short integer, long integer, unsigned long integer, signed long integer, double, char, long double.

Display and justify the output of each with the sizeof(value) operator.*/

#include<stdio.h>

int main()

printf("%d\n",sizeof(short int));

printf("%d\n",sizeof(long int));

printf("%d\n",sizeof(unsigned long int));

printf("%d\n", sizeof(signed long int));

printf("%d\n",sizeof(double));

printf("%d\n",sizeof(char));

printf("%d\n",sizeof(long double));

return 0;

}
#include <stdio.h>

void main()

int i=10;

printf("%d %d %d %d %d %d %d %d\n",i++,++i,--I,i--,i=+2,i=2+,i=-2,i=2-);

return 0;

}
18. Write a program to differentiate %d and %u output for a any integer
variable.

#include <stdio.h>

void main()

int num;

printf("enter any integer : \n");

scanf("%d",&num);

printf("for %%d we get %d and for %%u we get %u",num,num);

}
19. Write a program using ternary operator for condition checking.

/* Write a program using ternary operator for condition checking.*/

#include<stdio.h>

int main()

int num;

printf("Enter the Number : ");

scanf("%d",&num);

(num%2==0)?printf("Even"):printf("Odd");

}
21. Find the summation of an series for the first 30 numbers: 2.n^2 + 3.n + 5.

/* Find the summation of an series for the first 30 numbers: 2.n^2 + 3.n + 5. (Print the same thing three
times using while, do-while, for loop in the same program) */

#include <stdio.h>

#include <math.h>

int main()

int n,i,sum,x;

printf("21. Find the summation of an series for the first 30 numbers: 2.n^2 + 3.n + 5\n");

n=30;

i=1;

sum=0;

while(i<=n){

x=(2*n*n)+3*n+5;

sum=sum+x;

i++;

printf(" Sum upto 30 terms is %d \n",sum);

return 0;

}
22. Print all the numbers between 1001 to 2001 that are divisible by 7.

/* Print all the numbers between 1001 to 2001 that are divisible by 7. (Print the same thing three times
using while, do-while, for loop in the same program) */

#include <stdio.h>

int main()

int n;

n=1002;

while(n<2001){

if(n%7==0){printf("Numbers divisible by 7 are %d\n",n);

n++;

return 0;

}
23. Print all the leap years between 1901 to 1999.

/* Print all the leap years between 1901 to 1999.*/

#include <stdio.h>

int main()

int n;

n=1901;

printf("Print all the leap years between 1901 to 1999.\n");

while(n<1999){

if(n%4==0){

printf("%d is a leap year\n",n);

n++;

return 0;

}
24. Write any program using loop showing the use of break and continue
statement.

/ Write any program using loop showing the use of break and continue statement.*/

# include <stdio.h>

int main()

int i;

double number, sum = 0.0;

for(i=1; i <= 10; ++i)

printf("Enter a n%d: ",i);

scanf("%lf",&number);

// If user enters negative number, loop is terminated

if(number < 0.0)

continue;

sum += number; // sum = sum + number;

printf("Sum = %.2lf",sum);

return 0;

}
25. The outstanding balance on a home loan Rs. 8,00000. Each year a
payment of Rs 30000 is made which includes both interest and principal
repayment of the car loan. The monthly interest is calculated as 11% of the
outstanding balance of the loan for the first year and 14% for the rest. After
the interest is deducted the remaining part of the payment is used to payoff
the loan. Using this information, write a C program that produces a table
indicating the beginning monthly balance, the interest payment, the principal
payment, and the remaining loan balance after each payment is made. Your
output should resemble and complete the entries in the following table until
the outstanding loan balance is zero.

#include <stdio.h>

int main()

int prin,rate,years=0,months=0;

printf("THE GIVEN PRINCIPLE AMOUNT IS 800000\n");

prin=800000;

rate=11;

while(1){

if(years>1){

rate=14;

prin= prin+(rate*0.01*prin);

if(prin>(30000*12)){

prin=prin-(30000*12);

years++;

if(prin<=0)

{ break;

}
}

else {

months=prin/30000;

break;

printf("%d years %d months",years,months);

}
26. Write a program to check whether a given number is an Armstrong
number or not. An Armstrong number is the one in which the sum of cubes of
its digit is equal to the number itself. Your program should take a number as
input and output whether the given.

/*Write a program to check whether a given number is an Armstrong number or not. An Armstrong
number is the one in which the sum of cubes of its digit is equal to the number itself. Your program
should take a number as input and output whether the given number is an Armstrong number or not.
For example:

Input: 371

Output: Armstrong Number (27+343+1=371)*/

#include <stdio.h>

int main()

int number, originalNumber, remainder, result = 0;

printf("Enter a three digit integer: \n");

scanf("%d", &number);

originalNumber = number;

while (originalNumber != 0)

remainder = originalNumber%10;

result += remainder*remainder*remainder;

originalNumber /= 10;

}
if(result == number)

printf("%d is an Armstrong number.",number);

else

printf("%d is not an Armstrong number.",number);

return 0;

}
27. Write a program to assign a grade according to the marks received in an
exam. Make use of the switch statement. Your program should take the
marks as input from the user and print the appropriate grade.

/* Write a program to assign a grade according to the marks received in an exam. Make use of the
switch statement. Your program should take the marks as input from the user and print the appropriate
grade.

More than 80 receives A grade

More than 65 receives B grade

More than 50 receives C grade

More than 30 receives D grade

Less than 30 receives Failed */

#include<stdio.h>

int main()

int marks,y;

printf("\nEnter The Mark: ");

scanf("%d", &marks);

if(marks>100)

printf("\nEnter your Marks Between Limit\n");

else

y=(marks>80)?1:(marks>65)?2:(marks>50)?3:(marks>30)?4:5;

switch(y)

{
case 1 :

printf("\nYour Grade Is: A or Excellent");

break;

case 2 :

printf("\nYour Grade Is: B or Very Good" );

break;

case 3 :

printf("\nYour Grade Is: C or Fair" );

break;

case 4 :

printf("\nYour Grade Is: D or Pass");

break;

case 5 :

printf("\nYou Grade Is: F or Fail\n");

return 0;

}
28. Take two numbers a and b as input from the user and print the sum of the
squares of all the odd numbers between a and b (including a and b).

// Take two numbers a and b as input from the user and print the sum of the squares of all the odd
numbers between a and b (including a and b)

#include<stdio.h>

int main()

int a,b,sum,i;

printf("ENTER THE VALUES OF a and b \n");

scanf("%d %d",&a,&b);

sum=(a*a)+(b*b);

for(i=a+1;i<b;i++);

if(i%2==1){ sum=sum+(i*i);}

printf("the sum is %d \n",sum);

}
29. There are 100 students in a class. There is a event A: a student knows C
programming, event B: a student knows Fortran programming. Take suitable
inputs for P(A or B), P(A and B) and another variable of your choice. Write a
program to find the outputs.

//There are 100 students in a class. There is a event A: a student knows C programming, event B: a
student knows Fortran programming. Take suitable inputs for P(A or B), P(A and B) and another variable
of your choice. Write a program to find the outputs of P(B|A), P(A|B), P(not B| not A), P(not A| not B)
using Bayes theorem.

#include<stdio.h>

int main()

int a,b;

printf("enter the value P(a or B) : \n");

scanf("%d",&a||b);

printf("enter the value p(a and b): \n");

scanf("%d",&a&&b);

printf("enter the value ofp(a): \n");

scanf("%d",&a);

printf("the value P(a/B) : %d \n",a/b);

printf("the value of p(b/a) : %d",b/a);

printf("the value P(not a /not b) : %d \n",!a/!b);

}
30. Narcissistic.

/*Input a range from user and print all the narcissistic number in that range. (A number is called
narcissistic if each of its digits raised to the power of the number of digits equals the number.)

153 is a narcissistic number since 1^3 + 5^3 + 3^3

= 1 + 125 + 27 = 153. */

#include<stdio.h>

int power(int c, int d)

int pow = 1;

int count = 1;

while(count <= d)

pow = pow*c;

count++;

return pow;

int main()

int num1, digit, temp, rem, sum = 0;

printf("\n Enter a number:\t");

scanf("%d", &num1);

printf("\n Enter Number of Digits:\t");


scanf("%d", &digit);

temp = num1;

while(temp != 0)

rem = temp%10;

sum = sum + power(rem, digit);

temp = temp/10;

if(sum == num1)

printf("\n%d is a Narcissistic Number\n", num1);

else

printf("\n%d is not a Narcissistic Number\n", num1);

return 0;

}
31. Print the following (using a single nested loop): (Question corrected).

/* Print the following (using a single nested loop): (Question corrected) */

#include<stdio.h>

int main()

int rows, i, j, space;

int I, s, k=0;

printf("Enter number of rows: \n");

scanf("%d",&rows);

for(i=rows; i>=1; --i)

for(space=0; space < rows-i; ++space)

printf(" ");

for(j=i; j <= 2*i-1; ++j)

printf("* ");

for(j=0; j < i-1; ++j)

printf("* ");

printf("\n");

for(I=1; I<=rows; ++I, k=0)


{

for(s=1; s<=rows-I; ++s)

printf(" ");

while(k != 2*I-1)

printf("* ");

++k;

printf("\n");

return 0;

}
32. Store your name in a one dimensional array and print it from the array
character-wise using a loop.

// Store your name in a one dimensional array and print it from the array character-wise using a loop.

#include <stdio.h>

int main()

char name[50];

int i,alp;

printf("Enter your Name=\n");

scanf("%s",&name);

while(name[i]!='\0')

if((name[i]>='a' && name[i]<='z') || (name[i]>='A' && name[i]<='Z'))

alp++;

printf("Number of Alphabets in the string is : %d\n", alp);

return 0;

}
33. Using 2 dimensional array, develop a playable tic-tac-toe. Print the result
after every move. Declare a winner or draw as the game ends.

// Using 2 dimensional array, develop a playable tic-tac-toe. Print the result after every move. Declare a
winner or draw as the game ends.

#include <stdio.h>

char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

int checkwin();

void board();

int main()

int player = 1, i, choice;

char mark;

do

board();

player = (player % 2) ? 1 : 2;

printf("Player %d, enter a number: ", player);

scanf("%d", &choice);

mark = (player == 1) ? 'X' : 'O';


if (choice == 1 && square[1] == '1')

square[1] = mark;

else if (choice == 2 && square[2] == '2')

square[2] = mark;

else if (choice == 3 && square[3] == '3')

square[3] = mark;

else if (choice == 4 && square[4] == '4')

square[4] = mark;

else if (choice == 5 && square[5] == '5')

square[5] = mark;

else if (choice == 6 && square[6] == '6')

square[6] = mark;

else if (choice == 7 && square[7] == '7')

square[7] = mark;

else if (choice == 8 && square[8] == '8')

square[8] = mark;
else if (choice == 9 && square[9] == '9')

square[9] = mark;

else

printf("Invalid move ");

player--;

getch();

i = checkwin();

player++;

}while (i == - 1);

board();

if (i == 1)

printf("==>\aPlayer %d win ", --player);

else

printf("==>\aGame draw");

getch();

return 0;
}

int checkwin()

if (square[1] == square[2] && square[2] == square[3])

return 1;

else if (square[4] == square[5] && square[5] == square[6])

return 1;

else if (square[7] == square[8] && square[8] == square[9])

return 1;

else if (square[1] == square[4] && square[4] == square[7])

return 1;

else if (square[2] == square[5] && square[5] == square[8])

return 1;

else if (square[3] == square[6] && square[6] == square[9])

return 1;

else if (square[1] == square[5] && square[5] == square[9])

return 1;
else if (square[3] == square[5] && square[5] == square[7])

return 1;

else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&

square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7]

!= '7' && square[8] != '8' && square[9] != '9')

return 0;

else

return - 1;

void board()

system("cls");

printf("\n\n\tTic Tac Toe\n\n");

printf("Player 1 (X) - Player 2 (O)\n\n\n");

printf(" | | \n");

printf(" %c | %c | %c \n", square[1], square[2], square[3]);

printf("_____|_____|_____\n");
printf(" | | \n");

printf(" %c | %c | %c \n", square[4], square[5], square[6]);

printf("_____|_____|_____\n");

printf(" | | \n");

printf(" %c | %c | %c \n", square[7], square[8], square[9]);

printf(" | | \n\n");

}
34. Write a program that takes any two pairs of input for x- and y-coordinate
in cartesian coordinate system and suggests the output as the third
coordinate that makes the triangle an equilateral triangle.

/* Write a program that takes any two pairs of input for x- and y-coordinate in cartesian coordinate
system and suggests the output as the third coordinate that makes the triangle an equilateral triangle.

Ex. input: (x1,y1) and (x2,y2)

output: The third coordinate to form an equilateral triangle is (x3,y3). */

#include <stdio.h>

int main()

int x1,x2,x3,y1,y2,y3;

printf("*************************************************************\n");

printf("**** to find third coordinate of an equilateral triangle.****\n");

printf("*************************************************************\n\n");

printf("enter the first coordinate of an equilateral triangle\n");

scanf("%d %d",&x1,&y1);

printf("enter the second coordinate of an equilateral triangle\n");

scanf("%d %d",&x2,&y2);

x3=( (x1+x2) +( (sqrt (3)) * (y1-y2) ))/2;

y3=( (y1+y2) +( (sqrt (3)) * (x1-x2) ))/2;

printf("the third coordinate of an equilateral triangle is (%d,%d)\n",x3,y3);

return 0;

}
35. Write a program that takes three numbers as input from the user and
checks whether any combination of these form a Pythagorean triplet or not.
A Pythagorean triplet is the one in which the sum of squares of two numbers
is equal to the square of the third number.

#include <stdio.h>

#include <math.h>

int main()

int a,b,c,sum1,sum2,sum3;

printf("enter three numbers-\n");

scanf("%d %d %d",&a,&b,&c);

sum1 = sqrt((a*a)+(b*b));

sum2 = sqrt((c*c)+(b*b));

sum3 = sqrt((a*a)+(c*c));

if(sum1==c)

{ printf("the given numbers are pythogorous triplet\n");

if(sum2==a)

{ printf("the given numbers are pythogorous triplet\n");

if(sum3==b)

{ printf("the given numbers are pythogorous triplet\n");

else{ printf("the given numbers are not pythogorous triplet\n");

return 0;

}
36. Write a program using the function getchar() that inputs a rational
numbers and operators and then does that operation on them and prints it
using putchar(). scanf or printf should not be used. ASCII values may be used
to convert characters to integer.

// Write a program using the function getchar() that inputs a rational numbers and operators and then
does that operation on them and prints it using putchar(). scanf or printf should not be used. ASCII
values may be used to convert characters to integers.

#include <stdio.h>

int main()

float c,d;

puts("enter a rational numbers=\n");

c = getchar();

d = 3 + c;

putchar(d);

return 0;

}
37. Write a program to display the fibonacci series upto a number that is
taken as input and passed to the user-defined function.

/* Write a program to display the fibonacci series upto a number that is taken as input and passed to the
user-defined function.

e.g., input x, declare fibo(x) to display the the series.

*/

#include <stdio.h>

int main()

int n1=0,n2=1,sum,i,m;

printf("enter the number upto which fibonacci series to be displayed\n");

scanf("%d",&m);

for(i=1;i<=m;i++)

printf("%d ",n2);

sum=n1+n2;

n1=n2;

n2=sum;

return 0;

}
38. Write a user-defined funtion to evaluate factorial of a number taken as
input.

//Write a user-defined funtion to evaluate factorial of a number taken as input.

#include<stdio.h>

long factorial(int);

main()

int number;

long fact = 1;

printf("Enter a number to calculate it's factorial\n");

scanf("%d",&number);

printf("%d! = %ld\n", number, factorial(number));

return 0;

long factorial(int n)

int c;

long result = 1;

for( c = 1 ; c <= n ; c++ )

result = result*c;

return ( result );

}
39.

// Take any two pairs of coordinates as input which form two different circles of the same radius 10
units. Find whether the two circles intersect; if they do, find the area of intersection; otherwise, display
"doesn't intersect".

#include <stdio.h>

#include <math.h>

int main()

int x1,y1,x2,y2;

float dist,a;

printf("Enter the coordinates of first circle:\n");

scanf("%d %d",&x1,&y1);

printf("the coordinates of first circle: are (%d,%d)\n",x1,y1);

printf("Enter the coordinates of second circle:\n");

scanf("%d %d",&x2,&y2);

printf("the coordinates of second circle: are (%d,%d)\n",x2,y2);

d = sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));

printf("the distance between centres of two circles is %f\n",d);

if(dist<=20){

printf("The circles are intersecting\n");

a= (100*(acos(d/20)))-((d/4)*sqrt(400-pow(d,2)));

printf("Area of intersection=%f\n",a);

else{
printf("The circles are not intersecting\n");

return 0;

}
40. Take any two 4 x 4 matrices such as A and B, multiply them and store the
resultant matrix in matrix C. Display C. Take user input as A or B to perform
addition of C and A or B. Show the result of addition again. Follow matrix
multiplication rules.

//Take any two 4 x 4 matrices such as A and B, multiply them and store the resultant matrix in matrix C.
Display C. Take user input as A or B to perform addition of C and A or B. Show the result of addition
again. Follow matrix multiplication rules.

#include<stdio.h>

void main()

int i,j,p=4,r=4,k;

int a[4][4],b[4][4],ma[4][4];

int c[4][4];

printf("\n\t\tINPUT:");

printf("\n\t\t------");

printf("\n\t\tEnter the value for matrix A\n");

for(i=0;i<p;i++)

for(j=0;j<r;j++)

scanf("%d",&a[i][j]);

printf("\n");

printf("\n\t\tEnter the value for matrix B\n");

for(i=0;i<p;i++)
{

for(j=0;j<r;j++)

scanf("%d",&b[i][j]);

printf("\n");

for(i=0;i<p;i++)

for(j=0;j<r;j++)

c[i][j]=0;

for(k=0;k<p;k++)

c[i][j] +=a[i][k]*b[k][j];

printf("\n\t\tOUTPUT:");

printf("\n\t\t-------");

printf("\n\t\tThe multiplication matrix C is:\n");

for(i=0;i<p;i++)

for(j=0;j<r;j++)
{

printf("\t\t%d",c[i][j]);

printf("\n");

for(i=0;i<p;i++)

for(j=0;j<r;j++)

ma[i][j]=a[i][j]+c[i][j];

printf("\n\t\tThe addition matrix is:\n");

for(i=0;i<p;i++)

for(j=0;j<r;j++)

printf("\t\t%d",ma[i][j]);

printf("\n");

getch();

}
41.

// Take any continuous function f(x) and input two coordinates for the upper and lower limit. Take
another input coordinate to check whether it satsfies the Lagrange's mean value theorem or not. f(x)
and all the input should be taken in such a manner so that the theorem is once satisfied and once not
satisfied.

#include<stdio.h>

void main()

float a,x,uplimit,lowlimit,c,fup,flow,f2,f1;

a=x*x-3*x+5;

uplimit=4;

lowlimit=1;

printf("Function Considered: x*x-3*x+5 defined in [1,4]\n");

printf("Enter a coordinate to check Lagrange mean value theorem\n");

scanf("%f",&c);

f1=2*c-3;

fup=4*4-3*4+5;

flow=4*1-3*1+5;

f2=(fup-flow)/3;

if(f1==f2){

printf("c=%f satisfies LMT",c);

else{printf("Does not follow LMT");

}
42. Program to create, initialize, assign and access a pointer variable.

// Program to create, initialize, assign and access a pointer variable.

#include <stdio.h>

int main()

int num;

int *pNum;

pNum=& num;

num=100;

printf("Using pointer variable:\n");

printf("value of num: %d\naddress of num: %u\n",*pNum,pNum);

return 0;

}
43. Program to swap two numbers using pointers.

//Program to swap two numbers using pointers.

#include <stdio.h>

int main()

int x, y, *a, *b, temp;

printf("Enter the value of x and y\n");

scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

a = &x;

b = &y;

temp = *b;

*b = *a;

*a = temp;

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;

}
44. Program to change the value of constant integer using pointers.

// Program to change the value of constant integer using pointers.

#include <stdio.h>

int main()

const int a=10;

int *p;

p=&a;

printf("Before changing - value of a: %d",a);

*p=20;

printf("\nAfter changing - value of a: %d",a);

printf("\nvalue has changed.");

return 0;

}
45. Program to print a string using pointer.

#include <stdio.h>

int main()

char str[100];

char *ptr;

printf("Enter a string: ");

gets(str);

ptr=str;

printf("Entered string is: ");

while(*ptr!='\0')

printf("%c",*ptr++);

return 0;

}
46. Program to count vowels and consonants in a string using pointer.

// Program to count vowels and consonants in a string using pointer.

#include <stdio.h>

int main()

char str[100];

char *ptr;

int cntV,cntC;

printf("Enter a string: ");

gets(str);

//assign address of str to ptr

ptr=str;

cntV=cntC=0;

while(*ptr!='\0')

if(*ptr=='A' ||*ptr=='E' ||*ptr=='I' ||*ptr=='O' ||*ptr=='U' ||*ptr=='a' ||*ptr=='e' ||*ptr=='i'


||*ptr=='o' ||*ptr=='u')

cntV++;

else

cntC++;

//increase the pointer, to point next character

ptr++;

}
printf("Total number of VOWELS: %d, CONSONANT: %d\n",cntV,cntC);

return 0;

}
47. Program to read array elements and print with addresses.

// Program to read array elements and print with addresses.

#include <stdio.h>

int main()

int arr[10]; //declare integer array

int *pa; //declare an integer pointer

int i;

pa=&arr[0]; //assign base address of array

printf("Enter array elements:\n");

for(i=0;i < 10; i++){

printf("Enter element %02d: ",i+1);

scanf("%d",pa+i); //reading through pointer

printf("\nEntered array elements are:");

printf("\nAddress\t\tValue\n");

for(i=0;i<10;i++){

printf("%08X\t%03d\n",(pa+i),*(pa+i));

return 0;

}
49. Program to print size of different types of pointer variables.

//49. Program to print size of different types of pointer variables.

#include <stdio.h>

int main()

printf("\nsize of char pointer: %d" ,sizeof(char*));

printf("\nsize of int pointer: %d" ,sizeof(int*));

printf("\nsize of float pointer: %d" ,sizeof(float*));

printf("\nsize of long int pointer: %d" ,sizeof(long int*));

printf("\nsize of double pointer: %d\n" ,sizeof(double*));

return 0;

}
50. Program to demonstrate example of double pointer (pointer to pointer).

// Program to demonstrate example of double pointer (pointer to pointer)

#include <stdio.h>

int main()

int a;

int *p1;

int **p2;

p1=&a;

p2=&p1;

a=100;

printf("\nValue of a (using p1): %d",*p1);

printf("\nValue of a (using p2): %d",**p2);

*p1=200;

printf("\nValue of a: %d",*p1);

**p2=200;

printf("\nValue of a: %d",**p2);

return 0;

}
51. Program to demonstrate example of array of pointers.

// Program to demonstrate example of array of pointers.

#include <stdio.h>

int main()

int a,b,c;

int *ptr[3];

ptr[0]= &a;

ptr[1]= &b;

ptr[2]= &c;

a=100;

b=200;

c=300;

printf("value of a: %d, b: %d, c: %d\n",*ptr[0],*ptr[1],*ptr[2]);

*ptr[0] +=10;

*ptr[1] +=10;

*ptr[2] +=10;

printf("After adding 10\nvalue of a: %d, b: %d, c: %d\n",*ptr[0],*ptr[1],*ptr[2]);

return 0;

Potrebbero piacerti anche