0% found this document useful (0 votes)
6 views3 pages

Labdoc 2

The document contains four C programming examples authored by G. Radhika, demonstrating variable declaration and initialization, user input handling, value swapping, and the use of unformatted functions. Each program includes code snippets and corresponding outputs to illustrate their functionality. The examples cover basic programming concepts suitable for beginners.

Uploaded by

sathvikayerra7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Labdoc 2

The document contains four C programming examples authored by G. Radhika, demonstrating variable declaration and initialization, user input handling, value swapping, and the use of unformatted functions. Each program includes code snippets and corresponding outputs to illustrate their functionality. The examples cover basic programming concepts suitable for beginners.

Uploaded by

sathvikayerra7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Note: Execute and write in observation and record (Date:30-09-24)

Q1) This basic program demonstrates the declaration and initialization of variables of different data
types?
Program:
/*Author:G.Radhika
30-09-2024
declaration and initialization of variables */
#include <stdio.h>

int main() {
int a = 10; // Integer variable
float b = 20.5; // Float variable
char c = 'A'; // Character variable

printf("Integer: %d\n", a);


printf("Float: %.2f\n", b);
printf("Character: %c\n", c);

return 0;
}
Output:
Integer: 10
Float: 20.50
Character: A
Q2) This program takes input from the user and stores the values in variables?
Program:
/*Author:G.Radhika
27-09-2024
input from the user and stores the values in variables */
#include <stdio.h>

int main() {
int num;
float decimal;
char character;

printf("Enter an integer: ");


scanf("%d", &num);
printf("Enter a float: ");
scanf("%f", &decimal);
printf("Enter a character: ");
scanf(" %c", &character);

printf("You entered:\n");
printf("Integer: %d\n", num);
printf("Float: %.2f\n", decimal);
printf("Character: %c\n", character);
return 0;
}
Output:
Enter an integer: 12
Enter a float: 3.45
Enter a character: A
You entered:
Integer: 12
Float: 3.45
Character: A
Q3) This program swaps the values of two variables using a temporary variable?
Program:
/*Author:G.Radhika
30-09-2024
swaps the values of two variables */
#include <stdio.h>

int main() {
int x = 5, y = 10, temp;

printf("Before swapping: x = %d, y = %d\n", x, y);

// Swapping the values


temp = x;
x = y;
y = temp;

printf("After swapping: x = %d, y = %d\n", x, y);

return 0;
}
Output:
Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5
Q4)unformatted functions?
Program:
/*Author:G.Radhika
30-09-2024
Example program for unformatted functions */
#include <stdio.h>
#include <conio.h>

int main() {
char ch;
char str[100];
printf("Press any key (hidden with getch()): ");
ch = getch();
printf("\nYou pressed (hidden): %c\n", ch);

printf("Press any key (displayed with getche()): ");


ch = getche();
printf("\nYou pressed (with echo): %c\n", ch);

printf("\nUsing putch() to display character: ");


putch(ch);
printf("\n");

printf("Using putchar() to display character: ");


putchar(ch);
printf("\n");

printf("Enter a string using gets(): ");


gets(str);
printf("You entered: ");
puts(str);

printf("Enter a character using getchar(): ");


ch = getchar();
printf("You entered (using putchar()): ");
putchar(ch);
printf("\n");

return 0;
}
Output:
Press any key (hidden with getch()):
You pressed (hidden): f
Press any key (displayed with getche()): w
You pressed (with echo): w

Using putch() to display character: w


Using putchar() to display character: w
Enter a string using gets(): radhika
You entered: radhika
Enter a character using getchar(): n
You entered (using putchar()): n

You might also like