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

c lang. assignment

C Lang assignment

Uploaded by

vikramrajput9304
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

c lang. assignment

C Lang assignment

Uploaded by

vikramrajput9304
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

Q.no.

#include<stdio.h>

int main()

int a,b,c;

printf("Enter first number\n");

scanf("%d",&a);

printf("Enter second number\n");

scanf("%d",&b);

c=a+b;

printf("Add=%d",c);

R two number");

Scanf("%d%d",&a,&b);

c=a;

a=b;

b=c;

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

Q.no3

#include <stdio.h>

int main()

{
float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9 / 5) + 32;

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;

Q.no.4

#include<stdio.h>

int main()

int f,s;

printf("Enter first number\n");

scanf("%d",&f);

printf("Enter second number\n");

scanf("%d",&s);

if(f>s)

printf("first no is greater");

if(f<s)

printf("second no is greater");

printf("both are equal");


}

Q.no.5

#include<stdio.h>

int main()

int a,b,c;

printf("Enter first number\n");

scanf("%d",&a);

printf("Enter second number\n");

scanf("%d",&b);

printf("Enter third number\n");

scanf("%d",&c);

if(a>b&&a>c)

printf("%d is greater",a);

if(b>a&&b>c)

printf("%d is greater",b);

if(c>a&&c>b)

printf("%d is greater",c);
}

Q.no.6

#include <stdio.h>

int main()

{ int num;

printf("Enter any number: "); scanf("%d", &num);

if((num % 5 == 0) && (num % 11 == 0))

printf("Number is divisible by 5 and 11");

else

printf("Number is not divisible by 5 and 11");

return 0;

Q.no.7

#include<stdio.h>

int main()

int no;

printf("Enter any number\n");


scanf("%d",&no);

if(no%2==0)

printf("Even");

else

printf("Odd");

Q.no8

#include <stdio.h>

int main()

int y;

printf("Enter year: ");

scanf("%d",&y);

if(y % 4 == 0)

//Nested if else

if( y % 100 == 0)
{

if ( y % 400 == 0)

printf("%d is a Leap Year", y);

else

printf("%d is not a Leap Year", y);

else

printf("%d is a Leap Year", y );

else

printf("%d is not a Leap Year", y);

return 0;

Q.no.9

#include <stdio.h>

int main()

int n, r = 0;

printf("Enter a number to reverse\n");

scanf("%d", &n);

while (n != 0)
{

r = r * 10;

r = r + n%10;

n = n/10;

printf("Reverse of the number = %d\n", r);

return 0;

Q.no10

#include <stdio.h>

void main(){

int num,r,sum=0,t;

printf("Input a number: ");

scanf("%d",&num);

for(t=num;num!=0;num=num/10){

r=num % 10;

sum=sum*10+r;

if(t==sum)
printf("%d is a palindrome number.\n",t);

else

printf("%d is not a palindrome number.\n",t);

Q.no.11

#include<stdio.h>

int main()

int no,f=1;

printf("Enter any number\n");

scanf("%d",&no);

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

f=f*i;

printf("Factorial=%d",f);

Q.no.12

clude <stdio.h>

// Function declaration

double cube(double num);


int main()

int num;

double c;

//Inputting number from user

printf("Enter any number: ");

scanf("%d", &num);

c = cube(num);

printf("Cube of %d is %.2f", num, c);

return 0;

//Function to find cube of any number

double cube(double num)

return (num * num * num);

Q.no.13

#include<stdio.h>
#include<math.h>

double find_Diameter(double radius);

double find_Circumference(double radius);

double find_Area(double radius);

int main()

float radius, area, circumference, diameter;

printf("\n Please Enter the radius of a circle : ");

scanf("%f",&radius);

diameter = find_Diameter(radius);

circumference = find_Circumference(radius);

area = find_Area(radius);

printf("\n Diameter Of a Circle = %.2f\n", diameter);

printf("\n Circumference Of a Circle = %.2f\n", circumference);

printf("\n Area Of a Circle = %.2f\n", area);

return 0;

double find_Diameter(double radius)


{

return 2 * radius;

double find_Circumference(double radius)

return 2* M_PI * radius;

double find_Area(double radius)

return M_PI * radius * radius;

Q.no.14

#include<stdio.h>

int main() {

int original[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

int copied[10];

int loop;

for(loop = 0; loop < 10; loop++) {

copied[loop] = original[loop];

printf("original -> copied \n");


for(loop = 0; loop < 10; loop++) {

printf(" %2d %2d\n", original[loop], copied[loop]);

return 0;

Q.no.15

#include<stdio.h>

int main() {

int arr[30], element, num, i, location;

printf("\nEnter no of elements :");

scanf("%d", &num);

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

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

printf("\nEnter the element to be inserted :");

scanf("%d", &element);

printf("\nEnter the location");

scanf("%d", &location);
//Create space at the specified location

for (i = num; i >= location; i--) {

arr[i] = arr[i - 1];

num++;

arr[location - 1] = element;

//Print out the result of insertion

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

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

return (0);

Q.no.16

#include<stdio.h>

void main()

int a[100],i,n,pos;

printf("\nEnter no of elements\n");

scanf("%d",&n);
printf("Enter the elements\n");

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

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

printf("Elements of array are\n");

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

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

printf("Enter the position from which the number has to be deleted\n");

scanf("%d",&pos);

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

a[i]=a[i+1];

n=n-1;

printf("\nOn Deletion, new array we get is\n");

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

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

}
}

Q.no.17

#include <stdio.h>

#include <string.h>

int main( )

int len;

char array[20]="mayank" ;

len = strlen(array) ;

printf ( "\string length = %d \n" , len ) ;

return 0;

Q.no.18

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

char s1[200]="mayank";

char s2[200]="kumar";
printf("%s",strcat(s1,s2));

return 0;

Q.no.19

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

char name1[200]="mayank";

char name2[200]="kumar";

strcpy(name1,name2);

printf("%s",name1);

return 0;

Q.no.20

#include<stdio.h>

int main()

int no;

printf("Enter any number\n");

scanf("%d",&no);

switch(no)
{

case 1:

printf("Monday");

break;

case 2:

printf("Tuesday");

break;

case 3:

printf("Wednesday");

break;

case 4:

printf("Thrusday");

break;

case 5:

printf("Friday");

break;

case 6:

printf("Saturday");

break;

case 7:

printf("Sunday");

break;

default:

printf("Invalid input");

}
}

Q.no.22

#include <stdio.h>

int main()

char ch;

/* Input an alphabet from user */

printf("Enter any alphabet: ");

scanf("%c", &ch);

/* Switch value of ch */

switch(ch)

case 'a':

printf("Vowel");

break;

case 'e':

printf("Vowel");

break;

case 'i':

printf("Vowel");

break;
case 'o':

printf("Vowel");

break;

case 'u':

printf("Vowel");

break;

case 'A':

printf("Vowel");

break;

case 'E':

printf("Vowel");

break;

case 'I':

printf("Vowel");

break;

case 'O':

printf("Vowel");

break;

case 'U':

printf("Vowel");

break;

default:

printf("Consonant");

}
return 0;

Q.no22

#include<stdio.h>

#include<conio.h>

int main()

FILE *fp;

fp=fopen("/temp/demo.txt","w");

fprintf(fp,"Hello friends, how are you???.");

fclose(fp);

return 0;

Q.no23

// C program to illustate fgetc() function

#include <stdio.h>

int main ()

{
// open the file

FILE *fp = fopen("test.txt","r");

// Return if could not open file

if (fp == NULL)

return 0;

do

// Taking input single character at a time

char c = fgetc(fp);

// Checking for end of file


if (feof(fp))

break ;

printf("%c", c);

} while(1);

fclose(fp);

return(0);

Q.no24

#include <stdio.h>

int main() {

FILE *fp1, *fp2, *fp3, *fp4;

int n, i, num, flag = 0;

/* open data.txt in read mode */


fp1 = fopen("data.txt", "w");

printf("Enter the value for n:");

scanf("%d", &n);

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

fprintf(fp1, "%d ", i);

fprintf(fp1, "\n");

fclose(fp1);

/* open files to write even, odd and prime nos separately */

fp1 = fopen("data.txt", "r");

fp2 = fopen("even.txt", "w");

fp3 = fopen("odd.txt", "w");

fp4 = fopen("prime.txt", "w");

fprintf(fp2, "Even Numbers:\n");

fprintf(fp3, "Odd Numbers:\n");

fprintf(fp4, "Prime Numbers:\n");

/* print even, odd and prime numbers in separate files */

while (!feof(fp1)) {

fscanf(fp1, "%d", &num);

if (num % 2 == 0) {

fprintf(fp2, "%d ", num);

} else {

if (num > 1) {
for (i = 2; i < num; i++) {

if (num % i == 0) {

flag = 1;

break;

if (!flag) {

fprintf(fp4, "%d ", num);

fprintf(fp3, "%d ", num);

flag = 0;

fprintf(fp2, "\n");

fprintf(fp3, "\n");

fprintf(fp4, "\n");

/* close all opened files */

fclose(fp1);

fclose(fp2);

fclose(fp3);

fclose(fp4);

return 0;

}
Q.no.25

#include <stdio.h>

int main()

int num = 10;

printf("Value of variable num is: %d", num);

/* To print the address of a variable we use %p

* format specifier and ampersand (&) sign just

* before the variable name like &num.

*/

printf("\nAddress of variable num is: %p", &num);

return 0;

Q.no26

#include <stdio.h>

int main()

int first, second, *p, *q, sum;

printf("Enter two integers to add\n");

scanf("%d%d", &first, &second);

p = &first;
q = &second;

sum = *p + *q;

printf("Sum of the numbers = %d\n", sum);

return 0;

Qno27

#include <stdio.h>

int main()

int x, y, *a, *b, temp;

printf("Enter the value of x and y\n");

scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

a = &x;

b = &y;

temp = *b;
*b = *a;

*a = temp;

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;

You might also like