Facebook Experiences Widespread Outages on March 5 2024

ERC First Semester C Programming LAB SHEET NO.4 [To be familiar with Unformatted and Formatted I/0]

ERC First Semester C Programming LAB SHEET NO.4 [To be familiar with Unformatted and Formatted I/0]

Question

  1. WAP to get your name, address, and display using unformatted I/O.
  2. WAP to get a character from the user using unformatted I/O and display the ASCII value of the entered character.
  3. WAP to display the output as [take a=15, b=20.43, c=35]:
    A=              15|15             |              15|15             |              15|15             |              15|15             |              15|15             |
    B=           20.43|20.43          |           20.43|20.43          |           20.43|20.43          |           20.43|20.43          |           20.43|20.43          |
    C=              35|35             |              35|35             |              35|35             |              35|35             |              35|35             |
    
  4. WAP to display the output as below using formatted I/O [take char a[]="I Love Nepal"].
  5. I
    I L
    I LO
    I LOV
    I LOVE
    I LOVE N
    I LOVE NE
    I LOVE NEP
    I LOVE NEPA
    I LOVE NEPAL

Complete Labsheet 4

Objective: To be familiar with Unformatted and Formatted I/0

Solution:

1. WAP to get your name, address, and display using unformatted I/O.

C Program: Code

#include <stdio.h>
int main()
{
char na[50],ad[50];
puts("Enter Name:");
gets(na);
puts("Enter Address:");
gets(ad);
puts(na);
puts(ad);
return 0;
}

Output:

Enter Name:
Psynal
Enter Address:
Dharan
Psynal
Dharan

Discussion and Conclusion

Discussion:
The code prompts the user to enter their name and address, and then displays them using the puts() function. However, it uses the unsafe function gets() to read user input, which can lead to buffer overflow vulnerabilities.
Conclusion:
The code successfully takes user input for name and address and displays them. However, the use of gets() function is discouraged due to potential security risks, and it is recommended to use safer alternatives like fgets() to read user input.

2. WAP to get a character from the user using unformatted I/O and display the ASCII value of the entered character.

C Program: Code

#include<stdio.h>
int main()
{
char a;
puts("Enter any character:");
a=getchar();
printf("ASCII value of %c is %d.",a,a);
return 0;
}

Output:

Enter any character:
A
ASCII value of A is 65.

Discussion and Conclusion

Discussion:
This code prompts the user to enter a character, reads the input using getchar(), and then prints the ASCII value of the character using printf().
Conclusion:
The code successfully reads a character from the user and displays its corresponding ASCII value.

3. WAP to display the output as [take a=15, b=20.43, c=35]:
A=              15|15             |              15|15             |              15|15             |              15|15             |              15|15             |
B=           20.43|20.43          |           20.43|20.43          |           20.43|20.43          |           20.43|20.43          |           20.43|20.43          |
C=              35|35             |              35|35             |              35|35             |              35|35             |              35|35             |

C Program: Code

#include<stdio.h>
int main()
{
int a=15,c=35;
float b=20.43;
printf("A= %15d|%-15d| %15d|%-15d| %15d|%-15d| %15d|%-15d| %15d|%-15d|",a,a,a,a,a,a,a,a,a,a);
printf("\n");
printf("B= %15.2f|%-15.2f| %15.2f|%-15.2f| %15.2f|%-15.2f| %15.2f|%-15.2f| %15.2f|%-15.2f|",b,b,b,b,b,b,b,b,b,b);
printf("\n");
printf("C= %15d|%-15d| %15d|%-15d| %15d|%-15d| %15d|%-15d| %15d|%-15d|",c,c,c,c,c,c,c,c,c,c);
return 0;
}

Output:

A=              15|15             |              15|15             |              15|15             |              15|15             |              15|15             |
B=           20.43|20.43          |           20.43|20.43          |           20.43|20.43          |           20.43|20.43          |           20.43|20.43          |
C=              35|35             |              35|35             |              35|35             |              35|35             |              35|35             |

Discussion and Conclusion

Discussion:
The code uses printf() to display the variables a, b, and c in a formatted manner. It aligns the values using width modifiers and adjusts the precision for floating-point values.
Conclusion:
The code uses printf() to display the variables a, b, and c in a formatted manner. It aligns the values using width modifiers and adjusts the precision for floating-point values.

4. WAP to display the output as below using formatted I/O [take char a[]="I Love Nepal"].
I
I L
I LO
I LOV
I LOVE
I LOVE N
I LOVE NE
I LOVE NEP
I LOVE NEPA
I LOVE NEPAL

C Program: Code

#include<Stdio.h>
int main()
{
char a[]=("I LOVE NEPAL");
printf("%.1s\n",a);
printf("%.3s\n",a);
printf("%.4s\n",a);
printf("%.5s\n",a);
printf("%.6s\n",a);
printf("%.8s\n",a);
printf("%.9s\n",a);
printf("%.10s\n",a);
printf("%.11s\n",a);
printf("%.12s\n",a);
return 0;
}

Output:

I
I L
I LO
I LOV
I LOVE
I LOVE N
I LOVE NE
I LOVE NEP
I LOVE NEPA
I LOVE NEPAL

Discussion and Conclusion

Discussion:
The code utilizes printf() to format and display the variables a, b, and c. It uses width modifiers to align the values and adjust precision for floating-point numbers.
Conclusion:
The code utilizes printf() to format and display the variables a, b, and c. It uses width modifiers to align the values and adjust precision for floating-point numbers.
C Programming Lab
A free online educational resource provider.

Post a Comment

We get inspired from your single comment.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing Lantro UI, you agreed to use cookies in agreement with the Lantro UI's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.