You are on page 1of 11

1.

write a c program to take input of name ,rollno and marks obtained by a student
in 4 subjects of 100 marks each and display the name,rollno with percentage score
secured

#include<stdio.h>
int main()
{
char name[50];
int marks[4],rollno,i;
float sum=0,avg;
printf("Enter name: ");
scanf("%s",name);
printf("Enter rollno: ");
scanf("%d",&rollno);
printf("Enter marks of 4 subjects: \n");
for(i=0;i<4;i++)
{
scanf("%d",&marks[i]);
sum=sum+marks[i];
}
avg=(sum/400)*100;
printf("Name: %s \nRoll no: %d \nPercentage: %f\n",name,rollno,avg);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------
2.write a c program to input two number and display the maximum number

#include<stdio.h>
int main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
if (x > y)
{
printf("%d is maximum", x);
}
else
{
printf("%d is maximum", y);
}
return 0;
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------
3.write a c program to find whether a character is consonant or vowel using switch
statement

#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);

switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is a consonant.\n", ch);
}

return 0;
}
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
4.write a c program to print positive integers from 1 to 10

#include <stdio.h>

int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
5.write a c program to check whether a number is palindrome or not

#include <stdio.h>

int main()
{
int n, reverse = 0, t;

printf("Enter a number to check if it is a palindrome or not\n");


scanf("%d", &n);

t = n;

while (t != 0)
{
reverse = reverse * 10;
reverse = reverse + t%10;
t = t/10;
}

if (n == reverse)
printf("%d is a palindrome number.\n", n);
else
printf("%d isn't a palindrome number.\n", n);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
6.write a c program to generate fibonacci series

#include<stdio.h>

int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: ");

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


{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------
7.write a c program to insert 5 elements into an array and print the elements of
the array

#include <stdio.h>

int main()
{
int array[5];
int i;

printf("Enter the elements of the array:\n");


for (i = 0; i < 5; i++)
{
scanf("%d", &array[i]);
}

printf("The elements of the array are:\n");


for (i = 0; i < 5; i++)
{
printf("%d\n", array[i]);
}

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
8.write a c program to reverse the array elements in c programming

#include <stdio.h>

int main()
{
int n, c, d, a[100], b[100];

printf("Enter the number of elements in array\n");


scanf("%d", &n);

printf("Enter the array elements\n");

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


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

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


b[d] = a[c];

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


a[c] = b[c];

printf("Reverse array is\n");

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


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

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
9.write a c program to concatenate two strings

#include <stdio.h>
#include <string.h>

int main()
{
char str1[100] = "Hello";
char str2[100] = "World";

int len = strlen(str1);

int i;
for (i=0; str2[i]!='\0'; i++)
str1[len+i] = str2[i];

str1[len+i] = '\0';

printf("%s",str1);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
10.write a c program to compare two strings without using string library functions

#include <stdio.h>

int main()
{
char str1[100], str2[100], i, j;
printf("Enter first string: ");
scanf("%s", str1);

printf("Enter second string: ");


scanf("%s", str2);

for(i = 0; str1[i] == str2[i] && str1[i] != '\0'; i++);

if(str1[i] > str2[i])


printf("str1 > str2");
else if(str1[i] < str2[i])
printf("str1 < str2");
else
printf("str1 = str2");

return 0;
}
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------
11.write a c program to generate fibonacci series using recursive function

#include <stdio.h>

int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}

int main ()
{
int n = 9;
printf("Fibonacci series is \n");
int i;
for (i = 0; i < n; i++)
printf("%d ", fib(i));
getchar();
return 0;
}
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------
12.write a c program to find power of any number using recursion

#include <stdio.h>

int power(int x, int y)


{
if (y != 0)
return (x * power(x, y - 1));
else
return 1;
}

int main()
{
int x, y;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of y: ");
scanf("%d", &y);
printf("Power of %d to the %d is: %d", x, y, power(x, y));
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
13.write a c program to add,subtract, multiply and divide two integers using user
defined type function with return type

#include <stdio.h>

int add(int a, int b);


int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);

int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

int sum = add(num1, num2);


int difference = subtract(num1, num2);
int product = multiply(num1, num2);
int quotient = divide(num1, num2);

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


printf("Difference = %d\n", difference);
printf("Product = %d\n", product);
printf("Quotient = %d\n", quotient);

return 0;
}

int add(int a, int b)


{
int result = a + b;
return result;
}

int subtract(int a, int b)


{
int result = a - b;
return result;
}

int multiply(int a, int b)


{
int result = a * b;
return result;
}

int divide(int a, int b)


{
int result = a / b;
return result;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
14.write a c program to swap two integer using call by value and call by refernce
methods of passing arugments to a function

#include<stdio.h>

// Call by value
void swap1(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}

// Call by reference
void swap2(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

// Driver Code
int main()
{
int a, b;
a = 10;
b = 15;
printf("a = %d, b = %d\n",a,b);

// Call by value
swap1(a, b);
printf("After swap1 a = %d, b = %d\n",a,b);

// Call by reference
swap2(&a, &b);
printf("After swap2 a = %d, b = %d\n",a,b);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
15.write a c program to find biggest among three numbers using pointer

#include <stdio.h>

int main()
{
int a, b, c;
int *p, *q, *r;

p = &a;
q = &b;
r = &c;

printf("Enter three numbers: ");


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

if (*p > *q && *p > *r)


printf("%d is the largest number.", *p);

else if (*q > *p && *q > *r)


printf("%d is the largest number.", *q);

else
printf("%d is the largest number.", *r);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
16.write a c program to compare two strings using pointers

#include <stdio.h>

int main()
{
char str1[100], str2[100];
int i;

printf("Enter the first string\n");


scanf("%s", str1);

printf("Enter the second string\n");


scanf("%s", str2);

char *p1 = str1;


char *p2 = str2;

while(*p1 != '\0' && *p2 != '\0')


{
if(*p1 != *p2)
{
break;
}
p1++;
p2++;
}

if(*p1 == '\0' && *p2 == '\0')


{
printf("The two strings are equal\n");
}
else
{
printf("The two strings are unequal\n");
}

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
17.write a c program to create,declare and initialize structure

#include<stdio.h>
struct student
{
int roll;
char name[20];
char department[20];
float marks;
};

int main()
{
struct student s = {09, "Dheen", "MCA", 95.5};
printf("Roll: %d\n", s.roll);
printf("Name: %s\n", s.name);
printf("Department: %s\n", s.department);
printf("Marks: %f\n", s.marks);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
18.write a c program to declare ,intialize an UNION

#include <stdio.h>

union Value
{
int x;
float y;
char z;
};

int main()
{
union Value val;

val.x = 10;
printf("val.x: %d\n", val.x);

val.y = 12.5;
printf("val.y: %f\n", val.y);

val.z = 'A';
printf("val.z: %c\n", val.z);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
19.write a c program to create a file called emp.rec and store information about a
person ,in terms of his name, age and salary

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char name[30];
int age;
float salary;

//open file for writing


fp = fopen("emp.rec", "w");

if (fp == NULL)
{
puts("Cannot open file");
exit(1);
}

// read name, age and salary


printf("Enter name: ");
scanf("%s", name);

printf("Enter age: ");


scanf("%d", &age);

printf("Enter salary: ");


scanf("%f", &salary);

// write to file
fprintf(fp, "Name: %s\n", name);
fprintf(fp, "Age: %d\n", age);
fprintf(fp, "Salary: %f\n", salary);

printf("\nFile created and saved successfully!\n");

// close file
fclose(fp);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
20.write a c program to list all files and sub-directories in a directory

#include <stdio.h>
#include <dirent.h>

int main(void)
{
struct dirent *de; // Pointer for directory entry

// opendir() returns a pointer of DIR type.


DIR *dr = opendir(".");

if (dr == NULL) // opendir returns NULL if couldn't open directory


{
printf("Could not open current directory" );
return 0;
}
// Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
// for readdir()
while ((de = readdir(dr)) != NULL)
printf("%s\n", de->d_name);

closedir(dr);
return 0;
}
---------------------------------------------------------------------
finished---------------------------------------------------------------------------
---------------

You might also like