Professional Documents
Culture Documents
Introduction To Programming: Practical File
Introduction To Programming: Practical File
PROGRAMMING
PRACTICAL FILE
NAME:- AMAN
ROLL NO.:-E72
GROUP:-E2
ENROLLNMENT NO:-
INDEX
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);
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>
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
Code:-
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
Output:-
Enter first number: 1.20
Enter second number: 2.45
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
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:-
Code:-
#include<stdio.h>
#include<math.h>
int main() {
float a,r,i,tn;
int n;
float sum=0;
scanf("%f",&a);
scanf("%d",&n);
scanf("%f",&r);
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
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
Code:-
#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
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:-
Output:-