Question
- WAP to get your name, address, and display using unformatted I/O.
- WAP to get a character from the user using unformatted I/O and display the ASCII value of the entered character.
- 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 |
- 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
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;
}
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
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;
}
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.
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;
}
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;
}
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
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.
![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]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMxpcqmux9tyWfN5VatCs9eT87rLRCp0f-SHQ1r_BuaLJsXZ5OM56YsMggw9wGgoChhAqWKByL3E61nQHPc1zBMm45xamoYF2ggeLp4_uLgj9tGPiICrV_J5cCgG0XZmI1eXNN7HQZTTIavROMv0gjnaoJt8ZIpZMPLGRtmcdz4ldTmkehmsT5Scz-/s16000-rw/C%20Programming%20(3).png)