0% found this document useful (0 votes)
21 views19 pages

C Programming Operator and Function Examples

Cp assignment c programming

Uploaded by

pahujaroshan13
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)
21 views19 pages

C Programming Operator and Function Examples

Cp assignment c programming

Uploaded by

pahujaroshan13
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

ASSIGNMENT 1:

//Q1:Write a program to demonstrate Arithmetic,logical,and Bitwise operators

#include<stdio.h>

int main()

int a,b,c,d;

printf("Enter a : ");

scanf("%d", &a);

printf("Enter b : ");

scanf("%d", &b);

printf("Enter c : ");

scanf("%d", &c);

printf("Enter d : ");

scanf("%d", &d);

//Arithmetic Operators

printf("\nArithmetic operators:\n");

printf("%d + %d = %d\n", a, b, a+b);

printf("%d - %d = %d\n", a, b, a-b);

printf("%d * %d = %d\n", a, b, a*b);

printf("%d / %d = %d\n", a, b, a/b);

printf("(%d %% %d) = %d\n", a, b, a%b);

//Logical operators

printf("\nLogical operators:\n");
printf("(a > b) && (c > d): %d\n", (a > b) && (c > d));

printf("(a > b) || (c > d): %d\n", (a > b) || (c > d));

printf("!(a > b): %d\n", !(a > b));

// Bitwise operators

printf("\nBitwise operators:\n");

printf("a & b = %d\n", a & b);

printf("a | b = %d\n", a | b);

printf("a ^ b = %d\n", a ^ b);

printf("~a = %d\n", ~a);

printf("a << 2 = %d\n", a << 2);

printf("a >> 2 = %d\n", a >> 2);

return 0;

ASSIGNMENT 2:

//Q2 Write a program to print any one of the following pattern

#include<stdio.h>

int main()

int n,i;

printf("enter no of rows ");

scanf("%d",&n);

for(i=1;i<=n;i++)

for(int k=1;k<=n-i;k++)

printf(" ");

}
for(int j=1;j<=i;j++)

printf(" *");

printf("\n");

for(i=1;i<=n;i++)

for(int k=1;k<=i;k++)

printf(" ");

for(int j=1;j<=n-i;j++)

printf(" *");

printf("\n");

return 0;

ASSIGNMENT 3:

//Q3Write a Program to find the factorial, check whether the number is Armstrong, and

//check for perfect square, prime number, largest of three numbers, LCM and GCD

//using switch case. Also use Goto statments

#include <stdio.h>

#include <math.h>

long factorial(int n)
{

long fact = 1;

for(int i=1; i<=n; i++)

fact *= i;

return fact;

int isArmstrong(int n)

int sum = 0, temp = n;

while(temp != 0) {

int digit = temp % 10;

sum += digit * digit * digit;

temp /= 10;

return (sum == n);

int isPerfectSquare(int n)

int sqrt_n = sqrt(n);

return (sqrt_n * sqrt_n == n);

int isPrime(int n)

{
if(n <= 1) return 0;

for(int i=2; i*i<=n; i++)

if(n % i == 0) return 0;

return 1;

int largestThree(int a, int b, int c)

return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

int lcm(int a, int b)

int max=(a>b)?a:b;

while(1)

if(max%a==0 && max%b==0)

return max;

max++;

int gcd(int a, int b)

if(b == 0)

return a;
return gcd(b, a % b);

int main()

int choice, n1, n2, n3;

START:

printf("\nMenu:\n");

printf("1. Factorial\n");

printf("2. Armstrong Number\n");

printf("3. Perfect Square\n");

printf("4. Prime Number\n");

printf("5. Largest of Three Numbers\n");

printf("6. LCM\n");

printf("7. GCD\n");

printf("8. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Enter a number: ");

scanf("%d", &n1);

printf("Factorial: %ld\n", factorial(n1));

break;

case 2:

printf("Enter a number: ");


scanf("%d", &n1);

printf("%d is %sArmstrong number\n", n1, isArmstrong(n1) ? "" : "not ");

break;

case 3:

printf("Enter a number: ");

scanf("%d", &n1);

printf("%d is %sperfect square\n", n1, isPerfectSquare(n1) ? "" : "not ");

break;

case 4:

printf("Enter a number: ");

scanf("%d", &n1);

printf("%d is %sprime number\n", n1, isPrime(n1) ? "" : "not ");

break;

case 5:

printf("Enter three numbers: ");

scanf("%d %d %d", &n1, &n2, &n3);

printf("Largest number: %d\n", largestThree(n1, n2, n3));

break;

case 6:

printf("Enter two numbers: ");

scanf("%d %d", &n1, &n2);

printf("LCM: %d\n", lcm(n1, n2));

break;

case 7:

printf("Enter two numbers: ");

scanf("%d %d", &n1, &n2);

printf("GCD: %d\n", gcd(n1, n2));

break;

case 8:
goto END;

default:

printf("Invalid choice. Please choose again.\n");

goto START;

END:

return 0;

ASSIGNMENT 4:

//Q4Write a Program to find the value of nCr (Combination) using function

#include<stdio.h>

int main()

int n,r,nCr;

unsigned long int nf,rf,nrf;

unsigned long int factorial(int);

printf("Enter n and r:");

scanf("%d%d",&n,&r);

if(n<r || n<0 || r<0)

printf("Invalid data\n");

else

nf=factorial(n);

rf=factorial(r);
nrf=factorial(n-r);

nCr=nf/(rf*nrf);

printf("%dC%d=%d\n",n,r,nCr);

return 0;

unsigned long int factorial(int n)

unsigned long int fact=1;

int i;

for(i=1;i<=n;i++)

fact=fact*i;

return fact;

ASSIGNMENT 5:

//Q5 Write a Program to find the factorial using recursive function

#include<stdio.h>

int main()

int n;

unsigned long int ans;

unsigned long int factorial(int);

printf("Enter n:");

scanf("%d",&n);

ans=n*factorial(n-1);

if(n<0)
{

printf("Factorial of negative number is not possible");

else

printf("Factorial of %d is %lu\n",n,ans);

return 0;

unsigned long int factorial(int n)

int ans;

if(n==0)

return 1;

else

ans=n*factorial(n-1);

return ans;

ASSIGNMENT 7a:

/*a) Write a Program to find the average , largest, and arranging the elements in

descending order of one dimensional array*/

#include <stdio.h>

void sortArray(int arr[], int n)

{
for (int i = 0; i < n - 1; i++)

for (int j = i + 1; j < n; j++)

if (arr[i] < arr[j])

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

int main()

int n;

printf("Enter number of elements: ");

scanf("%d", &n);

int arr[n];

int total = 0, max;

printf("Enter the elements: ");

for (int i = 0; i < n; i++)

scanf("%d", &arr[i]);

total = total + arr[i];

}
max = arr[0];

for (int i = 1; i < n; i++)

if (arr[i] > max)

max = arr[i];

float avg = (float)total / n;

printf("Average: %.2f\n", avg);

printf("Largest: %d\n", max);

sortArray(arr, n);

printf("Array in descending order: ");

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

return 0;

ASSIGNMENT 7b:

//b) Write a Program to multiply two matrices using a function.

#include <stdio.h>

void multiply(int A[10][10], int B[10][10], int C[10][10], int r1, int c1, int r2, int c2) {

if (c1 != r2) {

printf("Multiplication not possible!\n");

return;
}

for (int i = 0; i < r1; i++) {

for (int j = 0; j < c2; j++) {

C[i][j] = 0;

for (int k = 0; k < c1; k++) {

C[i][j] += A[i][k] * B[k][j];

void display(int mat[10][10], int r, int c) {

for (int i = 0; i < r; i++) {

for (int j = 0; j < c; j++) {

printf("%d ", mat[i][j]);

printf("\n");

int main() {

int r1, c1, r2, c2;

printf("Enter the number of rows for Matrix A: ");

scanf("%d", &r1);

printf("Enter the number of columns for Matrix A: ");

scanf("%d", &c1);
printf("Enter the number of rows for Matrix B: ");

scanf("%d", &r2);

printf("Enter the number of columns for Matrix B: ");

scanf("%d", &c2);

if (c1 != r2) {

printf("Multiplication not possible!\n");

return 0;

int A[10][10], B[10][10], C[10][10];

printf("Enter elements of Matrix A:\n");

for (int i = 0; i < r1; i++) {

for (int j = 0; j < c1; j++) {

printf("Enter element A[%d][%d]: ", i + 1, j + 1);

scanf("%d", &A[i][j]);

printf("Enter elements of Matrix B:\n");

for (int i = 0; i < r2; i++) {

for (int j = 0; j < c2; j++) {

printf("Enter element B[%d][%d]: ", i + 1, j + 1);

scanf("%d", &B[i][j]);

multiply(A, B, C, r1, c1, r2, c2);


printf("Result of multiplication:\n");

display(C, r1, c2);

return 0;

ASSIGNMENT 8a:

// a) Write a Program to demonstrate the string functions.

#include <stdio.h>

#include <string.h>

int main()

char str1[50] = "Hello";

char str2[50] = "Worldd";

char str3[50];

printf("Length of str1: %lu\n", strlen(str1));

strcpy(str3, str1);

printf("str3 after copying str1: %s\n", str3);

int result = strcmp(str1, str2);

if (result == 0)

printf("str1 and str2 are equal.\n");

else if (result > 0)

printf("str1 is greater than str2.\n");

else

printf("str1 is smaller than str2.\n");


strcat(str2, str1);

printf("%s\n", str2);

return 0;

ASSIGNMENT 8b:

/* b)Write a Program to check whether the entered string is palindrome or not

without string functions.*/

#include <stdio.h>

int main() {

char input[100];

int start, end, length = 0, palindrome = 1;

printf("Enter a string: ");

scanf("%s", input);

while (input[length] != '\0') {

length++;

for (start = 0, end = length - 1; start < length; start++, end--) {

if (input[start] != input[end]) {

palindrome = 0;

break;

}
if (palindrome)

printf("The string is a palindrome.\n");

else

printf("The string is not a palindrome.\n");

return 0;

ASSIGMENT 9:

/* Write a program to store the name, roll number and marks in three subjects of n

students using Structure. Generate a merit list with respect to the total marks

secured. Display the output in Tabular form in order of maximum total marks to

minimum total marks*/

#include <stdio.h>

struct Student {

char name[50];

int roll_no;

int marks[3];

int total;

};

void input(struct Student *s) {

printf("Enter Name: ");

scanf("%s", s->name);

printf("Enter Roll Number: ");

scanf("%d", &s->roll_no);

printf("Enter marks in 3 subjects: ");


for (int i = 0; i < 3; i++) {

printf("Subject %d marks: ", i + 1);

scanf("%d", &s->marks[i]);

s->total = s->marks[0] + s->marks[1] + s->marks[2];

void display(struct Student s) {

printf("%-20s %-10d %-10d\n", [Link], s.roll_no, [Link]);

int main() {

int n;

printf("Enter number of students: ");

scanf("%d", &n);

struct Student students[n];

for (int i = 0; i < n; i++) {

printf("\nEntering details for student %d:\n", i + 1);

input(&students[i]);

for (int i = 0; i < n - 1; i++) {

for (int j = i + 1; j < n; j++) {

if (students[i].total < students[j].total) {

struct Student temp = students[i];

students[i] = students[j];
students[j] = temp;

printf("\nMerit List:\n");

printf("%-20s %-10s %-10s\n", "Name", "Roll No", "Total Marks");

for (int i = 0; i < n; i++) {

display(students[i]);

return 0;

Common questions

Powered by AI

Recursion and iterative methods both can be used to calculate factorials; the choice depends on readability and performance. A recursive factorial function (calling itself with a decrementing value) is elegant and concise but can lead to stack overflow for large numbers due to repeated function calls. Iterative methods use loops, which may be more efficient in terms of memory usage since they don't add to the call stack but might be less readable. The provided C programs demonstrate both methods: one using a recursive function , and another using a loop for iteration .

Matrix multiplication requires the number of columns in the first matrix to match the number of rows in the second matrix. The resulting matrix has dimensions based on the rows of the first and the columns of the second matrix. The code iterates over each entry of the resulting matrix and calculates its value by summing products of corresponding elements from the input matrices. Preconditions such as dimension checks are necessary to prevent invalid calculations, ensuring correct multiplication setup .

To sort an array in descending order, a nested loop iteratively compares elements and swaps them if they are not in the desired order. Calculating the average involves summing the elements and dividing by the number of elements, which is performed in a straightforward pass through the array. The sorting ensures data analysis or statistics tasks are accurate. The program integrates these tasks, allowing complex data handling necessary in computations involving dataset manipulation .

The Least Common Multiple (LCM) is the smallest number that is a multiple of two or more numbers, while the Greatest Common Divisor (GCD) is the largest number that divides two or more numbers without a remainder. The program calculates LCM by iterating through the numbers starting from the larger of the two inputs until it finds a number divisible by both. GCD is calculated using the Euclidean algorithm through recursion, which efficiently reduces the problem size. Calculating LCM and GCD is significant in problems involving ratios, fractions, and modular arithmetic .

Arithmetic operators in C perform mathematical operations such as addition, subtraction, multiplication, division, and modulus. Logical operators are used to evaluate boolean expressions, performing operations like logical AND (&&), logical OR (||), and logical NOT (!). Bitwise operators manipulate individual bits in data types using operations such as AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). The given program demonstrates these operators by allowing user input for integers and applying these operations to display the outcomes .

The recursive approach to calculating factorial involves defining a base case for n=0, where factorial is 1, and recursively calling the same function for n-1. This provides a clear and mathematically aligned way to solve the problem. Advantages include simpler and more elegant code, aligning closely to the mathematical definition. However, it has disadvantages such as higher memory use and risk of stack overflow with large n, compared to iterative solutions which are more efficient for large values as they avoid these issues by using loops .

Structures in C group related variables under a single name, enabling management of complex data types like student records, which include fields for name, roll number, and marks. In the student merit list program, structures facilitate sorting and display of student data by aggregating and operating on comprehensive records rather than separate arrays. This improves code readability and management, enabling efficient data manipulation and presentation .

A switch-case statement in C provides a clean, readable way to execute different parts of code based on user input. It compares the value of a variable against several cases, executing a block of code associated with the matching case. In the program, it provides a menu that the user can navigate by selecting a number for the desired operation, such as calculating factorial or checking for an Armstrong number. The switch structure makes the code organized and easy to extend or modify with additional features .

Calculating combinations, represented as nCr, involves factorials of n, r, and (n-r). The implementation checks for common errors like n < r and negative values for n or r, ensuring valid inputs before performing calculations. Without these checks, the program could attempt division by zero or produce meaningless results, showcasing the importance of error checking to prevent calculations on invalid data and ensuring program robustness and reliability .

String manipulations in C are primarily done using standard library functions, like strlen for length, strcpy for copying, strcat for concatenation, and strcmp for comparison. Using these functions, the program can efficiently manage strings, sorting, and combining data as necessary. These functions provide robust error-checking and efficiency, important when handling textual data in applications .

You might also like