CL Assignment
CL Assignment
#include <stdio.h>
#include <stdlib.h>
int main()
float x,y;
float sum,mul,div;
scanf("%f",&x);
scanf("%f",&y);
sum = x + y;
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()
int sum=0;
scanf("%d",&x);
scanf("%d",&y);
sum = x + y;
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;
scanf("%f",&r);
area = 3.14285 * r * r;
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;
scanf("%f",&p);
scanf("%f",&r);
scanf("%f",&t);
si= (p*r*t)/100;
return 0;
}
5. To print " how are you ?"
#include <stdio.h>
int main()
return 0;
}
6. PERFORM SUMMATION, SUBSTRACTION, MULTIPLICATION AND DIVISION
of INTEGERS.
#include <stdio.h>
int main()
int x,y;
int sum;
int diff;
int mul;
float div;
scanf("%d",&x);
scanf("%d",&y);
sum = x+y;
diff = x-y;
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 .
#include <stdio.h>
int main()
float x,y;
float sum;
float diff;
float mul;
float div;
scanf("%f",&x);
scanf("%f",&y);
sum = x+y;
diff = x-y;
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;
scanf("%d",&x);
return 0;
}
9. To print string.
#include <stdio.h>
int main()
char A[30];
scanf("%s",&A);
return 0;
}
10. 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;
}
11. or and xor.
#include <stdio.h>
int main()
printf("OR=%d\n",a|b);
printf("NOTa=%d\n",!a);
printf(" AND=%d\n",a&b);
printf("XOR=%d\n",a^b);
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;
scanf("%d",&x);
scanf("%d",&y);
sum=x+y;
return 0;
}
13. Printf how are you?.
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];
scanf("%s", name);
return 0;
}
14. 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;
scanf("%d",&num);
for(int i=2;i<=num;i++)
if (num%i==0)
count++;
if(count==1)
else
}
16. Display and justify the output of each with the sizeof(value) operator.
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(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;
scanf("%d",&num);
}
19. Write a program using ternary operator for condition checking.
#include<stdio.h>
int main()
int num;
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++;
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){
n++;
return 0;
}
23. Print all the leap years between 1901 to 1999.
#include <stdio.h>
int main()
int n;
n=1901;
while(n<1999){
if(n%4==0){
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;
scanf("%lf",&number);
continue;
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;
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;
}
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
#include <stdio.h>
int main()
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
else
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.
#include<stdio.h>
int main()
int marks,y;
scanf("%d", &marks);
if(marks>100)
else
y=(marks>80)?1:(marks>65)?2:(marks>50)?3:(marks>30)?4:5;
switch(y)
{
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
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;
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);}
}
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;
scanf("%d",&a||b);
scanf("%d",&a&&b);
scanf("%d",&a);
}
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.)
= 1 + 125 + 27 = 153. */
#include<stdio.h>
int pow = 1;
int count = 1;
while(count <= d)
pow = pow*c;
count++;
return pow;
int main()
scanf("%d", &num1);
temp = num1;
while(temp != 0)
rem = temp%10;
temp = temp/10;
if(sum == num1)
else
return 0;
}
31. Print the following (using a single nested loop): (Question corrected).
#include<stdio.h>
int main()
int I, s, k=0;
scanf("%d",&rows);
printf(" ");
printf("* ");
printf("* ");
printf("\n");
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;
scanf("%s",&name);
while(name[i]!='\0')
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()
char mark;
do
board();
player = (player % 2) ? 1 : 2;
scanf("%d", &choice);
square[1] = mark;
square[2] = mark;
square[3] = mark;
square[4] = mark;
square[5] = mark;
square[6] = mark;
square[7] = mark;
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
player--;
getch();
i = checkwin();
player++;
}while (i == - 1);
board();
if (i == 1)
else
printf("==>\aGame draw");
getch();
return 0;
}
int checkwin()
return 1;
return 1;
return 1;
return 1;
return 1;
return 1;
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]
return 0;
else
return - 1;
void board()
system("cls");
printf(" | | \n");
printf("_____|_____|_____\n");
printf(" | | \n");
printf("_____|_____|_____\n");
printf(" | | \n");
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.
#include <stdio.h>
int main()
int x1,x2,x3,y1,y2,y3;
printf("*************************************************************\n");
printf("*************************************************************\n\n");
scanf("%d %d",&x1,&y1);
scanf("%d %d",&x2,&y2);
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;
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)
if(sum2==a)
if(sum3==b)
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;
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.
*/
#include <stdio.h>
int main()
int n1=0,n2=1,sum,i,m;
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.
#include<stdio.h>
long factorial(int);
main()
int number;
long fact = 1;
scanf("%d",&number);
return 0;
long factorial(int n)
int c;
long result = 1;
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;
scanf("%d %d",&x1,&y1);
scanf("%d %d",&x2,&y2);
d = sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
if(dist<=20){
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------");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&a[i][j]);
printf("\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-------");
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];
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;
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){
}
42. Program to create, initialize, assign and access a pointer variable.
#include <stdio.h>
int main()
int num;
int *pNum;
pNum=& num;
num=100;
return 0;
}
43. Program to swap two numbers using pointers.
#include <stdio.h>
int main()
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
return 0;
}
44. Program to change the value of constant integer using pointers.
#include <stdio.h>
int main()
int *p;
p=&a;
*p=20;
return 0;
}
45. Program to print a string using pointer.
#include <stdio.h>
int main()
char str[100];
char *ptr;
gets(str);
ptr=str;
while(*ptr!='\0')
printf("%c",*ptr++);
return 0;
}
46. Program to count vowels and consonants in a string using pointer.
#include <stdio.h>
int main()
char str[100];
char *ptr;
int cntV,cntC;
gets(str);
ptr=str;
cntV=cntC=0;
while(*ptr!='\0')
cntV++;
else
cntC++;
ptr++;
}
printf("Total number of VOWELS: %d, CONSONANT: %d\n",cntV,cntC);
return 0;
}
47. Program to read array elements and print with addresses.
#include <stdio.h>
int main()
int i;
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.
#include <stdio.h>
int main()
return 0;
}
50. 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;
*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.
#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;
*ptr[0] +=10;
*ptr[1] +=10;
*ptr[2] +=10;
return 0;