0% found this document useful (0 votes)
2 views

Input Output

The document provides a series of C programming exercises that demonstrate basic input and output operations. Each exercise includes a code snippet that performs a specific task, such as printing a name, reading and displaying numbers of various types, and converting between number systems. The document serves as a practical guide for beginners to learn fundamental programming concepts in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Input Output

The document provides a series of C programming exercises that demonstrate basic input and output operations. Each exercise includes a code snippet that performs a specific task, such as printing a name, reading and displaying numbers of various types, and converting between number systems. The document serves as a practical guide for beginners to learn fundamental programming concepts in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Input/output

1. Write a program that prints your name.

Code:

#include<stdio.h>
int main()
{
printf("C language Lab"); // add your name here
return 0;
}

Output:

2. Write a program that an integer variable n contains 5, and then print the
value of n.

Code:

#include<stdio.h>
int main()
{
int n = 5;
printf("The value of n is: %d\n", n);
return 0;
}
Output:
3. Write a program that read and display an integer number.

Code:

#include<stdio.h>
int main()
{
int n;
printf("Enter an integer number: ");
scanf("%d",&n);
printf("The value of n is: %d\n", n);
return 0;
}

Output:
4. Write a program that read and display floating pointing number.

Code:

#include<stdio.h>
int main()
{
float n;
printf("Enter an floating point number: ");
scanf("%f",&n);
printf("The value of n is: %f\n", n);
return 0;
}

Output:
5. Write a program that read and display long number.

Code:
#include <stdio.h>
int main()
{
unsigned int a=0;
unsigned long long int b=0;

printf("Enter value of a: ");


scanf("%u",&a);
printf("Enter value of b: ");
scanf("%llu",&b);

printf("a=%u\n",a);
printf("b=%llu\n",b);
return 0;
}

Output:
6. Write a program that read and display double number.

Code:
#include<stdio.h>
int main()
{
double n;
printf("Enter a double number: ");
scanf("%lf",&n);
printf("The value of n is: %f\n", n);
return 0;
}

Output:
7. Write a program that read and display any character.

Code:
#include<stdio.h>
int main()
{
char ch;
printf("Enter a character number: ");
scanf("%c",&ch);
printf("The Entered character is: %c\n", ch);
return 0;
}

Output:
8. Write a program that read ASCII value and display equivalent character.

Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter a ASCII value(0 to 255): ");
scanf("%d",&num);
printf("Equivalent Character: %c",num);
return 0;
}

Output:

9. Write a program that read any character and display equivalent ASCII value.

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

Output:
10. Write a program that read any lower case character and display its upper
case.

Code:
#include <stdio.h>;
#include <ctype.h>;
int main()
{
int lower, upper;
printf("Enter lower case letter: ");
lower = getchar();
upper = toupper(lower);
printf("Upper case: ");
putchar(upper);
return 0;
}
Output:
11. Write a program that read any upper case character and display its lower
case.
Code:
#include <stdio.h>;
#include <ctype.h>;
int main()
{
int lower, upper;
printf("Enter upper case letter: ");
upper = getchar();
lower = tolower(upper);
printf("Lower case: ");
putchar(lower);
return 0;
}
Output:
12. Write a program that read any decimal number and display equivalent
octal number.

Code:
#include <stdio.h>
int main()
{
long decimalnum, remainder, quotient;
int octalNumber[100], i = 1, j;
printf("Enter the decimal number: ");
scanf("%ld", &decimalnum);
quotient = decimalnum;

while (quotient != 0)
{
octalNumber[i++] = quotient % 8;
quotient = quotient / 8;
}
printf("Equivalent octal value of decimal no %d: ", decimalnum);

for (j = i - 1; j > 0; j--)


{
printf("%d", octalNumber[j]);
}
return 0;
}

Output:
13. Write a program that read any decimal number and display equivalent
hexadecimal number.
Code:
#include<stdio.h>
int main()
{
int n ;
printf("Enter a decimal number: ");
scanf("%d", &n);
char hexaDeciNum[100];
int i = 0;
while(n!=0)
{
int temp = 0;
temp = n % 16;
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
for(int j=i-1; j>=0; j--)
{
printf("%c", hexaDeciNum[j]);
}
return 0;
}
Output:
14. Write a program that read any octal number and display equivalent
decimal number.

Code:
#include<stdio.h>
#include<math.h>
int main()
{
int octnum, decnum=0;
int i=0;
printf("Enter any Octal Number : ");
scanf("%d",&octnum);
while(octnum!=0)
{
decnum = decnum + (octnum%10) * pow(8, i);
i++;
octnum=octnum/10;
}
printf("Equivalent Decimal Value : %d ",decnum);
return 0;
}

Output:
15. Write a program that read any hexadecimal number and display equivalent
decimal number.

Code:
#include<stdio.h>
#include<string.h>

int main()
{
char hexNum[20];
printf("Enter Hexadecimal Number: ");
scanf("%s", hexNum);
int len = strlen(hexNum);
int base = 1;
int dec_val = 0;

for (int i=len-1; i>=0; i--)


{
if (hexNum[i]>='0' && hexNum[i]<='9')
{
dec_val += (hexNum[i] - 48)*base;
base = base * 16;
}
else if (hexNum[i]>='A' && hexNum[i]<='F')
{
dec_val += (hexNum[i] - 55)*base;
base = base*16;
}
}
printf("Decimal is: %d", dec_val);
return 0;
}

Output:
16.Write a program that read and display your name.

Code:
#include<stdio.h>
#include<string.h>

int main()
{
char Name[50];
printf("Enter Your Nick-Name: ");
scanf("%s", Name);

printf("Hello %s!", Name);


return 0;
}

Output:
17.Write a program that read and display a line of text.

Code:
#include <stdio.h>
int main()
{
char line[100];
printf("Enter a string/line: ");
gets(line); // read string
printf("Output is: ");
puts(line); // display string
return 0;
}

Output:

You might also like