Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

Programming in C Journal

Uploaded by

csteamforu2277
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)
47 views

Programming in C Journal

Uploaded by

csteamforu2277
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/ 51

Q1 write a program to display name and address.

#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Name : Prajapati Nain Haresh\n”);
printf(“Address : DMC 4-289-V-F-4 Narottam Niwas Mitnawad Road
Khariwad Nani Daman,Daman”);
getch();
}
Q2 write a program to define variables of different datatypes and
display their values.
#include<stdio.h>
#include<conio.h>
void main()
{
char name[]=”Prajapati Nain Haresh”;
char class[]=”F.Y.BSc Computer Science”;
char div=’A’;
int rollno=19;
float per=53.85;
printf(“Name : %s\n”,name);
printf(“Class : %s\n”,class);

created by Nain Prajapati


printf(“Division : %c\n”,div);
printf(“Roll No. : %d\n”,rollno);
printf(“Percentage : %f\n”,per);
getch();
}
Q3 Write a program to perform arithmetic operations on 2 numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2, answer;
printf(“Enter the first number: “);
scanf(“%d”, &num1);
printf(“Enter the second number: “);
scanf(“%d”, &num2);
answer = num1 + num2;
printf(“Addition: %d\n”, answer);
answer = num1 – num2;
printf(“Subtraction: %d\n”, answer);
answer = num1 * num2;
printf(“Multiplication: %d\n”, answer);

created by Nain Prajapati


if (num2 != 0)
{
answer = num1 / num2;
printf(“Division: %d\n”, answer);
}
else {
printf(“Cannot divide by zero.\n”);
}
getch();
}
Q4 write a program to check given number is even or odd.
#include <stdio.h>
#include<conio.h>
void main()
{
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num % 2 == 0)
{
printf(“%d is an even number.\n”, num);

created by Nain Prajapati


} else {
printf(“%d is an odd number.\n”, num);
}
getch();
}
Q5 write a program to check maximum numbers amongst 3 numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2, num3, max;
clrscr();
printf(“Enter three numbers: “);
scanf(“%d %d %d”, &num1, &num2, &num3);
max = num1;
if (num2 > max)
{
max = num2;
}
If (num3 > max)
{
max = num3;

created by Nain Prajapati


}
printf(“The maximum number is: %d\n”, max);
getch();
}
Q6 write a program to accept number value and check given value is
positive, negative or zero.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num > 0) {
printf(“%d is a positive number.\n”, num);
} else if (num < 0) {
printf(“%d is a negative number.\n”, num);
} else {
printf(“The number is zero.\n”);
}

created by Nain Prajapati


getch();
}
Q7 write a program to accept roll no , name , class and marks of 4
subjects calculate total and percentage,Also display grade.
#include <stdio.h>
#include<conio.h>
void main()
{
int rollno,std, marks[4], total = 0;
float percentage;
char name[50], grade;

printf (“Enter Class : “);


scanf(“%d”,&std);
printf(“Enter Roll Number: “);
scanf(“%d”, &rollno);
printf(“Enter Name: “);
scanf(“%s”, name);
printf(“\nEnter Marks for 4 Subjects:\n”);
for (int I = 0; I < 4; i++) {
printf(“Subject %d: “, I + 1);
scanf(“%d”, &marks[i]);

created by Nain Prajapati


total += marks[i];
}
percentage = total / 4;

if (percentage >= 90)


{
grade = ‘A’;
} else if (percentage >= 80)
{
grade = ‘B’;
} else if (percentage >= 70)
{
grade = ‘C’;
} else if (percentage >= 60)
{
grade = ‘D’;
} else
{
grade = ‘F’;
}
printf(“\nRoll Number: %d\n”, rollno);
printf(“Name: %s\n”, name);

created by Nain Prajapati


printf(“Total Marks: %d\n”, total);
printf(“Percentage: %f\n”, percentage);
printf(“Grade: %c\n”, grade);
getch();
}
Q8 Write a program to check given value is vowel or not (using switch
case).
#include<stdio.h>
#include<conio.h>
void main()
{
char n;
printf (“Enter A Value : \n”);
scanf (“%c”,&n);

switch(n)
{
case ’a’: printf(“vowel a”);
break;
case ’e’: printf(“vowel e”);
break;
case ’i’: printf(“vowel I”);

created by Nain Prajapati


break;
case ’o’: printf(“vowel o”);
break;
case ’u’: printf(“vowel u”);
break;

default : printf(“Wrong Input”);


}
getch();
}
Q9 write a program to accept the number value from 1 to 12 and
display the name of month according to given number value (using
switch case).
#include <stdio.h>
#include<conio.h>
void main()
{
int monthNumber;
printf(“Enter a number between 1 and 12: “);
scanf(“%d”, &monthNumber);

switch(monthNumber)
{

created by Nain Prajapati


case 1:
printf(“January\n”);
break;
case 2:
printf(“February\n”);
break;
case 3:
printf(“March\n”);
break;
case 4:
printf(“April\n”);
break;
case 5:
printf(“May\n”);
break;
case 6:
printf(“June\n”);
break;
case 7:
printf(“July\n”);
break;
case 8:

created by Nain Prajapati


printf(“August\n”);
break;
case 9:
printf(“September\n”);
break;
case 10:
printf(“October\n”);
break;
case 11:
printf(“November\n”);
break;
case 12:
printf(“December\n”);
break;
default:
printf(“Wrong input!\n”);
break;
}
getch();
}
Q10.1 write a program to display 1 to 5 numbers (using for loop).
#include<stdio.h>

created by Nain Prajapati


#include<conio.h>
void main()
{
clrscr();
int a ;
for(a=1;a<=5;a++)
{
printf (“ \n %d”,a);
}
getch();
}
Q10.2 Write a program to display 1 to 5 numbers (using while loop).
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a=1;
while(a<=5)
{
printf (“\n %d”,a);
a++;

created by Nain Prajapati


}
getch();
}
Q10.3 Write a program to display 1 to 5 numbers (using do while
loop).
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=1;
do
{
printf (“\n %d”,a);
a++;
}while(a<=5);
getch();
}
Q11 Write a program to display 1 to n numbers in reverse.
#include <stdio.h>
#include<conio.h>
void main()

created by Nain Prajapati


{
int n;
printf(“Enter a number: “);
scanf(“%d”, &n);

printf(“Numbers from 1 to %d in reverse:\n”, n);


for (int I = n; I >= 1; i--) {
printf(“%d “, i);
}
getch();
}
Q12 Write a program to accept n values and display the odd numbers
from 1 to n numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b=1;
clrscr();
printf(“Enter A Value : “);
scanf(“%d”,&a);
printf (“odd no.’s between 1 to %d are :”,a);

created by Nain Prajapati


while(b<=a)
{
if(b%2 !=0)
{
printf (“ \n %d is odd”,b);
}
b++;
}
getch();
}
Q13.1 write a program to draw following pattern.
*
* *
* * *
* * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j;
for(i=0;i<4;i++)
{

created by Nain Prajapati


for(j=0;j<=I;j++)
{
printf (“* “);
}
printf (“\n”);
}
getch();
}
Q13.2
* * * *
* * *
* *
*
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j;
for(i=4; i>=1; --i)
{
for(j=1; j<=I; ++j)
{

created by Nain Prajapati


printf (“* “);
}
printf (“\n”);
}
getch();
}
Q13.3
1
12
123
1234
#include <stdio.h>
#include<conio.h>
void main()
{
int I,j;
for (I = 1; I <= 4; i++)
{
for ( j = 1; j <= I; j++)
{
printf(“%d “, j);
}

created by Nain Prajapati


printf(“\n”);
}
getch();
}
Q13.4
1
22
333
4444
#include <stdio.h>
#include<conio.h>
void main()
{
int I,j; // Number of rows

for ( I = 1; I <= 4; i++)


{
for (j = 1; j <= I; j++)
{
printf(“%d “, i);
}
printf(“\n”);

created by Nain Prajapati


}
getch();
}
Q13.5
1
23
456
7 8 9 10
#include <stdio.h>
#include<conio.h>
void main ()
{
int I,j,k=1;
for ( I = 1; I <= 4; i++)
{
for ( j = 1; j <= I; j++)
{
printf(“%d “, k);
k++;
}
printf(“\n”);
}

created by Nain Prajapati


getch();
}
Q13.6
*
**
***
****
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,k;
for(i=0; i<5; i++)
{
for(k=I; k<=10; k++)
{
printf (“ “);
}
for(j=0; j<I; j++)
{
printf (“* “);

created by Nain Prajapati


}
printf (“\n”);
}
getch();
}
Q13.7
****
***
**
*
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,k;
for(i=0; i<4; i++)
{
for(k=0; k<I; k++)
{
printf(“ “);
}
for(j=0; j<4-I; j++)

created by Nain Prajapati


{
printf(“* “);
}
printf(“\n”);
}
getch();
}
Q13.8
*
**
***
****
***
**
*
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,k;
for(i=0; i<4; i++)
{

created by Nain Prajapati


for(k=I; k<=10; k++)
{
printf (“ “);
}
for(j=0; j<I; j++)
{
printf (“* “);
}
printf (“\n”);
}
for (i=4;i>0;i--)
{
for(k=10;k>=I;k--)
{
printf (“ “);
}
for(j=I;j>0;j--)
{
printf (“* “);
}
printf (“\n”);
}

created by Nain Prajapati


getch();
}
Q13.9
****
***
**
*
**
***
****
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,k;
for(i=1; i<4; i++)
{
for(k=I; k<=4; k++)
{
printf (“* “);
}
for(j=0; j<I; j++)

created by Nain Prajapati


{
printf (“ “);
}
printf (“\n”);
}
for (i=4;i>0;i--)
{
for(k=4;k>=I;k--)
{
printf (“* “);
}
for(j=I;j>0;j--)
{
printf (“ “);
}
printf (“\n”);
}
getch();
}

created by Nain Prajapati


Q13.10
*
**
***
****
***
**
*
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j;
for(i=0;i<=3;i++)
{
for (j=0;j<=I;j++)
{
printf (“* “);
}
printf (“\n”);
}

created by Nain Prajapati


for(i=3;i>=1;i--)
{
for(j=1;j<=I;j++)
{
printf (“* “);
}
printf (“\n”);
}
getch ();
}
Q14 write a program to define, store and display the values in one
dimension array.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5],i=0;
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;

created by Nain Prajapati


arr[4]=50;

while(i<5)
{
printf (“%d\n”,arr[i]);
i++;
}
getch();
}
Q15 write a program to find maximum and minimum value from one
dimension array.
#include <stdio.h>
#include<conio.h>
void main()
{
int size, I ,arr[10] ;
printf(“Enter the size of the array: “);
scanf(“%d”, &size);
printf(“Enter the elements of the array:\n”);
for (I = 0; I < size; i++)
{
scanf(“%d”, &arr[i]);

created by Nain Prajapati


}
int max = arr[0];
int min = arr[0];
for (I = 1; I < size; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
printf(“Maximum value: %d\n”, max);
printf(“Minimum value: %d\n”, min);
getch();
}
Q16 write a program to count sum of all the elements in one
dimension array.
#include <stdio.h>
#include<conio.h>
void main()

created by Nain Prajapati


{
int n ; // Number of elements in the array
printf(“Enter the number of elements: “);
scanf(“%d”, &n);

int arr[n]; // Declare an array of size n


printf(“Enter the elements:\n”);
// Input elements into the array
for (int I = 0; I < n; i++)
{
scanf(“%d”, &arr[i]);
}
int sum = 0; // Initialize sum to 0
// Calculate the sum of elements
for (int I = 0; I < n; i++)
{
sum += arr[i];
}
printf(“Sum of elements: %d\n”, sum);
getch();
}

created by Nain Prajapati


Q17 write a program to perform sorting on array (ascending and
descending order both).
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,I,j;
printf (“Enter Size Of Array : “);
scanf(“%d”,&n);

printf(“Enter Elements Of Array : “);


for(i=0; i<n; i++)
{
scanf (“%d”,&a[i]);
}

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


{
for(j=0; j<n; j++)
{
if(a[j]>a[i])
{

created by Nain Prajapati


int tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
}
}
printf (“\n\nAscending : “);
for(i=0; i<n; i++)
{
printf (“%d “,a[i]);
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(a[j]<a[i])
{
int tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
}

created by Nain Prajapati


}
printf (“\n\nDescending : “);
for(i=0; i<n; i++)
{
printf (“%d “,a[i]);
}
getch();
}
Q18 Write a program to demonstrate string and arithmetic built_in
functions
#include <stdio.h>
#include<conio.h>
#include <string.h> // Include the string.h library for string functions
void main() {
// String functions
char str1[] = “Hello, “;
char str2[] = “World!”;
char result[50]; // A buffer to store the result

// Concatenating two strings


strcpy(result, str1); // Copy the first string into the result buffer
strcat(result, str2); // Concatenate the second string to the result

created by Nain Prajapati


printf(“Concatenated String: %s\n”, result);
// Arithmetic functions
int num1;
printf(“Enter no. 1 : “);
scanf(“%d”,&num1);
int num2;
printf(“Enter no. 2 : “);
scanf(“%d”,&num2);
// Addition
int sum = num1 + num2;
printf(“Sum: %d\n”, sum);
// Subtraction
int difference = num1 – num2;
printf(“Difference: %d\n”, difference);
// Multiplication
int product = num1 * num2;
printf(“Product: %d\n”, product);
// Division
float quotient = (float)num1 / num2; // Cast one operand to float for
accurate division
printf(“Quotient: %.2f\n”, quotient);

created by Nain Prajapati


getch();
}
Q19 Write a program to calculate sum of 2 number using UDF. (Call by
value)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,tot;
printf(“Enter First Number: “);
scanf(“%d”,&a);
printf(“Enter Second Number: “);
scanf(“%d”,&b);
tot=total(a,b);
printf(“Sum of %d and%d is %d:”,a,b,tot);
getch();
}
int total(int a,int b)
{
int tot;

created by Nain Prajapati


tot=a+b;
return tot;
}
Q20 Write a program to find factorial of given number using UDF. (Call
by Ref)
#include<stdio.h>
#include<conio.h>
#include<math.h>
Void main()
{
int f;
printf(“Enter a Number to Find Factorial: “);
f=fact();
printf(“\nFactorial of a Given Number is: %d “,f);
getch();
}
int fact()
{
int i,fact=1,n;
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{

created by Nain Prajapati


fact=fact*I;
}
return fact;
}
Q21Write a program to accept employeeno, names and basic salary of
5 employees And sisplay it. (Using structure )
#include<stdio.h>
#include<conio.h>
void main()
{
struct emp
{
int eno;
char ename[20];
float basic;
}e[5];
int I;
float tot = 0;
clrscr();
for(i=0;i<=4;i++)
{

created by Nain Prajapati


printf(“\nEnter Emp No: ”);
scanf(“%d”,&e[i].eno);
printf(“\nEnter Emp Name: ”);
scanf(“%s”,&e[i].ename);
printf(“\nEnter Basic: ”);
scanf(“%f”,&e[i].basic);
}
printf(“\nEmpNo \t Name \t Basic”);
printf(“\n================================”);
for(i=0;i<=4;i++)
{
printf(“\n%d\t %s\t %.2f”,e[i].eno,e[i].ename,e[i].basic);
tot = tot + e[i].basic;
}
printf(“\nTotal Basic = %.2f”,tot);
getch();
}

created by Nain Prajapati


Q22 Write a program to print string in reverse order using pointer and
UDF
#include<stdio.h>
#include<conio.h>
void xstrrev(char *p1)
{
int 1=0, i;
while(*p1!=’\0’)
{
1++;
p1++;;
}
printf(“\nThe reverse string is: ”);
for(i=0;i<=l;i++)
{
printf(“%c”,*p1);
p1--;
}
}
void main()
{
char s[100];

created by Nain Prajapati


clrscr();
printf(“\nEnter the string: ”);
gets(s);
xstrrev(s);
getch();
}

Q23 Write a program to add two matrices using pointer.


#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,s=0;
int a[3][3],b[3][3],c[3][3];
int *p1=&a[0][0];
int *p2=&b[0][0];
int *p3=&c[0][0];
clrscr();
printf(“\nMatrix 1:\n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)

created by Nain Prajapati


{
printf(“\nEnter element [%d][%d] : “,I,j);
scanf(“%d”,(p1+3*i+j));
}
}
printf(“\nMatrix 2:\n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf(“\nEnter element [%d][%d] : “,I,j);
scanf(“%d”,(p2+3*i+j));
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
*(p3+3*i+j)= (*(p1+3*i+j))+(*(p2+3*i+j));
}
}

created by Nain Prajapati


printf(“\nAnswer is:\n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf(“%d\t”,*(p3+3*i+j));
}
printf(“\n”);
}
getch();
}

Q24 Write a program to accept numbers from the user and write all
the data into file Called “data.txt”. Then write a program to read this
file and write odd numbers Into “odd.txt” and even numbers into
“even.txt” file. Then write a program to Read the contents of both this
file.
#include< stdio.h >
#include<conio.h>
void main()
{
int n;

created by Nain Prajapati


char ch;
FILE *fp1, *fp2, *fp3;
fp1 = fopen(“data.txt”,”w”);
clrscr();
//To accept data from user & write all numbers into data.dat file
do
{
printf(“\nEnter data: ”);
scanf(“%d”,&n);
putw(n,fp1);
printf(“\n\tContinue: (y/n): ”);
ch = getche();
}while(ch==’y’ || ch==’Y’);
fclose(fp1);
/* Read data from data.dat file, write odd numbers into odd.dat file and
even
Numbers into even.dat file */
fp1 = fopen(“data.txt”,”r”);
fp2 = fopen(“odd.txt”,”w”);
fp3 = fopen(“even.txt”,”w”);
while((n=getw(fp1)) != EOF)

created by Nain Prajapati


{
if((n%2) == 0)
putw(n, fp3);
else
putw(n,fp2);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
// Read file odd.dat and even.dat
fp2 = fopen(“odd.txt”,”r”);
fp3 = fopen(“even.txt”,”r”);
printf(“\nThe list of odd numbers are: \n”);
while((n=getw(fp2)) != EOF)
printf(“%5d”,n);
printf(“\nThe list of even numbers are: \n”);
while((n=getw(fp3)) != EOF)
printf(“%5d”,n);
fclose(fp2);
fclose(fp3);
getch();
}

created by Nain Prajapati


OR
#include< stdio.h >
#include<conio.h>
void main()
{
FILE *fp1,*fp2,*fp3;
int n, I;
printf(“Writing integer data into File. \n”);
fp1=fopen(“data.dat”,”w”);
for(i=1;i< 10;i++)
{
scanf(“%d”,&n);
If(n==-1)
break;
putw(n,fp1);
}
fclose(fp1);
fp1=fopen(“data”,”r”);
fp2=fopen(“odd”,”w”);
fp3=fopen(“even”,”w”);
while((n=getw(fp1)) != EOF)

created by Nain Prajapati


{
if(n%2==0)
putw(n,fp3);
else
putw(n,fp2);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp2=fopen(“odd”,”r”);
fp3=fopen(“even”,”r”);
printf(“\nContents of the odd file\n”);
while(n=getw(fp2)) != EOF)
{
printf(“ %d”,n);
}
printf(“\nContents of the even file\n”);
while(n=getw(fp3)) != EOF)
{
printf(“%d”,n);
}

created by Nain Prajapati


fclose(fp2);
fclose(fp3);
getch();
}

Q25 Create a random file which store information of employees


(emp_no, emp_name, Salary). Provide add, view, append options.
Write a menu driven program
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
int sal,eno;
char ename[10];
void input()
{
printf(“\nenter no “);
scanf(“%d”,&eno);
printf(“\nenter name”);
scanf(“%c”,&ename;
printf(“\nenter salary “);
scanf(“%d”,&sal);
}
int main()

created by Nain Prajapati


{
int ch;
emp e;
fstream file;
file.open(“e10.dat”,ios::in | ios::out | ios::ate);
clrscr();
printf(“\n1.add”);
printf(“\n2.display “);
printf(“\n3.modify “);
printf(“\n4.exit “);
do
{
printf(“\nenter ur choice “);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
e.input();
file.write((char *)& e,sizeof€);
break;
case 2:
file.seek(0);

created by Nain Prajapati


printf(“\neno\tename\tsalary\n”);
printf(“\n=============================”);
while(file.read(char *)& e,sizeof€);
file.clear();
break;
case 3:
int obj,loc;
printf(“\nenter record no tobe update”);
sanf(“%d”,&obj;
loc=(obj-1)*sizeof€;
file.seekp(loc);
printf(“\nenter new value “);
e.input();
file.write((char *)&e,sizeof€);
break;
default:
printf(“\nwrong choice”);
}
}while(ch!=4);
file.close();

created by Nain Prajapati


getch();
return 0;
}

Q26 Write a program to stimulate type command using command line


argument.
#include<conio.h>
#include< stdio.h >
void main(int argc, char *argv[])
{
FILE *fp;
char c;
if(argc < 2)
printf(“\nEnter a file name”);
else
{
fp = fopen(argv[1], “r”);
while( (c = getc(fp) ) != EOF)
putchar©;
fclose(fp);
}
getch();

created by Nain Prajapati


}

Q27 Write a program to count total no. of arguments and display all
arguments.
#include<conio.h>
#include< stdio.h >
void main(int argc, char *argv[])
{
int I;
printf(“\nTotal no of arguments are %d”,argc);
for(i=0;i<argc;i++)
printf(“\n\t%s”,argv[i]);
}

created by Nain Prajapati

You might also like