Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

INTRODUCTION TO

PROGRAMMING

PRACTICAL FILE

NAME:- AMAN
ROLL NO.:-E72
GROUP:-E2
ENROLLNMENT NO:-
INDEX

S.NO. NAME DATE SIGNATURE


1 Hello World
2.1 Sum of 4
integers
2.2 Print age and
first letter
3.1 Swap numbers
3.2 Divisible by 5,9
and 15
3.3 Calculator
3.4 Sum of G.P.
4.1 Sum of digits of
number
4.2 A character is
consonant or
vowel
4.3 Roots of
quadratic
equation
4.4 Prime No.
Experiment – 1

Aim: Write a program to print Hello World.

Code –
#include <stdio.h>

int main()
{
printf("Hello World");

return 0;
}

Output-
Experiment – 2.1

Aim: Write a program in C to print the sum of four numbers with taking
input from the user.

Code-

#include <stdio.h>

int main(){
int num1,num2,num3,num4,ans;
printf("Enter The Integers: ");
scanf("%d %d %d %d",&num1,&num2,&num3,&num4);

ans = num1 + num2 + num3 + num4;


printf( “Sum of Integers = %d\n",ans);
return 0;
}

OutPut-
Experiment – 2.2

Aim: Write a program in C to take input from the user and print user's
first alphabet of his name and age of the user.

Code-

#include <stdio.h>

using namespace std;

int main(){
int age;
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin);
printf("\nEnter age: ");
scanf("%d",&age);
printf("\nFirst alphabet of name: %c",name[0]);
printf("\nAge: %d\n",age);
return 0;
}

Output-
EXPERIMENT:-3.1

Aim:- Write a program in C to swap a number

Code:-
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);

// Value of first is assigned to temp


temp = first;
// Value of second is assigned to first
first = second;
// Value of temp (initial value of first) is assigned to second
second = temp;
  // %.2lf displays number up to 2 decimal points
printf("\nAfter swapping, firstNumber = %.2lf\n", first);
printf("After swapping, secondNumber = %.2lf", second);
return 0;
}

Output:-
Enter first number: 1.20
Enter second number: 2.45

After swapping, firstNumber = 2.45


After swapping, secondNumber = 1.20
EXPERIMENT:-3.2

Aim:- Write a program in C to check if a number is divisible by 5, 9


and 15

Code:-
#include<stdio.h>
void main()
{
         int a;
         clrscr();
         printf ("Enter the no.");
         scanf("%d",&a);
         if(a%5==0)
          {
             printf("No.is Divisible by 5");

If(a%9==0){
Printf(“No is Divisible by 9”);
}
If(a%15==0){
Printf(“No is Divisible by 15”);

else {
printf(“No is not Divisible by 15”);
}
}

          }
else {
printf(“No is not Divisible by 9”);
}
         else
         {
               printf("No is not Divisible by 5");
         }
               getch();
}

Output:-
EXPERIMENT:-3.3

Aim:-Write a C program for a calculator.

Code:-

#include <stdio.h>
int main() {
char operator;
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (operator) {
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:-

Enter an operator (+, -, *,): *


Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8
EXPERIMENT:-3.4

Aim:-Write a C program to calculate sum of G.P series.

Code:-
#include<stdio.h>

#include<math.h>

int main() {

float a,r,i,tn;

int n;

float sum=0;

printf("Enter the first number of the G.P. series:


");

scanf("%f",&a);

printf("Enter the total numbers in the G.P. series:


");

scanf("%d",&n);

printf("Enter the common ratio of G.P. series: ");

scanf("%f",&r);

sum = (a*(1 - pow(r,n+1)))/(1-r);


tn = a * (1 -pow(r,n-1));

printf("tn term of G.P.: %f",tn);

printf("\nSum of the G.P.: %f",sum);

return 0;

Output:-
Enter the first number of the G.P. series: 1
Enter the total numbers in the G.P. series: 5
Enter the common ratio of G.P. series: 2
tn term of G.P. : 16.000000
Sum of the G.P. : 63.000000
EXPERIMENT:-4.1

Aim:-Write a C program to print sum of digits of a no entered by


user.

Code:-
#include <stdio.h>
int main()
{
   int n, t, sum = 0, remainder;

   printf("Enter a number\n");
   scanf("%d", &n);

   t = n;

   while (t != 0)
   {
      remainder = t % 10;
      sum       = sum + remainder;
      t         = t / 10;
   }

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

   return 0;
}

Output:-
Sum is=6

EXPERIMENT:-4.2

Aim:-Write a C program to find whether an alphabet entered by


user is a consonant or a vowel.

Code:-

#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);

// evaluates to 1 if variable c is a lowercase vowel


lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' ||
c == 'u');

// evaluates to 1 if variable c is a uppercase vowel


uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' ||
c == 'U');

// evaluates to 1 (true) if c is a vowel


if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
Output:-

Enter an alphabet: G
G is a consonant.
EXPERIMENT:-4.3
Aim:-Write a C program to find out roots of quadratic equation
entered by user.

Code:-

#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart,
imagPart, realPart, imagPart);
}
return 0;
}
Output:-

Enter coefficients a, b and c: 2.3


4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i
EXPERIMENT:-4.4

Aim:-Write a C program to find whether a no is prime or not.

Code:- #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


29 is a prime number

You might also like