[go: up one dir, main page]

0% found this document useful (0 votes)
56 views41 pages

Mukesh CSP Lab - 1to 5

The document contains code snippets for 5 questions related to C programming assignments. Question 1 prints "Hello World". Question 2 takes input of two numbers and prints their sum. Question 3 calculates simple interest given principal, rate and time. Question 4 converts Fahrenheit to Celsius. Question 5 demonstrates two ways to swap two numbers using a third variable or arithmetic operations.

Uploaded by

regregewgewg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views41 pages

Mukesh CSP Lab - 1to 5

The document contains code snippets for 5 questions related to C programming assignments. Question 1 prints "Hello World". Question 2 takes input of two numbers and prints their sum. Question 3 calculates simple interest given principal, rate and time. Question 4 converts Fahrenheit to Celsius. Question 5 demonstrates two ways to swap two numbers using a third variable or arithmetic operations.

Uploaded by

regregewgewg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

ASSIGNMENT-1

Q.1

#include<stdio.h>

int main()

printf("Hello world");

OUTPUT.

Q2.

#include<stdio.h>

int main()

int a,b;

printf("Enter the value of a\n",a);

scanf("%d",&a);
printf("Enter the value of b\n",&b);

scanf("%d",&b);

printf("Sum of two numbers is:%d\n",a+b);

return 0;

OUTPUT.

Q3.

#include<stdio.h>

int main()

int principle,rate,time;

printf("Enter the value of principle:%d\n",principle);

scanf("%d",&principle);

printf("Enter the value of rate:%d\n",rate);


scanf("%d",&rate);

printf("Enter the value of time:%d\n",time);

scanf("%d",&time);

printf("Simpleinterest of this amount is:%d\n",(principle*rate*time)/100);

return 0;

OUTPUT.

Q4.

#include<stdio.h>

int main()

float fahrenheit,celsius;

printf("Enter the value of fahrenheit:%f\n",fahrenheit);


scanf("%f",&fahrenheit);

printf("Value of this fahrenheit temprature in celsius:%f",(fahrenheit-32)*5/9);

return 0;

OUTPUT.

Q5 (I)

#include<stdio.h>

int main()

int firstnumber,secondnumber,tempnumber;

printf("Enter firstnumber:");

scanf("%d",&firstnumber);

printf("Enter secondnumber:");

scanf("%d",&secondnumber);
tempnumber=firstnumber;

firstnumber=secondnumber;

secondnumber=tempnumber;

printf("\nafter swap\nfirstnumber:%d secondnumber:%d",firstnumber,secondnumber);

return 0;

OUTPUT.

Q5 (II)

#include<stdio.h>

int main()

int a,b;

printf("Enter a:");

scanf("%d",&a);

printf("Enter b:");
scanf("%d",&b);

a=a+b;

b=a-b;

a=a-b;

printf("\nafter swap\na:%d b: %d",a,b);

return 0;

OUTPUT.
ASSIGNMENT- 2

Q1.

#include<stdio.h>

int main()

int year;

printf("Enter the year");

scanf("%d",&year);

//if year is divisible by 400 then it is a leap year

if(year%400==0){

printf("year is a leap year");

//if year is divisible by 100 but not divisible by 400 then it is not a leap year

else if(year%100==0){

printf("year is not a leap year");

//if year is divisible by 4 and not divisible by 100 then it is a leap year

else if(year%4==0){

printf("year is a leap year");


}

else{

printf("year is not a leap year");

return 0;

OUTPUT.

Q2.

#include<stdio.h>

int main()

int a;

printf("Enter the value of a number\n",a);

scanf("%d",&a);

//if number is divisible by 2 then it is even else odd

if(a%2==0){

printf("%d is even\n",a);
}

else{

printf("%d is odd\n",a);

return 0;

OUTPUT.

Q3.

#include<math.h>

#include<stdio.h>

int main()

//Equation of quadartic is ax^2+bx+c;

int a,b,c;

float d,root1,root2,realpart,imaginarypart;

printf("Enter the value of a,b,c\n");

scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;

if(d<0){

realpart=-b/(2*a);

imaginarypart=sqrt(d)/(2*a);

printf("the imaginary root is %f+i%f",realpart,imaginarypart,root1);

printf("the second imaginary root is %f-i%f",realpart,imaginarypart,root2);

else if(d==0){

printf("Both roots are equal.\n");

root1=-b/(2*a);

printf("Root of quadratic equation is:%f %f",root1,root2);

else if(d>0){

printf("Roots are real numbers.\n");

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

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

printf("Roots of quadratic equation are:%f,%f",root1,root2);

return 0;

OUTPUT.
Q4.

#include<stdio.h>

int main()

int a,b,c;

printf("Enter the value of a,b,c\n");

scanf("%d%d%d",&a,&b,&c);

//if a is greter than b and c then a is gretest

if(a>=b && a>=c){

printf("a is gretest\n");

//if b is greter than a and c then b is gretest

else if(b>=a && b>=c){

printf("b is gretest\n");

//if c is greter than a and b then c is gretest

else if(c>=a && c>=b){

printf("c is gretest\n");
}

return 0;

OUTPUT.
ASSIGNMENT-3

Q.1.1

#include<stdio.h>

int main ()

int i,n,m;

long int value=1;

printf("Enter the value of n\n");

scanf("%d",&n);

printf("Enter the value of m\n");

scanf("%d",&m);

for(i=0;i<m;i++)

value=value*n;

printf("The value of n to the power m is %ld",value);

return 0;

}
Output

Q.1.2.

#include<stdio.h>

int main()

int i,n;

long int value=1;

printf("Enter the value of n\n");

scanf("%d",&n);

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

value=value*n;

printf("The value of n cube is %ld",value);

return 0;
}

Output

#include<stdio.h>

int main()

int i,n;

long int value=1;

printf("Enter the value of n\n");

scanf("%d",&n);

for(i=0;i<2;i++)

value=value*n;

printf("The value of n square is %ld",value);

return 0;

Output
Q.2.

#include<stdio.h>

int main()

int i,n,sum=0;

printf ("Enter the value of n\n");

scanf("%d",&n);

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

printf("The value of i is %d\n",i);

sum=sum+i;

printf("the value of sum of first n natural number is %d",sum);

return 0;

Output
Q.3.

#include<stdio.h>

int main()

int i,j;

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

for(j=2;j<i;j++)

if(i%j==0)

break;

if(i==j)

printf("%d\n",i);

return 0;
}

Output

Q.4.

#include<stdio.h>

int main()

int n,i,factorial=1;

printf("Enter the value of n\n");


scanf("%d",&n);

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

factorial*=i;

printf("The value of factorial %d is %d",n,factorial);

return 0;

Output

Q.5.

#include<stdio.h>

int main()

int i,n;

//define first and second terms

int term1=0,term2=1;

int nextTerm=term1+term2;
printf("Enter the number of terms:");

scanf("%d",&n);

//print first two terms term1, term2

printf("fibonacci series:%d,%d,",term1,term2);

//print 3rd to nth terms

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

printf("%d,",nextTerm);

term1=term2;

term2=nextTerm;

nextTerm=term1+term2;

return 0;

Output
ASSIGNMENT-4

Q1.

#include<stdio.h>

main()

float a[100],sum=0,avg;

int n,i;

printf("enter a number \n");

scanf("%d",&n);

printf("enter numbers \n ");

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

printf("a[%d]= ",i);

scanf("%f",&a[i]);

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

sum=sum+a[i];

}
printf("sum=%f\n",sum);

avg=sum/n;

printf("avg=%f\n",avg);

return 0;

OUTPUT.

Q2.

#include<stdio.h>

int main(){

int i;

int a[10]={2,45,78,89,90,78,23,49,15,51};

printf("original order \n");

for(i=0;i<10;i++)

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

}
printf("\n");

printf("reverse order \n");

for(i=9;i>=0;i--)

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

OUTPUT.

Q3.

#include<stdio.h>

int remove(int arr[],int n)

if (n==0||n==1)

return n;

int temp[n];

int j=0;

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

if(arr[i]!=arr[i+1])

temp[j++]=arr[i];

temp[j++]=arr[n-1];

for(i=0;i<j;i++)

arr[i]=temp[i];

return j;

int main()

int n;

printf("enter n(how many numbers you will write) \n");

scanf("%d",&n);

printf("enter numbers\n");

int arr[n];

int i;

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

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

printf("after eliminating the duplicate numbers \n");


n=remove(arr,n);

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

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

return 0;

OUTPUT.

Q4.

#include<stdio.h>

int main()

int i, j, rows, columns, a[10][10], b[10][10], Count = 1;

printf("\n Please Enter Number of rows and columns : ");

scanf("%d %d", &i, &j);


printf("\n Please Enter the Matrix Elements \n");

for(rows = 0; rows < i; rows++)

for(columns = 0;columns < j;columns++)

scanf("%d", &a[rows][columns]);

//Transpose of matrix

for(rows = 0; rows < i; rows++)

for(columns = 0;columns < j; columns++)

b[columns][rows] = a[rows][columns];

for(rows = 0; rows < i; rows++)

for(columns = 0; columns < j; columns++)

if(a[rows][columns] != b[rows][columns])
{

Count++;

break;

if(Count == 1)

printf("\n The Matrix that you entered is a Symmetric Matrix ");

else

printf("\n The Matrix that you entered is Not a Symmetric Matrix ");

return 0;

OUTPUT.
Q5.

#include<stdio.h>

int main()

int mat[12][12];

int i,j,row,col,sum=0;

printf("enter the number of rows and columns for matrix \n");

scanf("%d%d",&row,&col);

printf("enter the numbers of matrix");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

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

printf("the matrix \n");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

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

printf("\n");

for(i=0;i<row;i++){

for(j=0;j<col;j++)

if(i==j)

sum=sum+mat[i][j];

printf("the sum of all diagonal elements of a square marix=%d\n",sum);


return 0;

OUTPUT.
ASSIGNMENT- 5

Q1.

#include <stdio.h>

int main()

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

printf("Enter value to find\n");

scanf("%d", &search);

first = 0;

last = n - 1;

middle = (first+last)/2;
while (first <= last) {

if (array[middle] < search)

first = middle + 1;

else if (array[middle] == search) {

printf("%d found at location %d.\n", search, middle+1);

break;

else

last = middle - 1;

middle = (first + last)/2;

if (first > last)

printf("Not found! %d isn't present in the list.\n", search);

return 0;

OUTPUT.
Q2.

#include <stdio.h>

int main()

int array[100], n, c, d, swap;

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)

for (d = 0 ; d < n - c - 1; d++)

if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */

swap = array[d];

array[d] = array[d+1];

array[d+1] = swap;

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)

printf("%d\n", array[c]);

return 0;

}
OUTPUT.

Q3.

#include <stdio.h>

int main()

int array[100], n, c, d, position, t;

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);


for (c = 0; c < n; c++)

scanf("%d", &array[c]);

for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times

position = c;

for (d = c + 1; d < n; d++)

if (array[position] > array[d])

position = d;

if (position != c)

t = array[c];

array[c] = array[position];

array[position] = t;

printf("Sorted list in ascending order:\n");


for (c = 0; c < n; c++)

printf("%d\n", array[c]);

return 0;

OUTPUT.

Q4.

#include <stdio.h>

int main()

int n, array[1000], c, d, t, flag = 0;


printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

for (c = 1 ; c <= n - 1; c++) {

t = array[c];

for (d = c - 1 ; d >= 0; d--) {

if (array[d] > t) {

array[d+1] = array[d];

flag = 1;

else

break;

if (flag)

array[d+1] = t;
}

printf("Sorted list in ascending order:\n");

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

printf("%d\n", array[c]);

return 0;

OUTPUT.

You might also like