0% found this document useful (0 votes)
4 views16 pages

Program File C With Outputs

The document contains a series of C programming examples that demonstrate various programming concepts, including calculating sums, finding the greatest and smallest of three numbers, computing areas, and generating patterns. Each example includes the code, expected output, and explanations of the functionality. Topics covered range from basic arithmetic operations to control structures and loops.

Uploaded by

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

Program File C With Outputs

The document contains a series of C programming examples that demonstrate various programming concepts, including calculating sums, finding the greatest and smallest of three numbers, computing areas, and generating patterns. Each example includes the code, expected output, and explanations of the functionality. Topics covered range from basic arithmetic operations to control structures and loops.

Uploaded by

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

Q: 1 write a program that display the sum of first 100 even numbers.

#include <stdio.h>

#include <conio.h>

void main() {

int sum = 0, i;

for(i = 2; i <= 200; i += 2)

sum += i;

printf("Sum = %d", sum);

getch();

OUTPUT : Sum = 10100

2. Write a Program to display Numbers 1 to 100 with their squares

#include <stdio.h>

#include <conio.h>

void main() {

int i;

for(i = 1; i <= 100; i++)

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

getch();

}
Output : 1^2 = 1

2^2 = 4

3^2 = 9

...

100^2 = 10000

3. Write a Program to display Sum of series 1 + 1/3 + 1/5 + ... + 1/99

#include <stdio.h>

#include <conio.h>

void main() {

float sum = 0;

int i;

for(i = 1; i <= 99; i += 2)

sum += 1.0 / i;

printf("Sum = %.4f", sum);

getch();

OutPUT:

Sum = 3.2950

4. Program: Greatest of three numbers

#include <stdio.h>

#include <conio.h>
void main() {

int a, b, c;

printf("Enter three numbers: ");

scanf("%d%d%d", &a, &b, &c);

if(a > b && a > c)

printf("Greatest = %d", a);

else if(b > c)

printf("Greatest = %d", b);

else

printf("Greatest = %d", c);

getch();

OutPUT: Enter three numbers: 45 78 12

Greatest = 78

5. Program: Smallest of three numbers

#include <stdio.h>

#include <conio.h>

void main() {

int a, b, c;

printf("Enter three numbers: ");

scanf("%d%d%d", &a, &b, &c);

if(a < b && a < c)


printf("Smallest = %d", a);

else if(b < c)

printf("Smallest = %d", b);

else

printf("Smallest = %d", c);

getch();

Output: Enter three numbers: 45 78 12

Smallest = 12

6. Area of triangle

#include<stdio.h>

#include<conio.h>

void main() {

float base, height, area;

printf("Enter base and height: ");

scanf("%f%f", &base, &height);

area = 0.5 * base * height;

printf("Area of triangle = %.2f", area);

getch();

Output: Enter base and height: 10 5

Area of triangle = 25.00


7. Sum and average of first 10 even numbers using FOR loop

#include<stdio.h>

#include<conio.h>

void main() {

int i, sum = 0;

float avg;

for(i = 2; i <= 20; i += 2)

sum += i;

avg = sum / 10.0;

printf("Sum = %d\nAverage = %.2f", sum, avg);

getch();

Output: Sum = 110

Average = 11.00

8. Factorial of a number

#include<stdio.h>

#include<conio.h>

void main() {

int n, i;

long fact = 1;

printf("Enter a number: ");


scanf("%d", &n);

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

fact *= i;

printf("Factorial = %ld", fact);

getch();

Output : Enter a number: 5

Factorial = 120

9. Product of first 10 even numbers using FOR loop

#include<stdio.h>

#include<conio.h>

void main() {

int i;

long product = 1;

for(i = 2; i <= 20; i += 2)

product *= i;

printf("Product = %ld", product);

getch();

output: Product = 3715891200


10. Square and cube of a number

#include<stdio.h>

#include<conio.h>

void main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Square = %d\nCube = %d", num * num, num * num * num);

getch();

Output: Enter a number: 4

Square = 16

Cube = 64

11. Circumference of a circle

#include<stdio.h>

#include<conio.h>

void main() {

float r, c;

printf("Enter radius: ");

scanf("%f", &r);

c = 2 * 3.14 * r;

printf("Circumference = %.2f", c);


getch();

output: Enter radius: 7

Circumference = 43.96

12. Area of rectangle

#include<stdio.h>

#include<conio.h>

void main() {

float length, width, area;

printf("Enter length and width: ");

scanf("%f%f", &length, &width);

area = length * width;

printf("Area = %.2f", area);

getch();

Output: Enter length and width: 5 6

Area = 30.00

13. Product of first 10 odd numbers

#include<stdio.h>

#include<conio.h>
void main() {

int i;

long product = 1;

for(i = 1; i <= 19; i += 2)

product *= i;

printf("Product = %ld", product);

getch();

Product = 654729075

14. Numbers from 100 to 1 using WHILE

#include<stdio.h>

#include<conio.h>

void main() {

int i = 100;

while(i >= 1) {

printf("%d ", i);

i--;

getch();

OutPut: 100 99 98 ... 1


15. Numbers from 1 to 100 using WHILE

#include<stdio.h>

#include<conio.h>

void main() {

int i = 1;

while(i <= 100) {

printf("%d ", i);

i++;

getch();

OutPut : 1 2 3 ... 100

16. Leap Year Checker

#include<stdio.h>

#include<conio.h>

void main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


printf("Leap Year");

else

printf("Not a Leap Year");

getch();

Output: Enter a year: 2000

Leap Year

17. Fahrenheit to Celsius

#include<stdio.h>

#include<conio.h>

void main() {

float f, c;

printf("Enter temperature in Fahrenheit: ");

scanf("%f", &f);

c = 5.0 / 9 * (f - 32);

printf("Celsius = %.2f", c);

getch();

OutPut:

Enter temperature in Fahrenheit: 98.6

Celsius = 37.00
18. Vowel or Consonant

#include<stdio.h>

#include<conio.h>

void main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u' ||

ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U')

printf("Vowel");

else

printf("Consonant");

getch();

Output:

Enter a character: a

Vowel

19. Prime Number Check

#include<stdio.h>

#include<conio.h>

void main() {
int num, i, flag = 1;

printf("Enter a number: ");

scanf("%d", &num);

if(num <= 1)

flag = 0;

for(i = 2; i <= num/2; i++) {

if(num % i == 0) {

flag = 0;

break;

if(flag)

printf("Prime Number");

else

printf("Not a Prime Number");

getch();

Output: Enter a number: 7

Prime Number
20. Reverse Hash Pattern

#include<stdio.h>

#include<conio.h>

void main() {

int i, j;

for(i = 5; i >= 1; i--) {

for(j = 1; j <= i; j++)

printf("# ");

printf("\n");

getch();

OutPut: # # # # #

####

###

##

21. @ Triangle Pattern

#include<stdio.h>

#include<conio.h>
void main() {

int i, j;

for(i = 1; i <= 5; i++) {

for(j = 1; j <= i; j++)

printf("@ ");

printf("\n");

getch();

OutPut:

@@

@@@

@@@@

@@@@@
22. Number Triangle Pattern

#include<stdio.h>

#include<conio.h>

void main() {

int i, j;

for(i = 1; i <= 5; i++) {

for(j = 1; j <= i; j++)

printf("%d ", j);

printf("\n");

getch();

Output:

12

123

1234

12345

You might also like