[go: up one dir, main page]

0% found this document useful (0 votes)
117 views29 pages

PROGRAM - 1 . Write A C Program To Display "This Is My First C Program"

This document contains programs written by Chetna for her computer engineering course. It includes 6 programs: 1) A simple "Hello World" program, 2) A program to print her name and age on separate lines, 3) A program to swap two variables with and without a third variable, 4) A program to display the size of different data types using sizeof, 5) Programs demonstrating implicit and explicit type casting, and 6) A program to perform basic math operations of addition, subtraction, multiplication, and division. For each program, the code and output are provided.

Uploaded by

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

PROGRAM - 1 . Write A C Program To Display "This Is My First C Program"

This document contains programs written by Chetna for her computer engineering course. It includes 6 programs: 1) A simple "Hello World" program, 2) A program to print her name and age on separate lines, 3) A program to swap two variables with and without a third variable, 4) A program to display the size of different data types using sizeof, 5) Programs demonstrating implicit and explicit type casting, and 6) A program to perform basic math operations of addition, subtraction, multiplication, and division. For each program, the code and output are provided.

Uploaded by

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

NAME = CHETNA

ROLL_NO=190010800013
COMPUTER ENGG.

PROGRAM_1=. Write a C program to display “This is my first C Program”.

#include <conio.h>
#include<stdio.h>
void main()
{
clrscr();
printf("This is my first C program!\n");
}

Output; This is my first C program


NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

PROGRAM_2= WRITE A PROGRAM YOUR NAME AND AGE IN DIFFERENT LINES.

#include<conio.h>

#include<stdio.h>

Void main()

Clrscr();

Printf(“name”);

Printf(“\n\nage”);

Getch();

OUTPUT= name

Age
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

PROGRAM_3= WRITE A PROGRAM TO SWAP VALUE OFTWO VARIABLE WITH OR WITHOUT USING
THIRD VARIABLE.

#include<conio.h>

#include<stdio.h>

Void main()

Clrscr();

Printf(“enter the value of a & b:”);

Scanf(“%d”,&a,&b);

a=a+b;

b=a-b;

a=a-b:

printf(“ after swapping the value of a&b%d%d”,a,b);

getch();

OUTPUT-= enter the value for a& b:4

After swapping the value of a&b54


NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

PROGRAM_4= WRITE A PROGRAM TO DISPLAY THE SIZE OF EYERY DATA TYPE


USING”SIZEOF”OPERATOR.

#include<conio.h>

#include<stdio.h>

Void main()

Int a;

Char b;

Float c;

Double d;

Printf(“%d byte\n”,sizeof(a));

Printf(“%d byte\n”,sizeof(b));

Printf(“%d byte\n”,sizeof(c));

Printf(“%d byte\n”,sizeof(d));

Getch();

OUTPUT=

2 BYTE

1 BYTE

4 BYTE

8 BYTE
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

PROGRAM_5= WRITE A PROGRAM TO SHOW THE USE OF IMPLICIT AND EXPLICIT DATA TYPE
CASTING.

1=IMPLICIT

#include<conio.h>

#include<stdio.h>

Void main()

Int a,b;

a=10;

char x;

x=’A’;

b=a+x;

printf(“the vcalue of b is:%d”,b);

OUTPUT=

The value of b is:75


NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

EXPLICIT=

#include<conio.h>

#include<stdio.h>

Void main()

Int a;

Floatb;

a=7,8;

b=150;

Printf(“ a equal %d”,a);

Printf(“\nb equals %f”,b);

OUTPUT=

A equals ?

B equals 150.000000

Processes returned 20(8*14) execution time :0.018 s


NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

PROGRAM_6=WRITE A C PROGRAM TO PERFORM ADDITION, SUBTRACTION, DIVISION AND


MULTIPLICATION OF TWO NUMBERS.

#include<conio.h>

#include<stdio.h>

Void main()

Int a=

/.
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

#include<conio.h>

#include<stdio.h>

Void main()

{
int n,sum=0,r;
clrscr();
printf("sum first digit and last digit of four digit
no=");
scanf("%d",&n);
r=n%1000;
sum=sum+r;
n=n/1000;
printf("sum=%d",sum);
}

Output

enter fist digit and last digit of four digit no=25

Sum=7
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

PROGRAM = (WHEN A IS MULTIPLIED BY B AND THE RESULT IS DIVIDE BY C )

#include<conio.h>

#include<stdio.h>

Void main()

{
int a,b,c,d;
clrscr();
printf("enter any no");
scanf("%d%d%d",&a,&b,&c);
d=a*b/c;
printf("%d",d);
}

Output

enter any no12

1
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

PROGRAM (MULTIPLICATION OF TWO N.O)

#include<conio.h>

#include<stdio.h>

Void main()

{
int a,b,c;
clrscr();
printf("enter any two no=");
scanf("%d%d",&a,&b);
c=a*b;
printf("%d",c);
}

Output

enter any two no5

20
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

PROGRAM (SUM OF FIRST DIGIT AND LAST DIGIT N.O )

#include<conio.h>

#include<stdio.h>

main()
{
int n,sum=0,r;
clrscr();
printf("sum first digit and last digit of four digit
no=");
scanf("%d",&n);
r=n%1000;
sum=sum+r;
n=n/1000;
printf("sum=%d",sum);
}

Output

enter fist digit and last digit of four digit no=25


NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

Sum=7

PROGRAM (REVERSE OF FOUR DIGIT N.O)

#include<conio.h>

#include<stdio.h>

main()
{
int n,rev=0,r;
clrscr();
printf("enter the reverse value of =");
scanf("%d",&n);

r=n%10;
rev=rev*10+r;
n=n/10;
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

r=n%10;
rev=rev*10+r;
n=n/10;

r=n%10;
rev=rev*10+r;
n=n/10;

r=n%10;
rev=rev*10+r;
n=n/10;
printf("rev=%d",rev);
}

Output

enter the reverse value of=1234

Rev=4321

PROGRAM(SUM OF 4 DIGIT NO-1)

#include<conio.h>

#include<stdio.h>

main()
{
int n,sum=0,r;
clrscr();
printf("enter any four digit no");
scanf("%d",&n);
r=n%10;
sum=sum+r;
n=n/10;
r=n%10;
sum=sum+r;
n=n/10;
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

r=n%10;
sum=sum+r;
n=n/10;
r=n%10;
sum=sum+r;
r=n/10;
printf("sum=%d",sum);
}

Output

enter any four digit no2345

Sum=14

PROGRAM = TO CHECK THE NO IS POSITIVE OR NOT

#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0) {
if (num == 0.0)
printf("You entered 0.");
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

else
printf("You entered a negative
number.");
} else
printf("You entered a positive
number.");
return 0;
}

OUTPUT=

Enter a number: 12.3


You entered a positive number.

PROGRAM TO FIND THE FACTORIAL NO.

#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

// 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

PROGRAM= FIND THE NO IS POSITIVE.

#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

if (num <= 0.0) {


if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative
number.");
} else
printf("You entered a positive number.");
return 0;
}

You can also solve this problem using nested if else statement.

Check Positive or Negative Using Nested if...else

#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);

if (num < 0.0)


printf("You entered a negative
number.");
else if (num > 0.0)
printf("You entered a positive
number.");
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

else
printf("You entered 0.");

return 0;
}

Output 1
Enter a number: 12.3
You entered a positive number.

Program to Check Even or Odd

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

// 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
Enter an integer: -7
-7 is odd.

LCM using while and if

#include <stdio.h>
int main() {
int n1, n2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

// maximum number between n1 and n2 is stored


in min
max = (n1 > n2) ? n1 : n2;

while (1) {
if (max % n1 == 0 && max % n2 == 0) {
printf("The LCM of %d and %d is %d.",
n1, n2, max);
break;
}
++max;
}
return 0;
}

Output
Enter two positive integers: 72
120
The LCM of 72 and 120 is 360.

Sum of Natural Numbers Using while Loop

#include <stdio.h>
int main() {
int n, i, sum = 0;
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

printf("Enter a positive integer: ");


scanf("%d", &n);
i = 1;

while (i <= n) {
sum += i;
++i;
}

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


return 0;
}

Output
Enter a positive integer: 100
Sum = 5050

Find the Frequency of a Character

#include <stdio.h>

int main() {

char str[1000], ch;

int count = 0;
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

printf("Enter a character to find its frequency: ");

scanf("%c", &ch);

for (int i = 0; str[i] != '\0'; ++i) {

if (ch == str[i])

++count;

printf("Frequency of %c = %d", ch, count);

return 0;

Output

Enter a string: This website is awesome.

Enter a character to find its frequency: e

Frequency of e = 4
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

Program to Check Prime Number

#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

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

// condition for non-prime


if (n % i == 0) {
flag = 1;
break;
}
}

if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}

return 0;
}

Output

Enter a positive integer: 29


NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

29 is a prime number.

Find the Frequency of a Character

#include <stdio.h>

int main() {

char str[1000], ch;

int count = 0;

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

printf("Enter a character to find its frequency: ");

scanf("%c", &ch);

for (int i = 0; str[i] != '\0'; ++i) {

if (ch == str[i])

++count;

printf("Frequency of %c = %d", ch, count);

return 0;

Output

Enter a string: This website is awesome.

Enter a character to find its frequency: e


NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

Frequency of e = 4

Program to Find the Size of Variables

#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
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

Find the Frequency of a Character

#include <stdio.h>

int main() {

char str[1000], ch;

int count = 0;

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

printf("Enter a character to find its frequency: ");

scanf("%c", &ch);

for (int i = 0; str[i] != '\0'; ++i) {

if (ch == str[i])

++count;

printf("Frequency of %c = %d", ch, count);

return 0;

}
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

Output

Enter a string: This website is awesome.

Enter a character to find its frequency: e

Frequency of e = 4

Program to count vowels, consonants etc.

#include <stdio.h>
NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

int main() {

char line[150];

int vowels, consonant, digit, space;

vowels = consonant = digit = space = 0;

printf("Enter a line of string: ");

fgets(line, sizeof(line), stdin);

for (int i = 0; line[i] != '\0'; ++i) {

if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||

line[i] == 'o' || line[i] == 'u' || line[i] == 'A' ||

line[i] == 'E' || line[i] == 'I' || line[i] == 'O' ||

line[i] == 'U') {

++vowels;

} else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {

++consonant;

} else if (line[i] >= '0' && line[i] <= '9') {

++digit;

} else if (line[i] == ' ') {

++space;

printf("Vowels: %d", vowels);


NAME = CHETNA
ROLL_NO=190010800013
COMPUTER ENGG.

printf("\nConsonants: %d", consonant);

printf("\nDigits: %d", digit);

printf("\nWhite spaces: %d", space);

return 0;

Output

Enter a line of string: adfslkj34 34lkj343 34lk

Vowels: 1

Consonants: 11

Digits: 9

White spaces: 2

You might also like