Pwc Praticals (2)-Cropped
Pwc Praticals (2)-Cropped
1
Aim :Write a program to understand concepts of structure of C Program,
scanf and printf.
#include <stdio.h>
int main()
{
int a; printf("Enter the number: ");
scanf("%d", &a);
printf("%d", a);
return 0;
}
Output :
Practical 1.2
Aim : Write a program to declare, assign, read and print values of
variables of different data types.
#include <stdio.h>
int main()
{
int a;
float b;
char c;
double d;
printf("Enter the number: ");
scanf("%d", &a);
printf("Enter the number: ");
scanf("%f", &b);
printf("Enter the number: ");
scanf(" %c", &c);
printf("Enter the number: ");
scanf("%lf", &d);
printf("the enter number are %d,%f,%c,%2lf",a,b,c,d);
return 0;
}
Output :
Practical 1.3
Aim : Write a program to that performs as calculator
(addition, multiplication, division,subtraction).
#include <stdio.h>
int main()
{
int a,b;
printf("Enter the number: ");
scanf("%d",&a);
printf("Enter the number: ");
scanf("%d",&b);
printf("the answer is addition %d\n",a+b);
printf("the answer is sub %d\n",a-b);
printf("the answer is multiplication %d\n",a*b);
printf("the answer is division %d\n",a/b);
return 0;
}
Output :
Practical 2.1
Aim : Write a program to understand concepts of other operators (bitwise,
increment/decrement, conditional, etc.).
#include <stdio.h>
int main()
{
int num1=26,num2=56;
printf(num1>num2?"num1 is greater\n":"num2 is greater than num1\n");
printf("Post Increment %d\n",num1++);
printf("Pre Increment %d\n",++num1);
printf("Post decrement %d\n",num1--);
printf("Post decrement %d\n",--num1);
printf("bitwise & %d\n",num1&num2);
printf("bitwise| %d\n",num1|num2);
printf("bitwise~ %d\n",~num1);
printf("Right Shift %d\n",num1<<1);
printf("Left Shift %d\n",num1>>2);
return 0;
}
Output :
Practical 2.2
Aim :Write a program to find the area of square, rectangle, triangle, and
circle.
#include <stdio.h>
int main()
{
int length, radius, breath, height;
printf("Enter the length: ");
scanf("%d", &length);
printf("Enter the radius: ");
scanf("%d", &radius);
printf("Enter the breath: ");
scanf("%d", &breath);
printf("Enter the height: ");
scanf("%d", &height);
printf("area of square %d\n", length * length);
printf("area of circle %f\n", 3.14 * radius * radius);
printf("area of rectangle %d\n", length * breath);
printf("area of triangle %f\n", length * height * 0.5);
return 0;
}
Output :
Practical 2.3
Aim :Write a program to calculate simple interest (i = (p*r*n)/100).
Where i = Simple interest, p = Principal amount,r = Rate of interest,
n = Number of years.
#include <stdio.h>
int main()
{
int p,r,n,i;
printf("Enter the Principal: ");
scanf("%d", &p);
printf("Enter the Rate: ");
scanf("%d", &r);
printf("Enter the Number of year: ");
scanf("%d", &n);
i=p*r*n/100;
printf("the simple interest is %d",i);
return 0;
}
Output :
Practical 3.1
Aim : Write a program to enter a distance into kilometres convert it into
metre,feet,inches,centimetre.
#include <stdio.h>
int main()
{
int a,meter,feet,inches,centimeter;
printf("Enter a number:");
scanf("%d",&a);
meter=a*1000;
feet=a*3280.84;
inches=a*39370.1;
centimeter=a*100000;
printf("\nMETER = %d",meter);
printf("\nFEET = %d",feet);
printf("\nINCHES = %d",inches);
printf("\nCENTIMETER = %d",centimeter);
return 0;
}
Output :
Practical 3.2
#include <stdio.h>
int main()
{
int num;
printf("enter a number:");
scanf("%d",& num);
if (num% 2==0)
{
printf("%d the number is even",num);
}
else
{
printf("%d the number is odd",num);
}
return 0;
}
Output :
Practical 3.3
#include <stdio.h>
int main()
{
float c,f;
printf("Enter the celsius ");
scanf("%f",&c); f=1.8*c+32;
printf(" %f fahrenheit",f);
return 0;
}
Output :
Practical 4.1
#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num>0)
{
printf("The number is positive");
}
else if (num<0)
{
printf("The number is negative");
}
else
{
printf("The num is 0 ");
}
return 0;
}
Output :
Practical 4.2
Aim :Write a program to read three numbers from the keyboard and find
out maximum out of these three (Nested if else).
#include <stdio.h>
int main()
{
int a,b,c;
printf("enter first number:");
scanf("%d",&a);
printf("enter second number:");
scanf("%d",&b);
printf("enter the third number:");
scanf("%d",&c);
Output :
Practical 4.3
#include <stdio.h>
int main()
{
char ch;
printf("\n enter any character");
scanf("%c",&ch);
if(ch >='0' && ch <='9')
{
printf("\n entered character is digit");
}
else if(ch>='A' && ch<='Z')
{
printf("\n entered character is captial letter");
}
else if(ch>='a' && ch<='z')
{
printf("\n entered character is small letter");
}
else
{
printf("\n entered character is special character");
}
return 0;
}
Output :
Practical 5.1
Aim : Write a program to read marks from keyboard and your program should
display equivalent grade according to following table (if else ladder);
Marks Grade 100 - 80
Distinction 79 - 60
First Class 59 - 40
Second Class < 40 Fail
#include<stdio.h>
int main() {
int marks;
printf("\n Enter Marks between 0-100 :");
scanf("%d", & marks);
if (marks > 100 || marks < 0)
{
printf("\n Your Input is out of Range");
} else if (marks >= 80)
{
printf("\n Distinction");
} else if (marks >= 60)
{
printf("\n First Class");
} else if (marks >= 40)
{
printf("\n Second Class");
} else
{
printf("\n Fail");
}
return 0;
}
Output :
Practical 5.2
#include <stdio.h>
int main()
{
int num1,num2;
float result;
char ch;
printf("enter first number: ");
scanf("%d",&num1);
printf("enter second number: ");
scanf("\n%d",&num2);
printf("choose opreator to perform: ");
scanf("\n%c",&ch);
result=0;
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=num1/num2;
break;
default:
printf("invalid oprataion\n");
}
printf("RESULT: %d %c %d = %.1f\n",num1,ch,num2,result);
return 0;
}
Output :
Practical 5.3
#include <stdio.h>
int main()
{
int i,n,factorial=1;
printf("enter any number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
factorial=factorial*i;
}
printf("factorial of %d is %d",n,factorial);
return 0;
}
Output :
Practical 6.1
#include <stdio.h>
int main()
{
int n, reverse=0, r;
printf("enter any number : ");
scanf("%d", &n);
while(n!=0)
{
r=n%10;
reverse=reverse*10+r;
n/=10;
}
Output :
Practical 6.2
#include <stdio.h>
int main()
{
int n, num1=0,num2=1,nextnum;
printf("enter the number");
scanf("%d",&n);
Output :
Practical 6.3
#include<stdio.h>
int main()
{
int i,n,Sum=0,numbers;
float Average;
printf("\nPlease Enter How many Number you want?\n");
scanf("%d",&n);
printf("\nPlease Enter the elements one by one\n");
for(i=0;i<n;++i)
{
scanf("%d",&numbers);
Sum = Sum +numbers;
}
Average = Sum/n;
printf("\nSum of the %d Numbers = %d",n, Sum);
printf("\nAverage of the %d Numbers = %.2f",n, Average);
return 0;
}
Output :
Practical 6.4
Output :
Practical 7.1
#include<stdio.h>
int main()
{
int n,i,sum=0;
printf("enter any number: ");
scanf("%d",&n);
sum=sum+(i*i);
}
printf("the sum of series : %d",sum);
return 0;
Output :
Practical 7.2
#include <stdio.h>
long long factorial(int num)
long long fact = 1;
for(int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
double factorial_series_sum(int n) {
double sum = 1.0; // Start with the first term (1)
for(int i = 2; i <= n; i++) {
sum += 1.0 / factorial(i);
}
return sum;
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
double result = factorial_series_sum(n);
printf("The sum of the series is: %.6f\n", result);
return 0;
}
Output :
Practical 7.3
1)
#include <stdio.h>
int main()
{
int i,j,rows;
printf("enter number of rows :");
scanf("%d" , &rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output :
2)
#include <stdio.h>
int main()
{
int i,j,rows;
printf("enter number of rows :");
scanf("%d" , &rows);
for(i=rows;i<=rows;i--)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output :
Practical 7.4
1)
#include <stdio.h>
int main()
{
int i,j,rows;
char c='A';
printf("enter number of rows :");
scanf("%d" , &rows);
for(i=rows;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%c",c);
}
c++;
printf("\n");
}
return 0;
}
Output :
Practical 7.4
2)
#include <stdio.h>
int main()
{
int n,i,j;
printf("Enter the number of rows:");
scanf("%d", &n);
for (i =1; i<=n; i++)
{
for(j=1;j<=n;j++)
{
if(i<=j)
printf("%d",j);
else printf(" ");
}
printf(" \n");
}
return 0;
}
Output :
Practical 7.4
3)
#include <stdio.h>
int main()
{
int i,j,n,p,q;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{ p=1;q=0;}
else
{ p=0;q=1;}
for(j=1;j<=i;j++)
if(j%2==0)
printf("%d",p);
else
printf("%d",q);
printf("\n");
}
return 0;
}
Output :
Practical 8.1
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Array in reverse order:\n");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output :
Practical 8.2
Aim :Write a program that adds two 1-dimensional array & store into
third array.
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the arrays: ");
scanf("%d", &n);
int array1[n], array2[n], result[n];
printf("Enter elements of the first array:\n");
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array1[i]);
}
printf("Enter elements of the second array:\n");
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array2[i]);
}
for (int i = 0; i < n; i++) {
result[i] = array1[i] + array2[i];
}
printf("The resulting array after addition is:\n");
for (int i = 0; i < n; i++) {
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
Output :
Practical 8.3
Output :
Practical 8.4
Aim :Write a program to sort a given array in ascending order (Use Bubble
Sort algorithm).
#include <stdio.h>
int main()
{
int arr[100], n, i, j, swap;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (arr[j] > arr[j+1])
{
swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (i= 0; i < n; i++)
printf("%d\n", arr[i]);
return 0;
}
Output :
Practical 9.1
#include <stdio.h>
int main()
{
int m, n, p, q, i, j, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix:\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix:\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);
printf("Enter number of rows and columns of second matrix:\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix:\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &second[i][j]);
for (i = 0; i < m; i++)
{ for (j = 0; j < q; j++)
{ for (k = 0; k < p; k++)
{ sum = sum + first[i][k]*second[k][j];
}
multiply[i][j] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (i = 0; i < m; i++)
{ for (j = 0; j < q; j++)
printf("%d\t", multiply[i][j]);
printf("\n");
}
}
return 0;
}
Output :
Practical 9.2
#include <stdio.h>
int main()
{
char s[100], i;
printf("Enter a string=");
scanf("%s", &s);
for(i=0; s[i] !='\0'; i++);
{
printf("\n Length of input string=%d", i);
}
return 0;
}
Output :
Practical 9.3
#include <stdio.h>
int main()
{
char str1[50], str2[50], i, j;
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
for(i=0; str1[i]!='\0'; ++i)
for(j=0; str2[j]!='\0'; ++j, ++i)
{
str1[i]=str2[j];
}
str1[i]='\0';
printf("\nOutput: %s",str1);
return 0;
}
Output :
Practical 10.1
#include <stdio.h>
void main()
{
char s[1000], c;
int i, count=0;
printf("Enter the string=");
gets(s);
printf("Enter character to be searched=");
c= getchar();
for(i=0; s[i]; i++)
{
if (s[i]==c)
{
count++;
}
}
printf("Character '%c' occurs '%d' times\n", c, count);
}
Output :
Practical 10.2
#include <stdio.h>
int main()
{
char str[100];
int c;
printf("Enter a string: ");
gets(str);
for (c = 0; str[c] != NULL; c++)
{
if (str[c] >= 'A' && str[c] <= 'Z')
str[c] = str[c] + 32;
else if (str[c] >= 'a' && str[c] <= 'z')
str[c] = str[c] - 32;
}
printf("String after toggle each characters: %s", str);
return 0;
}
Output :
Practical 10.3
#include<stdio.h>
int main()
{
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1])
{
flag = 1;
break;
}
}
if (flag)
{
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
} return 0;
}
Output :
Practical 11.1
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = " Hello, C World! ";
char str2[50];
char *token;
printf("Length of string: %lu\n", strlen(str1));
strcpy(str2, "Welcome to ");
strcat(str2, "C Programming!");
printf("Concatenated string: %s\n", str2);
printf("Comparison result: %d\n", strcmp("C", "C"));
printf("First occurrence of 'C': %s\n", strchr(str1, 'C'));
printf("Substring 'World' found at: %s\n", strstr(str1, "World"));
printf("Tokens:\n");
token = strtok(str1, " ,!");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, " ,!");
}
return 0;
}
Output :
Practical 11.2
Aim : Write a function power that computes x raised to the power y for
integer x and y and returns double type value.
#include <stdio.h>
double power(int x, int y) {
double result = 1.0;
if (y < 0) {
x = 1 / x;
y = -y;
}
for (int i = 1; i <= y; i++) {
result *= x;
}
return result;
}
int main() {
int base, exponent;
printf("Enter base (x): ");
scanf("%d", &base);
printf("Enter exponent (y): ");
scanf("%d", &exponent);
double result = power(base, exponent);
printf("%d raised to the power of %d is: %lf\n", base, exponent, result);
return 0;
}
Output :
Practical 11.3
#include <stdio.h>
double add(double a, double b) {
return a + b;
}
double subtract(double a, double b) {
return a - b;
}
double multiply(double a, double b) {
return a * b;
}
double divide(double a, double b) {
if (b != 0) {
return a / b;
} else {
printf("Error! Division by zero.\n");
return 0;
}
}
int main() {
double num1, num2, result;
int choice;
printf("Simple Calculator\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
printf("Enter first number: ");
scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);
switch(choice) {
case 1:
result = add(num1, num2);
printf("Result: %.2lf\n", result);
break;
printf("Result: %.2lf\n", result);
break;
case 3:
result = multiply(num1, num2);
printf("Result: %.2lf\n", result);
break;
case 4:
result = divide(num1, num2);
if (num2 != 0) {
printf("Result: %.2lf\n", result);
}
break;
default:
printf("Invalid choice! Please select a valid operation (1-4).\n");
break;
}
return 0;
}
Output :
Practical 12.1
#include <stdio.h>
int sumOfArray(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements of the array:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = sumOfArray(arr, n);
printf("The sum of the array elements is: %d\n", sum);
return 0;
}
Output :
Practical 12.2
Aim :Write a program that use user defined function swap() to interchange
the value of two variable.
#include <stdio.h>
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
printf("Enter the first number (a): ");
scanf("%d", &a);
printf("Enter the second number (b): ");
scanf("%d", &b);
printf("\nBefore swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("\nAfter swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output :
Practical 12.3
#include <stdio.h>
long long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d is: %lld\n", num, factorial(num));
}
return 0;
}
Output :
Practical 12.4
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series up to %d terms:\n", n);
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
Output :
Practical 13.1
Aim :Write a function which takes a two integer array as argument and
give sum of these arrays.
#include <stdio.h>
void sumArrays(int arr1[], int arr2[], int result[], int size) {
for (int i = 0; i < size; i++) {
result[i] = arr1[i] + arr2[i];
}
}
int main() {
int n;
printf("Enter the size of the arrays: ");
scanf("%d", &n);
int arr1[n], arr2[n], result[n];
printf("Enter elements of the first array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
printf("Enter elements of the second array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr2[i]);
}
sumArrays(arr1, arr2, result, n);
printf("Sum of the arrays is:\n");
for (int i = 0; i < n; i++) {
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
Output :