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

CP Assignments

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)
73 views19 pages

CP Assignments

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
You are on page 1/ 19

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", s.name, s.roll_no, s.total);

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;

You might also like