Most Impotant Questions from C
//Odd and even using function
#include<stdio.h>
int OE();
int main()
{
OE();
}
int OE()
{
int num ;
printf("Enter any number:");
scanf("%d",&num);
if(num%2==0)
{
printf("The number is Even");
}
else {
printf("The number is Odd");
}
return 0;
}
//Enter any number:123
//The number is Odd
//Recursive sum of digits
//revise formula...
//For factorial.....n*fname(n-1)
//For Fibnaccii.....f(n-1)+f(n-2)
#include<stdio.h>
int sum(int); //just decalring function
int main()
{
int num,x;
printf("Enter any number:");
scanf("%d",&num);
x=sum(num);
printf("%d",x);
return 0;
}
int sum(int num)
{
if (num<2)
{
return num;
}
else
{
return num+sum(num-1);
}
}
// Enter any number:5
// 15
//wap to ask a name of a student and rank using structure and print the data :
#include<stdio.h>
int main()
{
// array similar type of data
// strut different type of data
struct std{
char name[25];//int s[100]
int rank;
}s[100];
int i,num;
printf("Enter the number of datas :");
scanf("%d",&num);
for (i=0;i<num;i++)
{
printf("Enter name :");
scanf("%s",s[i].name);
printf("Enter rank :");
scanf("%d",&s[i].rank);
}
for (int j=0;j<num;j++)
{
printf("%s\t",s[j].name);
printf("%d\n",s[j].rank);
}
return 0;
}
//Ascending Order according to increasing salary
#include<stdio.h>
#include<string.h>
int main()
{
struct emp
{
char name[25];
float sal ;
}s[100];
int num;
printf("Enter the number of datas: ");
scanf("%d",&num);
int k;
for (k=0;k<num;k++)
{
printf("Enter name :");
scanf("%s",s[k].name);
printf("Enter salary :");
scanf("%f",&s[k].sal);
}
int i,j;
char tname[25];
float tsal;
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(s[i].sal<s[j].sal)
{
tsal=s[i].sal;
s[i].sal=s[j].sal;
s[j].sal=tsal;
strcpy(tname,s[i].name);
strcpy(s[i].name,s[j].name);
strcpy(s[j].name,tname);
}
}
}
printf("The data in sorted form is :\n\n");
printf("Name\t Salary\n");
int p;
for (p=0;p<num;p++)
{
printf("%s\t",s[p].name);
printf("%f\n",s[p].sal);
}
return 0;
}
/*
Enter the number of datas: 3
Enter name :aashik
Enter salary :1000
Enter name :sciencepic
Enter salary :10
Enter name :sujan
Enter salary :3000
The data in sorted form is :
Name
Salary
sujan
3000.000000
aashik 1000.000000
sciencepic
10.000000
*/
//Concept for Pointer
#include<stdio.h>
int main (){
int a=69;
int *l;
l=&a;
printf("%d\n",l);
printf("%d\n",*l);
printf("%d",a);
return 0;
}
/*
6487572
69
69
*/
//greatest between 2 number using pointer
#include<stdio.h>
int main()
{
int a,b;
int *p,*q;
printf("Enter any two numbers:");
scanf("%d%d",&a,&b);
p=&a;
q=&b;
if(*p>*q)
{
printf("The greatest number is : %d",*p);
}
else
{
printf("The greatest number is : %d",*q);
}
return 0;
}
/*
Enter any two numbers:12
23
The greatest number is : 23
*/
//file handling:
//wap to ask name and age and store it in std.txt
#include<stdio.h>
int main()
{
FILE*fptr;
fptr=fopen("std.txt","w");
int num,age;
char name[30];
printf("Enter the number of data: ");
scanf("%d",&num);
int i;
for (i=0;i<num;i++)
{
printf("Enter name: ");
scanf("%s",name);
fprintf(fptr,"%s\t",name);
printf("Enter age: ");
scanf("%d",&age);
fprintf(fptr,"%d\n",age);
}
return 0;
}
/*
Enter the number of data: 2
Enter name: Aashik
Enter age: 20
Enter name: Sujan
Enter age: 19
Aashik 20
Sujan 19
*/
//wap to ask and store name salary of employees and display the employee having salary more than 100
#include<stdio.h>
int main()
{
FILE*fptr;
fptr=fopen("emp.txt","w");
struct emp{
int sal;
char name[30];
}s[100];
int num;
printf("Enter the number of data: ");
scanf("%d",&num);
int i;
for (i=0;i<num;i++)
{
printf("Enter name: ");
scanf("%s",s[i].name);
fprintf(fptr,"%s\t",s[i].name);
printf("Enter sal: ");
scanf("%d",&s[i].sal);
fprintf(fptr,"%d\n",s[i].sal);
}
fclose(fptr);
fptr=fopen("emp.txt","r");
int j=0;
while(fscanf(fptr,"%s%d",s[j].name,&s[j].sal)!=EOF)
{
if(s[j].sal>100)
{
printf("%s\t%d\n",s[j].name,s[j].sal);
}
j+=1;
}
fclose(fptr);
return 0;
}
/*
the number of data: 3name: aashik
sal: 1000
name: sujan
sal: 2000
name: aashim
sal: 90
1000
2000
aashik 1000
sujan 2000
aashim 90
*/