c file-1
c file-1
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Output
Hello World
#include <stdio.h>
int main()
{
printf("Ramavtar");
return 0;
}
Output
Ramavtar
1
3. Write a program to print an integer entered by the user
#include <stdio.h>
int main() {
int number;
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
Output
Enter an integer: 87
You entered: 87
#include <stdio.h>
int main() {
2
5. Write a program to check whether a number is prime or not.
#include <stdio.h>
int main() {
int n,c;
printf("enter the number: ");
scanf("%d",&n);
if (n <= 1)
printf("%d is NOT prime\n", n);
else {
for (int i = 1; i <= n; i++) {
if (n % i == 0)
c++;
}
if (c > 2)
printf("%d is NOT prime\n", n);
// else it is prime
else
printf("%d is prime", n);
}
return 0;
}
Output 1
enter the number: 7
7 is prime
Output 2
enter the number: 9
9 is not prime
3
#include <stdio.h>
int main()
{
float n,c;
printf("enter two numbers :");
scanf("%f %f", &n, &c);
printf("%f", n*c);
return 0;
}
Output
Enter two numbers : 589.59
2.456
1448.033203
#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 1
Enter a character: A
ASCII value of A = 65
Output 2
Enter a character: a
ASCII value of a = 97
8. Write a C program to Swap Two Numbers.
4
#include<stdio.h>
int main(){
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
#include<stdio.h>
5
int main()
{
float fahrenheit, celsius;
//get the limit of fibonacci series
printf("Enter Fahrenheit: ");
scanf("%f",&fahrenheit);
celsius = (fahrenheit - 32)*5/9;
printf("Celsius: %f ", celsius);
return 0;
}
Output
Enter Fahrenheit: 9
Celsius: -12.777778
10. Write a C program to Find the Size of int, float, double, and char.
#include<stdio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));
return 0;
}
Output
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
11. Write a C program to Add Two Complex Numbers .
#include <stdio.h>
6
typedef struct complex {
float real;
float imag;
} complex;
complex add(complex n1, complex n2);
int main() {
complex n1, n2, result;
7
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
return 0;
}
Output 1
Enter a positive integer: 29
29 is a prime number.
Output 2
Enter a positive integer: 8
8 is not a prime number.
13. Write a C program to Find Simple Interest
# include <conio.h>
# include <stdio.h>
8
# include <stdlib.h>
int main(){
return 0;
}
Output:
Enter the principal: 1000
Enter the rate: 2
Enter the time: 23
The Simple interest is 460
9
int main()
{
float principle, rate, time, CI;
return 0;
}
Output
Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4
Compound Interest = 1333.099243
10
void main() {
/* Variable Declaration. */
float lnth, wdth, perimeter, area;
int main()
11
{
int num;
if(num > 0)
{
printf("Number is POSITIVE");
}
if(num < 0)
{
printf("Number is NEGATIVE");
}
if(num == 0)
{
printf("Number is ZERO");
}
return 0;
}
Output 1
Enter any number: 9
Number is POSITIVE
Output 2
Enter any number: -5
Number is NEGATIVE
Output 3
Enter any number: 0
Number is ZERO
17. Write a C program to Check Whether Number is Even or Odd.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
12
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
Output 1
Enter an integer: 7
7 is odd.
Output 2
Enter an integer: 8
8 is even.
int main() {
14
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
return 0;
}
Output
Enter three different numbers: 25
95
65
15
for (i = 1; i <= n; ++i) {
sum += i;
}
16
if (c == 'U' || c == 'u') {
for (c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
} else if (c == 'L' || c == 'l') {
for (c = 'a'; c <= 'z'; ++c)
printf("%c ", c);
} else {
printf("Error! You entered an invalid character.");
}
return 0;
}
Output 1
Enter u to display uppercase alphabets.
Enter l to display lowercase alphabets.
u
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Output 2
Enter u to display uppercase alphabets.
Enter l to display lowercase alphabets.
l
abcdefghijklmnopqrstuvwxyz
17
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
return 0;
}
Output 1
Enter a year: 1900
1900 is not a leap year.
Output 2
Enter a year: 2012
2012 is a leap year.
18
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
Output
Enter an integer: 10
Factorial of 10 = 3628800
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
19
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
Output 1
Enter an operator (+, -, *, /): +
Enter two operands: 5.2
6.5
5.2 + 6.5 = 11.7
Output 2
Enter an operator (+, -, *, /): -
Enter two operands: 5.9
4.2
5.9 - 4.2 = 1.7
Output 3
Enter an operator (+, -, *, /): *
Enter two operands: 6.7
20
2.1
6.7 * 2.1 = 14.1
Output 4
Enter an operator (+, -, *, /): /
Enter two operands: 6.6
2.2
6.6 / 2.2 = 3.0
21
}
return 0;
}
Output
Enter an integer: 8
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
int i, n;
22
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
return 0;
}
Output
Enter the number of terms: 15
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
int main() {
23
// maximum number between n1 and n2 is stored in max
max = (n1 > n2) ? n1 : n2;
lcm = max;
return 0;
}
Output
Enter two positive integers: 521
720
The LCM of 521 and 720 is 375120.
while (originalNum != 0) {
24
// remainder contains the last digit
remainder = originalNum % 10;
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Output 1
Enter a three-digit integer: 371
371 is an Armstrong number.
Output 2
Enter a three-digit integer: 56
56 is not an Armstrong number.
25
// swap numbers if high < low
if (high < low) {
high += low;
low = high - low;
high -= low;
}
originalNumber = number;
return 0;
26
}
Output
Enter two numbers(intervals): 1
1000
Armstrong numbers between 1 and 1000 are: 2 3 4 5 6 7 8 9 153 370 371 407
int main() {
int n, reverse = 0, remainder, original;
original = n;
27
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
if (original % 10 == 0) {
printf("Reversed number = %d", reverse);
while (original % 10 == 0) {
printf("0");
original /= 10;
}
} else {
printf("Reversed number = %d", reverse);
}
return 0;
}
Output
Enter an integer: 2719
Reversed number = 9172
return 0;
}
Output 1
Enter an integer: 2002
2002 is a palindrome.
Output 2
Enter an integer: 2020
2020 is not a palindrome.
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
29
flag = 1;
return 0;
}
Output 1
Enter a positive integer: 7
7 is a prime number.
Output 2
Enter a positive integer: 8
8 is not a prime number.
33. Write a C program to Find All Factors of a Natural Number.
#include <stdio.h>
int main() {
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d ", i);
}
}
30
return 0;
}
Output
Enter a positive integer: 100
Factors of 100 are: 1 2 4 5 10 20 25 50 100
31
if (count <= rows - 1) {
printf("%d ", i + k);
++count;
} else {
++count1;
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 5
1
232
34543
4567654
567898765
32
return 0;
}
Output
Enter the number of rows: 5
1
12
123
1234
12345
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
33
for (int j = i; j >= 1; j--) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 5
543212345
4321234
32123
212
1
34
// number rows
for(i = 0; i < rows; i++)
{
// This for loop is used to
// identify number of columns
// based on the rows
for(j = 0; j <= i; j++)
{
// Printing character to get
// the required pattern
printf("%c ",character);
// Incrementing character value so
// that it will print the next character
character++;
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 4
A
BC
DEF
GHIJ
37. Write a C program to Print Number Pattern.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
// Upper half of the diamond
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
printf(" ");
}
for (int k = 1; k < (i * 2); k++) {
35
printf("%d", k);
}
printf("\n");
}
36