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

C Program

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

C Program

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

CS102.

3 Programming in C Language
1. Display your name and school name in two separate lines.
c

#include <stdio.h>
int main() {
printf("Your Name\n");
printf("Your School Name\n");
return 0;
}

2. Display the following output using printf() statements:


c

#include <stdio.h>
int main() {
printf("*\n");
printf("**\n");
printf("***\n");
printf("****\n");
printf("*****\n");
return 0;
}

3. Input values for int, float, double, and char data types and display the value of each
variable.
c

#include <stdio.h>
int main() {
int i;
float f;
double d;
char c;
printf("Enter an integer: ");
scanf("%d", &i);
printf("Enter a float: ");
scanf("%f", &f);
printf("Enter a double: ");
scanf("%lf", &d);
printf("Enter a character: ");
scanf(" %c", &c);
printf("Integer: %d\n", i);
printf("Float: %f\n", f);
printf("Double: %lf\n", d);
printf("Character: %c\n", c);
return 0;
}

4. Input two integers and display the total.


c

#include <stdio.h>
int main() {
int a, b, total;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
total = a + b;
printf("Total: %d\n", total);
return 0;
}

5. Input two numbers with decimals and display the average with decimals.
c

#include <stdio.h>
int main() {
float a, b, avg;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
avg = (a + b) / 2;
printf("Average: %f\n", avg);
return 0;
}

6. Input a student name, birth year, and display student name with age.
c

#include <stdio.h>
int main() {
char name[50];
int birthYear, age;
printf("Enter student name: ");
scanf("%s", name);
printf("Enter birth year: ");
scanf("%d", &birthYear);
age = 2024 - birthYear; // Assuming current year is 2024
printf("Student Name: %s, Age: %d\n", name, age);
return 0;
}

7. Input two numbers, swap the values and display the output (before swap and after swap).
c

#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("Before swap: a = %d, b = %d\n", a, b);
temp = a;
a = b;
b = temp;
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}

8. Execute the given code and analyze the output.


c

#include <stdio.h>
int main() {
printf("The color: %s\n", "blue");
printf("First number: %d\n", 12345);
printf("Second number: %04d\n", 25);
printf("Third number: %i\n", 1234);
printf("Float number: %3.2f\n", 3.14159);
printf("Hexadecimal: %x\n", 255);
printf("Octal: %o\n", 255);
printf("Unsigned value: %u\n", 150);
printf("Just print the percentage sign %%\n");
return 0;
}

• Output Analysis:
• The color: blue
• First number: 12345
• Second number: 0025
• Third number: 1234
• Float number: 3.14
• Hexadecimal: ff
• Octal: 377
• Unsigned value: 150
• Just print the percentage sign %

You might also like