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

c program

The document contains two C programs: one for displaying ASCII values of characters and strings, and another for calculating the squares of numbers up to a user-defined positive integer. The first program prompts the user for a character and a string, then prints their ASCII values along with the ASCII values of the alphabet. The second program welcomes the user, prompts for a positive integer, calculates the squares of numbers from 1 to that integer, and provides a summary of the calculations.

Uploaded by

moganapriya517
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

c program

The document contains two C programs: one for displaying ASCII values of characters and strings, and another for calculating the squares of numbers up to a user-defined positive integer. The first program prompts the user for a character and a string, then prints their ASCII values along with the ASCII values of the alphabet. The second program welcomes the user, prompts for a positive integer, calculates the squares of numbers from 1 to that integer, and provides a summary of the calculations.

Uploaded by

moganapriya517
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

ASSIGNING THE ASCII VALUE

PROGRAM:

#include <stdio.h>

#include <string.h>

void printASCII(char c) {

printf("Character: %c, ASCII Value: %d\n", c, c);

void printStringASCII(char str[]) {

printf("\nASCII values for string: \"%s\"\n", str);

for (int i = 0; i < strlen(str); i++) {

printf("Character: %c, ASCII: %d\n", str[i], str[i]);

void printAlphabetASCII() {

printf("\nASCII values for Alphabets:\n");

for (char ch = 'A'; ch <= 'Z'; ch++) {

printf("%c: %d\t", ch, ch);

printf("\n");

for (char ch = 'a'; ch <= 'z'; ch++) {

printf("%c: %d\t", ch, ch);

printf("\n");

int main() {

char inputChar;

char inputString[100];
printf("Enter a character to find its ASCII value: ");

scanf(" %c", &inputChar);

printASCII(inputChar);

printf("\nEnter a string to display its ASCII values: ");

fgets(inputString, sizeof(inputString), stdin);

inputString[strcspn(inputString, "\n")] = 0; // Remove newline character

printStringASCII(inputString);

printAlphabetASCII();

return 0;

OUTPUT:

Output:

Enter a character to find its ASCII value: Character: A, ASCII Value: 65

Enter a string to display its ASCII values:


ASCII values for string: ""

ASCII values for Alphabets:


A: 65 B: 66 C: 67 D: 68 E: 69 F: 70 G: 71
H: 72 I: 73 J: 74 K: 75 L: 76 M: 77 N: 78
O: 79 P: 80 Q: 81 R: 82 S: 83 T: 84 U: 85
V: 86 W: 87 X: 88 Y: 89 Z: 90

a: 97 b: 98 c: 99 d: 100 e: 101 f: 102 g: 103


h: 104 i: 105 j: 106 k: 107 l: 108 m: 109 n: 110
o: 111 p: 112 q: 113 r: 114 s: 115 t: 116 u: 117
v: 118 w: 119 x: 120 y: 121 z: 122
2. SQUARE OF TWO NUMBERS: USING FOR LOOP

PROGRAM:

#include <stdio.h>

void clearInputBuffer() {

while (getchar() != '\n');

void printSquares(int n) {

for (int i = 1; i <= n; i++) {

printf("%d squared = %d\n", i, i * i);

int getUserInput() {

int n;

printf("Enter a positive integer: ");

while (scanf("%d", &n) != 1 || n <= 0) {

clearInputBuffer(); // Clear any invalid input

printf("Invalid input. Please enter a positive integer: ");

return n;

int main() {

int n;

printf("Welcome to the Square Calculator!\n");

printf("This program will calculate the square of numbers from 1 up to the number you
provide.\n\n");

n = getUserInput();

printf("\nCalculating squares of numbers from 1 to %d:\n", n);


printSquares(n);

printf("\nProgram Summary:\n");

printf("You entered: %d\n", n);

printf("The squares of numbers from 1 to %d have been calculated.\n", n);

printf("\nThank you for using the Square Calculator. Have a great day!\n");

return 0;

}
OUTPUT:
Welcome to the Square Calculator!
This program will calculate the square of numbers from 1 up to the
number you provide.

Enter a positive integer: 12


Calculating squares of numbers from 1 to 12:
1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25
6 squared = 36
7 squared = 49
8 squared = 64
9 squared = 81
10 squared = 100
11 squared = 121
12 squared = 144

Program Summary:
You entered: 12
The squares of numbers from 1 to 12 have been calculated.

Thank you for using the Square Calculator. Have a great day

You might also like