0% found this document useful (0 votes)
27 views

C Code

Uploaded by

nazrin3141
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)
27 views

C Code

Uploaded by

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

Digital Assessment 1

NAZRIN NISSAM (24BBS0111)


1. SIMPLE ADDITION

#include<stdio.h>
int main(){
int a,b,c;
printf("enter a number:");
scanf("%d%",&a);
printf("enter a number:");
scanf("%d%",&b);
c=a+b;
printf("sum of the two numbers is %d",c);
return 0;
}

2. SUBTRACTION
#include<stdio.h>
int main(){
int a,b,c;
printf("enter a number:");
scanf("%d%",&a);
printf("enter a number:");
scanf("%d%",&b);
c=a-b;
printf("sum of the two numbers is %d",c);
return 0;
}
3. Multiplication
#include<stdio.h>
int main(){
int a,b,c;
printf("enter a number:");
scanf("%d%",&a);
printf("enter a number:");
scanf("%d%",&b);
c=a*b;
printf("product of the two numbers is %d",c);
return 0;
}

4. Division
#include<stdio.h>
int main(){
int a,b,c;
printf("enter a number:");
scanf("%d%",&a);
printf("enter a number:");
scanf("%d%",&b);
c=a/b;
printf("quotient of the two numbers is %d",c);
return 0;
}
5. Exponent
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
printf("enter a number:");
scanf("%f",&a);
printf("enter a number:");
scanf("%f",&b);
c=pow(a,b);
printf("Output: %f",c);
return 0;
}

Program 2
a) QUADRATIC EQUATION
#include <stdio.h>
#include <math.h>

int main() {

float a, b, c;
float d, r1, r2;
printf("Enter coefficient a: ");
scanf("%f", &a);
printf("Enter coefficient b: ");
scanf("%f", &b);
printf("Enter coefficient c: ");
scanf("%f", &c);
d = b * b - 4 * a * c;

if (a == 0) {
printf("Coefficient 'a' cannot be zero.\n");
} else if (d > 0) {

r1 = (-b + sqrt(d)) / (2 * a);


r2 = (-b - sqrt(d)) / (2 * a);
printf("Roots are real and different.\n");
printf("Root 1 = %f", r1);
printf("Root 2 = %f", r2);
} else if (d == 0) {
r1 = r2 = -b / (2 * a);
printf("Roots are real and the same.\n");
printf("Root 1 = Root 2 = %f\n", r1);
} else {

float realPart = -b / (2 * a);


float imaginaryPart = sqrt(-d) / (2 * a);
printf("Roots are complex and different.\n");
printf("Root 1 = %f + %f", realPart, imaginaryPart);
printf("Root 2 = %f - %f", realPart, imaginaryPart);
}

return 0;
}

b) Area of a circle
#include<stdio.h>
#include<math.h>
int main(){
float radius,area;
printf("Enter the radius of the circle");
scanf("%f",&radius);
area=3.14*pow(radius,2);
printf("The area of the circle is: %f",area);
return 0;
}
Program 3: Comparison
#include <stdio.h>

int main() {
int a, b, c;
int greatest, smallest;

printf("Enter the first number: ");


scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
printf("Enter the third number: ");
scanf("%d", &c);
greatest = a;
smallest = a;
if (b > greatest) {
greatest = b;
}
if (c > greatest) {
greatest = c;
}

if (b < smallest) {
smallest = b;
}
if (c < smallest) {
smallest = c;
}

printf("The greatest number is %d\n", greatest);


printf("The smallest number is %d\n", smallest);

return 0;
}
Program 4: Multiplication Table
#include<stdio.h>
int main(){
int a,n,i;
printf("Enter the number:");
scanf("%d",&a);
printf("Enter the last number to be multiplied with:");
scanf("%d",&n);
printf("Multiplication table for %d:\n", a);
for (i = 1; i <= n; i++) {
printf("%d x %d = %d\n", a, i, a*i);
}
return 0;
}

Program 5: Odd/Even Series


#include<stdio.h>
int main(){
int a,n,i;
printf("Enter the first number:");
scanf("%d",&a);
printf("Enter the number of items in the series");
scanf("%d",&n);
for (i=0;n>0;i+=2){
printf("%d\n",a+i);
n--;
}
}
Program 6: Fibonacci Series
#include <stdio.h>
int main() {
int a, b, n, sum, i;

printf("Enter the number of elements in the series: ");


scanf("%d", &n);

if (n <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}
sum = 0;
a = 0;
b = 1;
for (i = 1; i <= n; i++) {
// Add the current number to sum
sum += a;

// Print the current number


printf("%d ", a);

// Generate the next Fibonacci number


int next = a + b;
a = b;
b = next;
}
// Print the total sum
printf("\nThe sum of the numbers is %d\n", sum);

return 0;
}

Program 7: Armstrong Number


#include<stdio.h>
#include<math.h>
int main(){
int sum,num,ref,dig;
printf("Enter a number to check if it is armstrong number or not:");
scanf("%d",&num);
ref=num;
sum=0;
while (ref>0)
{
dig=ref%10;
sum=sum+pow(dig,3);
ref=ref/10;
}
if (sum==num){
printf("%d IS AN ARMSTRONG NUMBER",num);
}
else
{
printf("%d IS NOT AN ARMSTRONG NUMBER",num);
}
return 0;
}

Program 8: Shift operation & increment/decrement operator


#include <stdio.h>

int main() {
int num, leftshift, rightshift;
int preincrement, postincrement, predecrement, postdecrement;

printf("Enter a number: ");


scanf("%d", &num);
leftshift = num << 2; // Left shift num by 2 positions
rightshift = num >> 2; // Right shift num by 2 positions

int original_num = num;

// Increment and decrement operations


preincrement = ++num; // Increment num first, then assign to preincrement
postincrement = num++; // Assign current num to postincrement, then increment num
predecrement = --num; // Decrement num first, then assign to predecrement
postdecrement = num--; // Assign current num to postdecrement, then decrement num
printf("Original number: %d\n", original_num);
printf("LEFT SHIFT: %d\n", leftshift);
printf("RIGHT SHIFT: %d\n", rightshift);
printf("PREFIX INCREMENT: %d\n", preincrement);
printf("POSTFIX INCREMENT: %d\n", postincrement);
printf("PREFIX DECREMENT: %d\n", predecrement);
printf("POSTFIX DECREMENT: %d\n", postdecrement);
return 0;
}

Program 9: Implicit and Explicit type conversion


#include <stdio.h>

int main() {
int a = 8;
float b = 11.25;
float result;
result = a + b;
printf("Result: %f\n", result);
return 0;
}

#include <stdio.h>
int main() {
float f = 9.75;
int i;
i = (int) f;
printf("Integer value: %d\n", i);
return 0;
}

You might also like