fcp assigment (1)
fcp assigment (1)
Q1. Write a C program to rotate the value of three variables (x has the
value of y, y has the value of z and z has the value of x).
Ans.
#include<stdio.h>
int main(){
int x,y,z;
printf("x=");
scanf("%d",&x);
printf("y=");
scanf("%d",&y);
printf("z=");
scanf("%d",&z);
x=y;
y=z;
z=x;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}
OUTPUT :
x=4
y=7
z=3
x=7
y=3
z=7
Q2. Write a C program to find the rightmost digit of the integral part of
the number.
Ans.
#include<stdio.h>
int main(){
int n;
printf("n=");
scanf("%d",&n);
n=n%10;
printf("%d",n);
return 0;
}
U24EE082
OUTPUT :
n=174
4
Q3. Write a C program to display the number as follows: First line : All
digits , Second line : All except first digit , Third line : All except first two
digits . . . . . . Last line : Last digit .
Ans.
#include<stdio.h>
#include<math.h>
int main(){
int n,x=0,i,temp;
printf("n=");
scanf("%d",&n);
temp=n;
while(temp>0){
x++;
temp=temp/10;
}
temp=n;
for(i=x;i>=1;i--){
int y=pow(10,i);
int num=temp%y;
printf("%d\n",num);
}
return 0;
}
OUTPUT:
n=2754
2754
754
54
4
U24EE082
Q4. For a certain electrical circuit with an inductance L and resistance
R, the damped natural frequency is given by Frequency = sqrt{(1/L*C)-
(R*R/4*C*C)} Write a C program to calculate the frequency for different
values of C starting from 0.01 to in steps of 0.01 .
Ans.
#include<stdio.h>
#include<math.h>
int main(){
float l,r,c,freq,x;
printf("L=");
scanf("%f",&l);
printf("R=");
scanf("%f",&r);
printf("C=");
scanf("%f",&c);
x=(1/(l*c))-((r*r)/(4*c*c));
freq=pow(x,0.5);
printf("%f2",freq);
return 0;
}
OUTPUT:
L=1
R=.001
C=.01
9.9998752
U24EE082
Assignment 2
Q1. Write a C program to Add and Subtraction of two matrices.
Ans.
#include<stdio.h>
int main(){
int n1,m1,n2,m2,i,j;
printf("Enter rows of 1st matrix = ");
scanf("%d",&n1);
printf("Enter coloum of 1st matrix = ");
scanf("%d",&m1);
int arr1[n1][m1];
for(i=0;i<n1;i++){
for(j=0;j<m1;j++){
printf("Enter number at idx %d%d ",i,j );
scanf("%d",&arr1[i][j]);
}
}
printf("Enter rows of 2st matrix = ");
scanf("%d",&n2);
printf("Enter coloum of 2st matrix = ");
scanf("%d",&m2);
int arr2[n2][m2];
for(i=0;i<n2;i++){
for(j=0;j<m2;j++){
printf("Enter number at idx %d%d ",i,j );
scanf("%d",&arr2[i][j]);
}
}
int arr3[n1][m1];
printf("Sum of the matrix \n");
if(n1==n2 && m1==m2){
for(i=0;i<n1;i++){
for(j=0;j<m1;j++){
arr3[i][j]=arr1[i][j]+arr2[i][j];
printf("%d ",arr3[i][j]);
}
printf("\n");
}
}else{
printf("Sum is not possible");
}
int arr4[n1][m1];
U24EE082
printf("Subtraction of the matrix \n");
if(n1==n2 && m1==m2){
for(i=0;i<n1;i++){
for(j=0;j<m1;j++){
arr4[i][j]=arr1[i][j]-arr2[i][j];
printf("%d ",arr4[i][j]);
}
printf("\n");
}
}else{
printf("Subtraction is not possible");
}
return 0;
}
OUTPUT.
Enter rows of 1st matrix = 2
Enter coloum of 1st matrix = 3
Enter number at idx 00 1
Enter number at idx 01 2
Enter number at idx 02 3
Enter number at idx 10 4
Enter number at idx 11 5
Enter number at idx 12 6
Enter rows of 2st matrix = 2
Enter coloum of 2st matrix = 3
Enter number at idx 00 6
Enter number at idx 01 5
Enter number at idx 02 4
Enter number at idx 10 3
Enter number at idx 11 2
Enter number at idx 12 1
Sum of the matrix
777
777
Subtraction of the matrix
-5 -3 -1
135
Q2. Write a C program to store elements in array and display the
elements in ascending order.
Ans.
#include<stdio.h>
int main(){
int n,i,j,temp;
U24EE082
printf("enter the size of arr = ");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++){
printf("enter element at idx %d = ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++){
for(j=0;j<n-i;j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(i=0;i<n;i++){
printf("%d",arr[i]);
}
return 0;
}
OUTPUT.
enter the size of arr = 5
enter element at idx 0 = 1
enter element at idx 1 = 9
enter element at idx 2 = 6
enter element at idx 3 = 4
enter element at idx 4 = 7
14679
int main(){
int n,m,i,j,k,x=0;
printf("Enter rows = ");
scanf("%d",&n);
printf("Enter coloum = ");
scanf("%d",&m);
k=(n*m)/2;
U24EE082
int arr[n][m];
for(i=0;i<n;i++){
for(j=0;j<m;j++){
printf("Enter number at idx %d%d ",i,j );
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(arr[i][j]==0){
x++;
}
}
}
if(x>k){
printf("Matrix is sparse");
}else{
printf("Matrix is not sparse");
}
return 0;
}
OUTPUT.
Enter rows = 3
Enter coloum = 3
Enter number at idx 00 0
Enter number at idx 01 0
Enter number at idx 02 1
Enter number at idx 10 0
Enter number at idx 11 0
Enter number at idx 12 0
Enter number at idx 20 0
Enter number at idx 21 3
Enter number at idx 22 0
Matrix is sparse
Q4. Write a C program to print a sin(x) and cos(x) table (0-180, Interval
of 15 degrees) .
Ans.
#include<stdio.h>
int main(){
int x,i;
for(i=0;i<=180 ;i=i+15){
U24EE082
x=i;
switch(x){
case 0 : printf("sin(0)=0,cos(0)=1\n");
break;
case 15 : printf("sin(15)=0.2588,cos(15)=0.9659\n");
break;
case 30 : printf("sin(30)=0.5,cos(30)=0.8666\n");
break;
case 45 : printf("sin(45)=0.707,cos(45)=0.707\n");
break;
case 60 : printf("sin(60)=0.866,cos(60)=0.5\n");
break;
case 75 : printf("sin(75)=0.96,cos(75)=0.2588\n");
break;
case 90 : printf("sin(90)=1,cos(90)=0\n");
break;
case 105 : printf("sin(105)=0.9659,cos(105)=-0.2588\
n");
break;
case 120 : printf("sin(120)=0.866,cos(120)=-0.5\n");
break;
case 135 : printf("sin(135)=0.707,cos(135)=-707\n");
break;
case 150 : printf("sin(150)=0.5,cos(150)=-0.866\n");
break;
case 165 : printf("sin(165)=0.2588,cos(165)=-0.965\
n");
break;
case 180 : printf("sin(180)=0,cos(180)=-1\n");
break;
}
}
return 0;
}
OUTPUT.
sin(0)=0,cos(0)=1
sin(15)=0.2588,cos(15)=0.9659
sin(30)=0.5,cos(30)=0.8666
sin(45)=0.707,cos(45)=0.707
sin(60)=0.866,cos(60)=0.5
sin(75)=0.96,cos(75)=0.2588
sin(90)=1,cos(90)=0
U24EE082
sin(105)=0.9659,cos(105)=-0.2588
sin(120)=0.866,cos(120)=-0.5
sin(135)=0.707,cos(135)=-707
sin(150)=0.5,cos(150)=-0.866
sin(165)=0.2588,cos(165)=-0.965
sin(180)=0,cos(180)=-1
Q5. Write a C program to print a square and square root table (0-100,
Interval of 10 units) .
Ans.
#include<stdio.h>
#include<math.h>
int main(){
int x,i,j;
float y;
for(i=0;i<=100;i=i+10){
x=i*i;
printf("Suqure of %d = %d\n",i,x);
}
for(i=0;i<=100;i=i+10){
y=pow(i,0.5);
printf("Suqure root of %d = %f\n",i,y);
}
return 0;
}
OUTPUT.
Suqure of 0 = 0
Suqure of 10 = 100
Suqure of 20 = 400
Suqure of 30 = 900
Suqure of 40 = 1600
Suqure of 50 = 2500
Suqure of 60 = 3600
Suqure of 70 = 4900
Suqure of 80 = 6400
Suqure of 90 = 8100
Suqure of 100 = 10000
Suqure root of 0 = 0.000000
Suqure root of 10 = 3.162278
Suqure root of 20 = 4.472136
Suqure root of 30 = 5.477226
Suqure root of 40 = 6.324555
Suqure root of 50 = 7.071068
U24EE082
Suqure root of 60 = 7.745967
Suqure root of 70 = 8.366600
Suqure root of 80 = 8.944272
Suqure root of 90 = 9.486833
Suqure root of 100 = 10.000000
int main(){
int n,min,max,i;
printf("enter the size of arr = ");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++){
printf("enter element at idx %d = ",i);
scanf("%d",&arr[i]);
}
min=arr[0];
max=arr[0];
for(i=0;i<n;i++){
if(arr[i]<min){
min=arr[i];
}
if(arr[i]>max){
max=arr[i];
}
}
printf("minimum=%d\n",min);
printf("maximum=%d",max);
return 0;
}
OUTPUT.
enter the size of arr = 5
enter element at idx 0 = 1
enter element at idx 1 = 4
enter element at idx 2 = 7
enter element at idx 3 = 3
enter element at idx 4 = 2
minimum=1
U24EE082
maximum=7
Assignment 3
Q1. Write a C program to read a stirng and rewrite it in the alphabetical
order. For example, the word string should be written as ginrst.
Ans.
#include<stdio.h>
#include<string.h>
int main(){
int i,j;
char temp;
char str[100];
printf("String=");
gets(str);
for(i=0;i<strlen(str)-1;i++){
for(j=0;j<strlen(str)-1-i;j++){
if(str[j]>str[j+1]){
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
for(i=0;i<strlen(str);i++){
printf("%c",str[i]);
}
return 0;
}
OUTPUT:
String=deacut
acdetu
U24EE082
Q2. Write a C program to copy one string into another and count the
number of characters copied.
Ans.
#include <stdio.h>
int main() {
char source[100], destination[100];
int i = 0, count = 0;
return 0;
}
OUTPUT:
Enter a string: aabge
Copied string: aabge
Number of characters copied: 5
int main(){
int i,j;
char temp;
char str[100];
printf("String=");
gets(str);
U24EE082
for(i=0;i<strlen(str)-1;i++){
for(j=0;j<strlen(str)-1-i;j++){
if(str[j]>str[j+1]){
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
for(i=0;i<strlen(str);i++){
printf("%c",str[i]);
}
return 0;
}
OUTPUT:
String=ajczp
acjpz
Q4. Write a C program that reads a string from the user and determines
whether the string is a Palindrome or not(A string is Palindrome if it can
be read from left and right with the same meaning).
Ans.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len, isPalindrome = 1;
len = strlen(str);
U24EE082
if (isPalindrome) {
printf("The string is a Palindrome.\n");
} else {
printf("The string is NOT a Palindrome.\n");
}
return 0;
}
OUTPUT:
Enter a string: acbca
The string is a Palindrome.
Q5. Write a C program to read strings and compare them using the
function strncmp() and print a message that the first string is equal to,
greater or less than the second one.
Ans.
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
if (result == 0) {
printf("The first string is equal to the second string.\
n");
} else if (result > 0) {
printf("The first string is greater than the second
string.\n");
} else {
printf("The first string is less than the second string.\
n");
U24EE082
}
return 0;
}
OUTPUT:
Enter the first string: abDe
Enter the second string: abde
The first string is less than the second string.
Q6. Write a C program to read a line of text from the user and print out
the number of occurrences of a given substring.
Ans.
#include <stdio.h>
#include <string.h>
int main() {
char text[1000], sub[100];
int count = 0, i, j, found;
U24EE082
printf("The substring \"%s\" occurred %d time(s).\n", sub,
count);
return 0;
}
OUTPUT:
Enter a line of text: abde
Enter the substring to search: de
The substring "de" occurred 1 time(s).
int main() {
char sentence[1000];
int vowels = 0, consonants = 0, i = 0;
U24EE082
return 0;
}
OUTPUT:
Enter a sentence: ahged
Number of vowels: 2
Number of consonants: 3
U24EE082
Assignment 4
Ans.
(a). wrong .
Correct : int void (fun);
(b) Correct
(c) Correct
(d) Wrong because void cannot use as a parameter type
Ans.
200 40000
U24EE082
Q3. The function test is coded as follows:
int test (int number) {
int m, n = 0;
while (number)
{
m = number % 10;
if (m % 2)
n = n + 1;
number = number /10;
}
return (n);
}
What will the x and y values be when the following statements are
executed?
int x = test (135);
int y = test (246);
Ans.
Aim : to find even number in a digit .
x=0
y=3
Q4. Find errors, if any, in the following function definitions: [LO 10.3 H]
(a)
void abc (int a, int b) {
int c;
....
return (c);
}
(b)
int abc (int a, int b) {
....
....
}
(c)
int abc (int a, int b) {
U24EE082
double c = a + b;
return (c);
}
(d)
void abc (void) {
....
....
return;
}
(e)
int abc(void)
{
....
....
}
Ans.
(a) Error because function type is void and it is does not return any
value.
(b) Error because function type is int and it is return value but in this
question function does not return any value .
(c) Correct
(d) Correct
(e) Error because function type is int and it is return value but in this
question function does not return any value .
U24EE082
double ans(double x,int n){
double p,q,r,y,z;
if(n==1){
return x;
}
p= pow(-1,n-1);
q=pow(x,2*n-1);
r=factorial(2*n-1);
y=(p*q)/r;
z=y+ans(x,n-1);
return z;
}
int main(){
float x;
int n;
printf("x=");
scanf("%f",&x);
printf("n=");
scanf("%d",&n);
printf("%f",ans(x,n));
return 0;
}
OUTPUT :
x=1
n=3
0.841667
U24EE082
if(n==0){
return;
}
printf("%d ",c);
febo(b,c,n-1);
}
int main(){
int a=1,b=1, n;
printf("n=");
scanf("%d",&n);
printf("%d %d ",a,b);
febo(1,1,n);
return 0;
}
OUTPUT:
n=5
1 1 2 3 5 8 13
n=10
1 1 2 3 5 8 13 21 34 55 89 144
n=15
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
Q7. Design and code an interactive modular program that will use
functions to a matrix of m by n size, compute column averages and row
averages, and then print the entire matrix with averages shown in
respective rows and columns.
Ans.
#include <stdio.h>
void matrix() {
int n, m;
printf("Enter rows of the matrix = ");
scanf("%d", &n);
printf("Enter columns of the matrix = ");
scanf("%d", &m);
int arr[n][m];
float arr1[n+1][m+1];
U24EE082
for (int j = 0; j < m; j++) {
printf("Enter element at idx [%d][%d] = ", i, j);
scanf("%d", &arr[i][j]);
arr1[i][j] = arr[i][j];
}
}
float total = 0;
for (int i = 0; i < n; i++) {
total += arr1[i][m];
}
arr1[n][m] = total / n;
U24EE082
int main() {
matrix();
return 0;
}
OUTPUT:
Enter rows of the matrix = 3
Enter columns of the matrix = 3
Enter element at idx [0][0] = 1
Enter element at idx [0][1] = 2
Enter element at idx [0][2] = 3
Enter element at idx [1][0] = 4
Enter element at idx [1][1] = 5
Enter element at idx [1][2] = 6
Enter element at idx [2][0] = 7
Enter element at idx [2][1] = 8
Enter element at idx [2][2] = 9
int main(){
float a;
printf("Enter value: ");
scanf("%f",&a);
U24EE082
printf("%.2f", round2(a));
return 0;
}
OUTPUT:
Enter value: 151.5678
151.57
U24EE082
Assignment 5
Q3. Which one of the following is the correct way of declaring a pointer
to a function? Why?
(a) int ( *p) (void) ;
(b) int *p (void);
Ans.
(a) int ( *p) (void) ;
This declares p as a pointer to a function that returns an int.
U24EE082
(b)error
U24EE082
(c)error
(d)correct
(e)correct
(f)error
(g) correct
Q5. Write a function using pointers to add two matrices and return the
resultant matrix to the calling function. A structure tag name abc is used
to declare and initialize the structure variables of type struct abc in the
following statements. Which of them is incorrect? Why? Assume that the
structure abc has three members, int, float, and char in that order.
(a) struct a,b,c;
(b) struct abc a,b,c
(c) abc x,y,z;
(d) struct abc a[ ];
(e) struct abc a = { };
(f) struct abc = b, { 1+2, 3.0, “xyz”}
(g) struct abc c = {4,5,6};
(h) struct abc a = 4, 5.0, “xyz”;
Ans.
#include <stdio.h>
U24EE082
int main() {
int rows = 2, cols = 3;
printf("Resultant Matrix:\n");
printMatrix(&result[0][0], rows, cols);
return 0;
}
OUTPUT:
Resultant Matrix:
8 10 12
14 16 18
U24EE082
Q7. Define a structure data type named date containing three integer
members day, month, and year. Develop an interactive modular program
to perform the following tasks:
• To read data into structure members by a function
• To validate the date entered by another function
• To print the date in the format
April 29, 2002
by a third function.
The input data should be three integers 29, 4, and 2002 corresponding
to day, month, and year.Examples of invalid data:
31, 4, 2002 – April has only 30 days
29, 2, 2002 – 2002 is not a leap year
Ans.
#include <stdio.h>
#include <stdbool.h>
// Structure definition
struct date {
int day;
int month;
int year;
};
// Function declarations
void readDate(struct date *d);
bool isLeapYear(int year);
bool isValidDate(struct date d);
void printDate(struct date d);
int main() {
struct date d;
readDate(&d);
if (isValidDate(d)) {
printDate(d);
} else {
printf("Invalid date entered!\n");
}
return 0;
U24EE082
}
return true;
}
OUTPUT:
Enter day, month, and year: 5 2 2016
February 5, 2016
U24EE082
U24EE082